1

クエリ:

SELECT          
         item_descr
        ,shop_id
        ,COUNT(item_descr) as times
from [Order] 
group  by item_descr , shop_id
order by shop_id , times desc

結果:

item_descr  shop_id    times
product A     shop1      5
product B     shop1      3
product A     shop2      6
product B     shop2      2

予想された結果:

item_descr   shop1   shop2 
product A     5        6
product B     3        2

期待される結果を得るためにクエリを変更するにはどうすればよいですか?

4

2 に答える 2

0

ピボットの使用

Select item_descr, [shop1], [Shop2]
from 
(
  Select item_descr, shop_id, times
  from [Order]  
)p
pivot 
(
  sum(times)
  for shop_id in ([shop1], [Shop2])
)pvt;

SQL FIDDLEでのデモ

于 2013-05-30T12:46:54.067 に答える