在SQL Server中进行透视和分组

时间:2016-09-08 16:47:58

标签: sql-server sql-server-2008 pivot

我想做一个数据透视表来显示每个类别和每个客户的销售情况 而且类别表是这样的:

groupBy %>% agg

当我使用类别表的所有元素进行简单的透视时,一切正常,但是该表有近600个不同的类别,因此结果不是很易读,现在我想要的是按2位数字分组所以结果将是这样的:ILUMINACION,MATERIAL ELECTRICO,MUEBLES,ELECTRODOMESTICOS ......,这样做会减少类别,使得枢轴更易读,我尝试在from子句中添加一个组,但是给我一个错误说“子查询返回了多个值”。

这是我的代码:

--- ---- EDIT 我试图尽可能地简化

首先是选择,带有枢轴的总和(这不是问题)

code     |  name
-----------------------
100000   | ILUMINACION
980101   | MUEBLES ACCESORIOS DECORATIVO
980102   | MUEBLES ACCESORIOS INTERIOR
980103   | MUEBLES ACCESORIOS ZOCALO

这是问题的核心

select cliente,
(
select CASE WHEN cliente!='' then max(c.nombre) else 'CONTADO' end 
from CLIENTES c where c.nif=cliente
),
sum([APARELLAJE MEDIA TENSION_2015]) as [APARELLAJE MEDIA TENSION_2015],
sum([APARELLAJE MEDIA TENSION_2016]) as [APARELLAJE MEDIA TENSION_2016], 
sum([AUTOMATIZACION_2015]) as [AUTOMATIZACION_2015],
sum[AUTOMATIZACION_2016]) as [AUTOMATIZACION_2016],
sum([CANALIZACION_2015]) as [CANALIZACION_2015], 
sum([CANALIZACION_2016]) as [CANALIZACION_2016],
sum([COMPUESTOS_2015]) as [COMPUESTOS_2015], 
sum([COMPUESTOS_2016]) as [COMPUESTOS_2016],
sum([CONDUCTOR_2015]) as [CONDUCTOR_2015], 
sum([CONDUCTOR_2016]) as [CONDUCTOR_2016],
sum([CONEXION_2015]) as [CONEXION_2015], 
sum([CONEXION_2016]) as [CONEXION_2016],
sum([ELECTRODOMESTICOS_2015]) as [ELECTRODOMESTICOS_2015],
sum([ELECTRODOMESTICOS_2016]) as [ELECTRODOMESTICOS_2016],
sum([ENVOLVENTES_2015]) as [ENVOLVENTES_2015],
sum([ENVOLVENTES_2016]) as [ENVOLVENTES_2016],
sum([HERRAMIENTAS_2015]) as [HERRAMIENTAS_2015], 
sum([HERRAMIENTAS_2016]) as [HERRAMIENTAS_2016],
sum([ILUMINACION_2015]) as [ILUMINACION_2015], 
sum([ILUMINACION_2016]) as [ILUMINACION_2016],
sum([INSTRUMENTACION_2015]) as [INSTRUMENTACION_2015], 
sum([INSTRUMENTACION_2016]) as [INSTRUMENTACION_2016],
sum([MATERIAL DE INSTALACION_2015]) as [MATERIAL DE INSTALACION_2015],
sum([MATERIAL DE INSTALACION_2016]) as [MATERIAL DE INSTALACION_2016],
sum([PEQUEÑO MATERIAL_2015]) as [PEQUEÑO MATERIAL_2015], 
sum([PEQUEÑO MATERIAL_2016]) as [PEQUEÑO MATERIAL_2016],
sum([PROTECCION MANDO Y MEDIDA_2015]) as [PROTECCION MANDO Y MEDIDA_2015],
sum([PROTECCION MANDO Y MEDIDA_2016]) as [PROTECCION MANDO Y MEDIDA_2016],
sum([VARIOS_2015]) as [VARIOS_2015], sum([VARIOS_2016]) as [VARIOS_2016]

PIVOT的一部分(我觉得这里没有任何问题)

from
(
  select c.nif cliente, lac.ImporteLimpio suma,
  (
    SELECT DISTINCT
     (
      select case when ca.Codigo like '97%' then 'HERRAMIENTAS'
      else ca.Nombre end from CodigosEstadisticos_ARTICULOS ca
      where ca.Codigo=substring(cea.Codigo,1,2)+'0000'
     )
  +'_'+cast(YEAR(acc.fecalb)as varchar(100))
  from ALBARAN_CLIENTE acc,CodigosEstadisticos_ARTICULOS cca
  group by substring(cca.Codigo,1,2),acc.fecalb
  ) AS SalesRegion

from ALBARAN_CLIENTE ac join LINEAS_ALBARAN_CLIENTE lac on
c.codalb=lac.codalb join ARTICULOS a on a.codart=lac.codart join 
CodigosEstadisticos_ARTICULOS cea on cea.Codigo=a.codigoEstadistico
join CLIENTES c on c.codcli=ac.codcli 
where month(ac.fecalb)<month(getdate()) and(year(ac.fecalb)=year(getdate())
or year(ac.fecalb)=year(getdate())-1

)as p

0 个答案:

没有答案