2017-03-29 17 views
0

私は1つのテーブルからデータを取得したいが、データが条件に基づいており、これは私のクエリです:異なる条件でのテーブルから同じ列のマージ

Select 
    Store_ID, 
    (select Stc_Item_Desc, Imported_Date, Store_ID, Quantity_On_Hand 
    from DailyStockHistory 
    where Stc_Item_Desc = 'SAWA/QuickNet prepaid triosim' 
     and Imported_Date = '3-15-2017' 
     and Store_ID like 'S%'), 
    (select Stc_Item_Desc, Imported_Date, Store_ID, Quantity_On_Hand 
    from DailyStockHistory 
    where Stc_Item_Desc = 'SAWA/QuickNet prepaid triosim' 
     and Imported_Date = '3-22-2017' 
     and Store_ID like 'S%') 
from 
    DailyStockHistory 

すべてのヘルプしてください。

ありがとうございます。

+0

は、いくつかのサンプルデータと期待されるデータのサンプルを提供できます。 – csharpbd

+0

SAWA/QUICKnet TrioSIM \t 2017年3月15日のプリペイド\t SCCC387 \t 518 SAWA/QUICKnet TrioSIM \t 2017年3月22日\t SCCC387 \t 251にデータをプリペイド互いに – ahmad

+0

の近くに私はちょうど同じテーブル – MaCron

答えて

1

期待する出力を得るにはPIVOTテーブルを生成する必要がありますので、これをチェックしてください。

サンプルデータ:

Insert Into DailyStockHistory Values('SCCC387','SAWA/QuickNet prepaid triosim','2017-03-15',518) 
Insert Into DailyStockHistory Values('SCCC387','SAWA/QuickNet prepaid triosim','2017-03-22',251) 

SQL:

Select 
    * 
From 
(
    Select 
     Stc_Item_Desc, Imported_Date, Store_ID, Quantity_On_Hand 
    From DailyStockHistory 
    Where Stc_Item_Desc = 'SAWA/QuickNet prepaid triosim' 
     And Store_ID Like 'S%' 
     And (Imported_Date = '3-15-2017' Or Imported_Date = '3-22-2017') 
) AS Data 
PIVOT 
(
    SUM(Quantity_On_Hand) 
    FOR Imported_Date IN ([3-15-2017],[3-22-2017]) 
) AS PIVOTData 

出力:Quantity_On_Hand数量accordiため

enter image description here

3-15-2017 & 3-22-2017がある列〜Imported_Date日付。

1

は、なぜあなたは代わりに2クエリ結果を結合する1つのクエリで条件を結合していない:

Select Store_ID, 
    (select Stc_Item_Desc, Imported_Date,Store_ID, Quantity_On_Hand 
    from DailyStockHistory 
    where Stc_Item_Desc = 'SAWA/QuickNet prepaid triosim' AND 
     ((Imported_Date = '3-15-2017') OR (Imported_Date = '3-22-2017')) 
     AND Store_ID like 'S%' 
    ) 
    from DailyStockHistory 

は、特定の日のためにQuantity_On_Handを取得するには、単に別のクエリを行うことができ

 select Quantity_On_Hand 
    from *above result* 
    where Imported_Date = '3-15-2017' 
+0

親愛なる@Mhd原因私はQuantity_On _Handとそれらの日付の異なる必要があると私はクエリをexcuteサブクエリがEXISTS " – ahmad

+0

@ahmadで更新された応答を見て導入されていないときに、選択リストで1つの式を指定できます。あなたは 'Imported_Date'を選択しているので、グループ化して、あなたが望む日付を数えることができます。 – Mhd

+0

どうすればいいですか? – ahmad

0
Select 
    Stc_Item_Desc, Imported_Date, Store_ID, Quantity_On_Hand 
From 
    DailyStockHistory 
Where 
    Stc_Item_Desc = 'SAWA/QuickNet prepaid triosim' 
    And Store_ID Like 'S%' 
    And (Imported_Date = '3-15-2017' Or Imported_Date = '3-22-2017) 
+0

buddy @MaCron – ahmad

関連する問題