2015-04-17 111 views
1
select 
    store, sum(value) 
from 
    rms.sa_tran_head 
where 
    store_day_seq_no in (select store_day_seq_no 
         from rms.sa_store_day 
         where store in (3003) 
          and business_date = '01-JAN-2015') 
    and tran_type in ('SALE', 'RETURN') 
group by 
    store; 

如何使用連接到上面寫查詢..寫查詢連接

SELECT 
    sd.store, 
    SUM(TH.VALUE) AS GROSS , 
    SUM(ti.qty) AS QTY 
FROM rms.sa_tran_head AS th 
    JOIN rms.sa_store_day AS sd 
     ON th.store_day_seq_no = sd.store_day_seq_no 
    JOIN rms.sa_tran_item AS ti 
     ON ti.tran_seq_no = th.tran_seq_no 
WHERE sd.store in (3003) --in (3003) use in if more than 1 value 
    AND sd.business_date = '01-JAN-2015' 
    AND th.tran_type IN ('SALE','RETURN') 
GROUP BY 
    sd.store; 

當我添加另一個表則顯示了不同價值觀的其他列...

+0

你有一個引用鍵來加入兩個表嗎? –

回答

1

我認爲這很好。

SELECT 
    t1.store, 
    SUM(t1.Value) AS Sum_Value 
FROM rms.sa_tran_head t1 
INNER JOIN sa_store_day t2 ON t1.store_day_seq_no = t2.store_day_seq_no 
WHERE t2.store IN (3003) 
    AND t2.business_date = '01-JAN-2015' 
    AND t1.tran_type IN ('SALE' , 'RETURN') 
GROUP BY t1.store 
+0

謝謝你... :) – Sr3vs

+0

SELECT sd.store,SUM(TH.VALUE)AS GROSS,SUM( ti.qty)AS QTY FROM rms.sa_tran_head第 JOIN rms.sa_store_day SD ON th.store_day_seq_no = sd.store_day_seq_no JOIN rms.sa_tran_item TI 上ti.tran_seq_no = th.tran_seq_no WHERE sd.store在(3003 )--in(3003)如果超過1個值,則使用 AND sd.business_date = '01 -JAN-2015' AND th.tran_type IN('SALE','RETURN') group by sd.store; 當我將另一列添加到上面的查詢值正在改變...你能幫我嗎.. – Sr3vs

+0

取決於如果原始查詢的子選擇包含重複與否。如果是這樣,這個查詢的SUM是錯誤的,因爲同一個項目可以包含多次。首先在irms.sa_store_day上執行DISTINCT以避免此問題。 – jarlh

2

我以爲store_day_seq_no是表rms.sa_store_day的FK,JOIN這樣的查詢,

SELECT 
    sd.store, 
    SUM(sd.value) AS [Sum] 
FROM rms.sa_tran_head AS th 
    JOIN rms.sa_store_day AS sd 
     ON th.store_day_seq_no = sd.store_day_seq_no 
WHERE 
    sd.store = 3003 --in (3003) use in if more than 1 value 
    AND sd.business_date = '01-JAN-2015' 
    AND th.tran_type IN ('SALE','RETURN') 
GROUP BY 
    sd.store; 
+0

謝謝.. :) @Selva TS – Sr3vs

+0

SELECT sd.store,SUM(TH.VALUE)以總,SUM(ti.qty)AS數量 FROM rms.sa_tran_head日 JOIN rms.sa_store_day SD ON th.store_day_seq_no = sd.store_day_seq_no 上ti.tran_seq_no JOIN rms.sa_tran_item TI = th.tran_seq_no WHERE sd.store在(3003)--in(3003),如果超過1個值 AND sd.business_date = '01使用 - JAN-2015' AND th.tran_type IN('SALE','RETURN') group by sd.store; 當我添加其他表的其他列時,「值」正在改變......你能幫我一把嗎 – Sr3vs

+0

@SreerangaVinjamuri你可以請所有表格的'schema'。 –