How many stores are there?

來源: 萬見愁 2009-06-21 00:30:31 [] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (744 bytes)
Each store needs a column for total quantity on each item. If total number of stores varies every time you run the query, you need to write a stored procedure to determine the total number of stores first, and then use the dynamic SQL to create a temporary table has enough columns for each store.

If total number of stores is fixed, you can code the SQL as follows:

select item,
sum(case when store = 1 then qty else 0 end)'store1',
sum(case when store = 2 then qty else 0 end)'store2',
sum(case when store = 3 then qty else 0 end)'store3',
sum(case when store = 4 then qty else 0 end)'store4',
sum(qty)'total'
from tblname
group by item
order by item
請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”