标题: 语句简化 [打印本页] 作者: yekai_hua 时间: 2010-5-27 19:15 标题: 语句简化 SELECT md,(select sum (tt.xs) from tt where yf= 1 and md = "aa") as 1y,(select sum (tt.xs) from tt where yf= 2 and md = "aa") as 2y,(select sum (tt.xs) from tt where yf= 3 and md = "aa") as 3y
FROM tt
where md = "aa"
GROUP BY tt.md
UNION ALL
SELECT md,(select sum (tt.xs) from tt where yf= 1 and md = "bb") as 1y,(select sum (tt.xs) from tt where yf= 2 and md = "bb") as 2y,(select sum (tt.xs) from tt where yf= 3 and md = "bb") as 3y
FROM tt
where md = "bb"
GROUP BY tt.md
UNION ALL SELECT md,(select sum (tt.xs) from tt where yf= 1 and md = "cc") as 1y,(select sum (tt.xs) from tt where yf= 2 and md = "cc") as 2y,(select sum (tt.xs) from tt where yf= 3 and md = "cc") as 3y
FROM tt
where md = "cc"
GROUP BY tt.md;
麻烦那位大虾帮忙简化下以上这段东东作者: ycxchen 时间: 2010-5-27 20:10
看似天书,没有例子谁知是什么东东?作者: yekai_hua 时间: 2010-5-27 20:17
门店号 月份 销售
aa 1 1
aa 2 2
aa 3 3
bb 1 4
bb 2 5
bb 3 6
cc 1 7
cc 2 8
cc 3 9作者: yekai_hua 时间: 2010-5-27 20:19
把上面那个表按门店 月份汇总
结果为
1月 2月 3月
aa 1 2 3
bb 4 5 6
cc 7 8 9作者: andymark 时间: 2010-5-27 20:24
晕死,用交叉表来处理呀作者: yekai_hua 时间: 2010-5-27 20:28
交叉表
萨意思请指教作者: yekai_hua 时间: 2010-5-28 11:26
TRANSFORM Sum(tt.[xs])
SELECT tt.[md]
FROM tt
GROUP BY tt.[md]
PIVOT tt.[yf];
已简化 分享下呵呵 谢谢楼上的大哥