一个有关问题,怎么在小弟我的数据表中查询小弟我需要的报表
请教大家一个问题,如何在我的数据表中查询我需要的报表?
我先叙述一下我的数据表的结构,如图所示。

以上是我数据表的结构。字段的含义如下:production是每台称所测得的产量值,chenid就是每一台称的id,分为“1号称,2号称,3号称等”,称的数量是系统设定的,一般有7台称。dtime是写入数据的时间,系统设定,每一小时向系统里写一次产量数据。我现在想通过一条sql语句,返回这样的一张报表:

假设这张年报表是2001年的。
这样的sql语句该怎么写呢?或者说,我的想法能实现吗?
------解决方案--------------------
主要取数部分,总量可用 Excel 自己的公式。
------解决方案--------------------
'1号称' 要用方括号 [1号称]
... 要用具体的值替换
最后 T 前面少个 )
(我手头的 SQL Server 版本太低,不能用 PIVOT,所以是纯手写,见谅。)
------解决方案--------------------
(子查询中少了 GROUP BY
)
------解决方案--------------------
select
case
when datepart(month,dtime)=1 then '1月'
when datepart(month,dtime)=2 then '2月'
when datepart(month,dtime)=3 then '3月'
when datepart(month,dtime)=4 then '4月'
when datepart(month,dtime)=5 then '5月'
when datepart(month,dtime)=6 then '6月'
when datepart(month,dtime)=7 then '7月'
when datepart(month,dtime)=8 then '8月'
when datepart(month,dtime)=9 then '9月'
when datepart(month,dtime)=10 then '10月'
when datepart(month,dtime)=11 then '11月'
else '12月' end as '月份' ,
sum(
case
when chenid='1号称' then cast(prodution as float) else 0 end
) as '1号称' ,
sum(
case
when chenid='2号称' then cast(prodution as float) else 0 end
) as '2号称' ,
sum(
case
when chenid='3号称' then cast(prodution as float) else 0 end
) as '3号称'
from tablechen group by datepart(month,dtime)
没有总量!
------解决方案--------------------
>首先,结果中,前3行分别是10月,11月和12月,为什么不能从1月开始排序呢?
因为字符串 '10月' < '1月',要排序就用 DATEPART(month, dtime) 数值排序。
>其次,在PIVOT字句中,in函数的内容可以写成一个select查询语句吗?
不能。可以改用用动态 SQL 拼接。
我先叙述一下我的数据表的结构,如图所示。
以上是我数据表的结构。字段的含义如下:production是每台称所测得的产量值,chenid就是每一台称的id,分为“1号称,2号称,3号称等”,称的数量是系统设定的,一般有7台称。dtime是写入数据的时间,系统设定,每一小时向系统里写一次产量数据。我现在想通过一条sql语句,返回这样的一张报表:
假设这张年报表是2001年的。
这样的sql语句该怎么写呢?或者说,我的想法能实现吗?
------解决方案--------------------
主要取数部分,总量可用 Excel 自己的公式。
SELECT *
FROM (SELECT Convert(varchar(2),DATEPART(month, dtime))+'月' m
chenid,
SUM(production) sump
FROM 数据表
WHERE DATEPART(year, dttime) = 2001
) p
PIVOT (SUM(sump) FOR chenid in ('1号称','2号称',...,'7号称') T
------解决方案--------------------
'1号称' 要用方括号 [1号称]
... 要用具体的值替换
最后 T 前面少个 )
(我手头的 SQL Server 版本太低,不能用 PIVOT,所以是纯手写,见谅。)
------解决方案--------------------
(子查询中少了 GROUP BY
SELECT *
FROM (SELECT Convert(varchar(2),DATEPART(month, dtime))+'月' m
chenid,
SUM(production) sump
FROM 数据表
WHERE DATEPART(year, dttime) = 2001
GROUP BY Convert(varchar(2),DATEPART(month, dtime))+'月',
chenid
) p
PIVOT ...
------解决方案--------------------
select
case
when datepart(month,dtime)=1 then '1月'
when datepart(month,dtime)=2 then '2月'
when datepart(month,dtime)=3 then '3月'
when datepart(month,dtime)=4 then '4月'
when datepart(month,dtime)=5 then '5月'
when datepart(month,dtime)=6 then '6月'
when datepart(month,dtime)=7 then '7月'
when datepart(month,dtime)=8 then '8月'
when datepart(month,dtime)=9 then '9月'
when datepart(month,dtime)=10 then '10月'
when datepart(month,dtime)=11 then '11月'
else '12月' end as '月份' ,
sum(
case
when chenid='1号称' then cast(prodution as float) else 0 end
) as '1号称' ,
sum(
case
when chenid='2号称' then cast(prodution as float) else 0 end
) as '2号称' ,
sum(
case
when chenid='3号称' then cast(prodution as float) else 0 end
) as '3号称'
from tablechen group by datepart(month,dtime)
没有总量!
------解决方案--------------------
>首先,结果中,前3行分别是10月,11月和12月,为什么不能从1月开始排序呢?
因为字符串 '10月' < '1月',要排序就用 DATEPART(month, dtime) 数值排序。
>其次,在PIVOT字句中,in函数的内容可以写成一个select查询语句吗?
不能。可以改用用动态 SQL 拼接。