我每个月都要尝试做一个销售支点,每隔30分钟就有一个问题,这是我的结果:
正如您所看到的那样,这是不正确的,因为它会在每个月重复全年的销售。
我将我的代码放在下面,hormod是一个时间类型字段,而fecalb是一个日期类型字段,
select concat(DATEPART(HOUR, hormod),
case when(DATEPART(MINUTE, hormod) / 30)=0 then ':00' else ':30'end) Hour,
count([January])[Jan],count([February])[Feb],
count([March])[Mar],count([April])[Apr]
from
(
select hormod, fecalb,codalb, datename(month,fecalb) mes
from balbara WHERE year(fecalb)=2017) as z
pivot(count(codalb) for mes in ([January],[February],[March],[April])
)as pivotable
group by DATEPART(HOUR, hormod),(DATEPART(MINUTE,hormod) / 30)
在这里,我没有每个月都放置它们以使它们更具可读性
EDIT 我使用SQL Fiddle和数据:http://sqlfiddle.com/#!18/66430/1
答案 0 :(得分:1)
您应该如下查询,我已经比较了条件,如果它没有问题,那么你可以根据你的要求改变逻辑:
SELECT
CONCAT(hormodh, ':', CASE WHEN hormodm >= 30 THEN '30' ELSE '00' END) Hour,
SUM([January])[Jan],
SUM([February])[Feb],
SUM([March])[Mar],
SUM([April])[Apr]
FROM
(
SELECT
DATEPART(HOUR, hormod) AS hormodh,
DATEPART(MINUTE, hormod) AS hormodm,
--fecalb, not required
codalb,
DATENAME(MONTH,fecalb) mes
FROM balbara
WHERE YEAR(fecalb)=2017) AS z
PIVOT(COUNT(codalb) FOR mes IN ([January],[February],[March],[April])
)AS pivotable
GROUP BY CONCAT(hormodh, ':', CASE WHEN hormodm >= 30 THEN '30' ELSE '00' END)