解决方案 »
- Sqlserver2008如何修改字段的数据类型
- [SQL 2005] 提示未正确安装公共语言运行时(CLR) v2.0.50727。需要有 CLR 才能使用 SQL/CLR 功能。
- sqlserver中的表的某一列属性为image时,怎么插入图象呀?
- 怎么能把查询分析器的执行结果,连同列名整齐的贴出来。(以方便提问)
- 附加特定的条件怎么写?
- 为什么SQL语句中,where后面不能跟变量?
- 关于触发器问题
- sql语句中间的几个小问题,请教
- VC通过ODBC连接Sql Server数据库的问题
- sql server执行字符串比较,不认中文。
- SQL查询问题,用一条SQL语句实现。之前发过一篇,有点乱。整理下重发
- 求大虾相助,紧急!
只有一个表。这个机试的一个题目。
然后再行转列啊。
sum(case when BizType='p' then Month1Num else 0 end) as p,
sum(case when BizType='s' then Month1Num else 0 end) as s,
sum(case when BizType='i' then Month1Num else 0 end) as i
from 表 group by 大类,小类,型号
union all
select 大类,小类,型号,'2' 月份,
sum(case when BizType='p' then Month2Num else 0 end) as p,
sum(case when BizType='s' then Month2Num else 0 end) as s,
sum(case when BizType='i' then Month2Num else 0 end) as i
from 表 group by 大类,小类,型号
union all
select 大类,小类,型号,'3' 月份,
sum(case when BizType='p' then Month3Num else 0 end) as p,
sum(case when BizType='s' then Month3Num else 0 end) as s,
sum(case when BizType='i' then Month3Num else 0 end) as i
from 表 group by 大类,小类,型号
.... 有几个月union 前提 一个大类,小类,型号 在表中 只用 p s i 3条记录
select
BigCategory,SmallCategory,Model_No ,Month
max(case BizType when 'p' then Month1Num else 0 end) as 'P',
max(case BizType when 'S' then Month1Num else 0 end) as 'S',
max(case BizType when 'I' then Month1Num else 0 end) as 'I',
from
(
select BigCategory,SmallCategory,Model_No ,BizType,Month1Num,1 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month2Num,2 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month3Num,3 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month4Num,4 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month5Num,5 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month6Num,6 as [Month] from tb
)t
group by
BigCategory,SmallCategory,Model_No ,Month盘点不知道怎么算的 自己去算下
BigCategory,SmallCategory,Model_No ,Month
sum(case BizType when 'p' then Month1Num else 0 end) as 'P',
sum(case BizType when 'S' then Month1Num else 0 end) as 'S',
sum(case BizType when 'I' then Month1Num else 0 end) as 'I',
from
(
select BigCategory,SmallCategory,Model_No ,BizType,Month1Num,1 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month2Num,2 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month3Num,3 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month4Num,4 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month5Num,5 as [Month] from tb
union all
select BigCategory,SmallCategory,Model_No ,BizType,Month6Num,6 as [Month] from tb
)t
group by
BigCategory,SmallCategory,Model_No ,Month应该是SUM
变成每个月多少量,然后在行转列,但BizType进行行转列
哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀
哎哟,还发现一个问题用SUM的话得到的是总的数量,用MAX是得到最大数量。这样结果就不对呀用sum是求和吧,总的数量,那么会有什么问题呢
我要的不是求和呀。是每个月份对应的 P S I 而不是总的P S I
我要的不是求和呀。是每个月份对应的 P S I 而不是总的P S I哦,会不会,在一个月同一个大类、小类、款式,有2个P呢
select BigCategory,SmallCategory,Model_No,month,P=sum(case when BizType='P' then Month1Num else 0 end),
S=sum(case when BizType='S' then Month1Num else 0 end),
I=sum(case when BizType='I' then Month1Num else 0 end),
盘点=sum(case when BizType='P' then Month1Num else 0 end-case when BizType='S' then Month1Num else 0 end-case when BizType='I' then Month1Num else 0 end)
from
(
select BigCategory,SmallCategory,Model_No,BizType,1 as month,Month1Num from Phone
union all select BigCategory,SmallCategory,Model_No,BizType,2,Month2Num from Phone
union all select BigCategory,SmallCategory,Model_No,BizType,3,Month3Num from Phone
union all select BigCategory,SmallCategory,Model_No,BizType,4,Month4Num from Phone
union all select BigCategory,SmallCategory,Model_No,BizType,5,Month5Num from Phone
union all select BigCategory,SmallCategory,Model_No,BizType,6,Month6Num from Phone
)t
group by BigCategory,SmallCategory,Model_No,month
order by BigCategory
/*
BigCategory SmallCategory Model_No month P S I 盘点
-------------------------------------------
电脑 联想 V480 1 21 0 0 21
电脑 联想 V480 2 30 0 0 30
电脑 联想 V480 3 30 0 0 30
电脑 联想 V480 4 30 0 0 30
电脑 联想 V480 5 30 0 0 30
电脑 联想 V480 6 30 0 0 30
电脑 联想 Z370 1 20 0 0 20
电脑 联想 Z370 2 20 0 0 20
电脑 联想 Z370 3 20 0 0 20
电脑 联想 Z370 4 20 0 0 20
电脑 联想 Z370 5 21 0 0 21
电脑 联想 Z370 6 20 0 0 20
手机 HTC BUTTERFLY 1 10 1 8 1
手机 HTC BUTTERFLY 2 10 1 9 0
手机 HTC BUTTERFLY 3 10 1 9 0
手机 HTC BUTTERFLY 4 10 2 9 -1
手机 HTC BUTTERFLY 5 10 1 9 0
手机 HTC BUTTERFLY 6 10 1 9 0
手机 苹果 IPHONE4S 1 10 10 0 0
手机 苹果 IPHONE4S 2 10 10 0 0
手机 苹果 IPHONE4S 3 10 10 0 0
手机 苹果 IPHONE4S 4 10 10 0 0
手机 苹果 IPHONE4S 5 10 10 0 0
手机 苹果 IPHONE4S 6 10 10 0 0
手机 苹果 IPHONE5 1 10 5 5 0
手机 苹果 IPHONE5 2 10 4 6 0
手机 苹果 IPHONE5 3 10 3 7 0
手机 苹果 IPHONE5 4 10 7 3 0
手机 苹果 IPHONE5 5 10 2 8 0
手机 苹果 IPHONE5 6 10 9 1 0
*/