按天分组带有数据透视表的sql

时间:2017-08-25 21:17:37

标签: sql sql-server

我需要创建一个按模型生成每天记录的查询,我目前创建一个生成月度记录的查询:

select
  Modelo,
  case when January Is not null then January else 0 end as Enero,
  case when February Is not null then February else 0 end as Febrero,
  case when March Is not null then March else 0 end as Marzo,
  case when April Is not null then April else 0 end as Abril,
  case when May Is not null then May else 0 end as Mayo,
  case when June Is not null then June else 0 end as Junio,
  case when July Is not null then July else 0 end as Julio,
  case when August Is not null then August else 0 end as Agosto,
  case when September Is not null then September else 0 end   as Setiembre,
  case when October Is not null then October else 0 end   as Octubre,
  case when November Is not null then November else 0 end   as Noviembre,
  case when December Is not null then December else 0 end   as Diciembre
 from
 (
select  v.tyt01_nombre as Modelo,
datename(month,c.tyt15_fecha_cotizacion) as Mes
,COUNT(*) as Total
from  tyt15_cotizaciones c  inner join tyt01_vehiculos v on c.tyt01_id = v.tyt01_id
where c.tyt15_fecha_cotizacion between '2016-01-01 00:00:00' and '2016-12-31 23:59:59'
group by v.tyt01_nombre,
datename(month,tyt15_fecha_cotizacion)
) T
PIVOT (SUM(T.Total) FOR T.Mes IN ([January],[February],[March]
,[April],[May],[June],[July],[August],[September],
[October],[November],[December]))PVT
go

结果是Query Result

现在我需要进行类似的查询,但是按天进行分组,结果是这样的Result I need

非常感谢您的帮助

0 个答案:

没有答案