在枢轴聚合函数中使用CONCAT函数的SQL Server错误

时间:2018-11-19 11:18:18

标签: sql sql-server sql-server-2012

我有一个输出枢轴的SQL查询。

我想做的是向CONCAT添加一个pivot函数,但是出现以下错误:

  

'CONCAT'不是公认的聚合函数。

我当前的代码(会给出错误):

SELECT * FROM (
SELECT dateadd(minute,(datediff(minute,0,Time)/15)*15,0) Time,  AREA, COUNT(BLOB) BLOBs, CARS
FROM 
(SELECT BLOB, Time, CARS, DRIVER, [MK], AREA, Tier FROM (
SELECT [BLOB Nbr] BLOB, Time, CARS, [From PT], [To PT], [From PN], [To PN], DRIVER, [MK],
CASE AREA
WHEN '01' THEN '02' WHEN '03' THEN '02'
WHEN '05' THEN '06' WHEN '07' THEN '06'
WHEN '09' THEN '10' WHEN '11' THEN '10'
ELSE AREA END AREA, 
Position, Tier
FROM (
SELECT [BLOB Nbr], T_IME Time, [C NAME] CARS, [From PT], [To PT], [From PN], [To PN], [C V] DRIVER, [MK],
CASE WHEN [From PT] = 'DRIVER' THEN LEFT(RIGHT([From PN],6),2) ELSE LEFT(RIGHT([To PN],6),2) END AREA,
CASE WHEN [From PT] = 'DRIVER' THEN LEFT(RIGHT([From PN],4),2) ELSE LEFT(RIGHT([To PN],4),2) END Position,
CASE WHEN [From PT] = 'DRIVER' THEN RIGHT([From PN],2) ELSE RIGHT([To PN],2) END Tier
FROM mytable c
WHERE ([From PT] = 'DRIVER' OR [To PT] = 'DRIVER')
) a ) b ) c
GROUP BY dateadd(minute,(datediff(minute,0,Time)/15)*15,0),  AREA, CARS) src pivot(CONCAT(sum(BLOBs),CARs) for AREA in ([02],[06],[10])) piv;

如果我将第二行更改为(删除CARs):

SELECT dateadd(minute,(datediff(minute,0,Time)/15)*15,0) Time,  AREA, COUNT(BLOB) BLOBs

如果我将GROUP BY更改为(删除CARsCONCAT函数):

GROUP BY dateadd(minute,(datediff(minute,0,Time)/15)*15,0),  AREA) src pivot(sum(BLOBs) for AREA in ([02],[06],[10])) piv;

它可以工作,但是我希望在结果中进行串联。

当前工作输出:

Time                      02    06      10
2018-05-07 16:00:00.000   11    NULL    NULL
2018-05-07 16:15:00.000   2     7       NULL
2018-05-07 16:30:00.000   NULL  8       NULL
2018-05-07 16:45:00.000   9     NULL    NULL
2018-05-07 17:00:00.000   9     NULL    8

预期的输出(使用CONCAT):

Time                      02        06      10
2018-05-07 16:00:00.000   BMW11     NULL    NULL
2018-05-07 16:15:00.000   BMW2      KIA7    NULL
2018-05-07 16:30:00.000   NULL      KIA8    NULL
2018-05-07 16:45:00.000   BMW9      NULL    NULL
2018-05-07 17:00:00.000   BMW9      NULL    FIAT8

从查询中抽样数据,不包含PIVOT

Time                        AREA    BLOBs   CARs
2018-05-07 16:00:00.000     02      11      BMW
2018-05-07 16:15:00.000     02      2       BMW
2018-05-07 16:15:00.000     06      7       KIA
2018-05-07 16:30:00.000     06      8       KIA
2018-05-07 16:45:00.000     02      9       BMW
2018-05-07 17:00:00.000     02      9       BMW
2018-05-07 17:00:00.000     10      8       FIAT

表中的原始样本数据:

2 个答案:

答案 0 :(得分:1)

您可以在下面尝试-您需要在数据透视表中使用聚合函数,因此您可以使用max(CONCAT(sum(BLOBs),CARs))

    SELECT * FROM (
SELECT dateadd(minute,(datediff(minute,0,Time)/15)*15,0) Time,  AREA, COUNT(BLOB) BLOBs, CARS
FROM 
(SELECT BLOB, Time, CARS, DRIVER, [MK], AREA, Tier FROM (
SELECT [BLOB Nbr] BLOB, Time, CARS, [From PT], [To PT], [From PN], [To PN], DRIVER, [MK],
CASE AREA
WHEN '01' THEN '02' WHEN '03' THEN '02'
WHEN '05' THEN '06' WHEN '07' THEN '06'
WHEN '09' THEN '10' WHEN '11' THEN '10'
ELSE AREA END AREA, 
Position, Tier
FROM (
SELECT [BLOB Nbr], T_IME Time, [C NAME] CARS, [From PT], [To PT], [From PN], [To PN], [C V] DRIVER, [MK],
CASE WHEN [From PT] = 'DRIVER' THEN LEFT(RIGHT([From PN],6),2) ELSE LEFT(RIGHT([To PN],6),2) END AREA,
CASE WHEN [From PT] = 'DRIVER' THEN LEFT(RIGHT([From PN],4),2) ELSE LEFT(RIGHT([To PN],4),2) END Position,
CASE WHEN [From PT] = 'DRIVER' THEN RIGHT([From PN],2) ELSE RIGHT([To PN],2) END Tier
FROM mytable c
WHERE ([From PT] = 'DRIVER' OR [To PT] = 'DRIVER')
) a ) b ) c
GROUP BY dateadd(minute,(datediff(minute,0,Time)/15)*15,0),  AREA, CARS) src pivot(max(CONCAT(sum(BLOBs),CARs)) for AREA in ([02],[06],[10])) piv;

答案 1 :(得分:1)

尝试一下

CREATE TABLE #PIVOT (Time  DATETIME, AREA INT, BLOBs INT, CARs VARChar (10))

insert into #PIVOT values 
 ('2018-05-07 16:00:00.000', 02, 11     ,'BMW')
,('2018-05-07 16:15:00.000', 02, 2      ,'BMW')
,('2018-05-07 16:15:00.000', 06, 7      ,'KIA')
,('2018-05-07 16:30:00.000', 06, 8      ,'KIA')
,('2018-05-07 16:45:00.000', 02, 9      ,'BMW')
,('2018-05-07 17:00:00.000', 02, 9      ,'BMW')
,('2018-05-07 17:00:00.000', 10, 8      ,'FIA')


select
 Time, CARs + convert (varchar (10), [02]) [02]
 , CARs + convert (varchar (10), [06]) [06], CARs + convert (varchar (10), [10]) [10]
from (
 select
 *
 from #PIVOT
) p
pivot
(
max (BLOBs) for area in ([02],[06], [10])
) t

Time                        02      06      10
2018-05-07 16:00:00.000     BMW11   NULL    NULL
2018-05-07 16:15:00.000     BMW2    NULL    NULL
2018-05-07 16:45:00.000     BMW9    NULL    NULL
2018-05-07 17:00:00.000     BMW9    NULL    NULL
2018-05-07 17:00:00.000     NULL    NULL    FIA8
2018-05-07 16:15:00.000     NULL    KIA7    NULL
2018-05-07 16:30:00.000     NULL    KIA8    NULL

让我为动态查询工作。