我遇到以下问题:我有一张包含员工和电子邮件的表格。
现在我需要员工以逗号分隔的格式在一列中显示电子邮件的最长日期,最短日期和所有收到的邮件。
我有以下内容:
SELECT ...
FROM
(SELECT
MAX(Datum) as MDatum, MIN(Datum) as MinDatum,
Sender, Betreff,
CASE
WHEN MIN(Datum) = MAX(Datum)
THEN MIN(Datum)
ELSE dbo.explode(datum) <- my problem
END AS VALUE,
Nummer
FROM
#NEWTABLE
WHERE
not nummer IS NULL
AND Sender <> ''total''
GROUP BY
Sender, Betreff, Nummer, datum <- second problem
) x
PIVOT
(max(VALUE)
FOR [Nummer] IN (' + @cols2 + ')
) p
@cols2
是充满活力的员工
当我按基准分组时,我只获得数据中的最大值(值)
我怎么能解决这个问题?
最诚挚的问候,坦率地说增加:
这是我的表(#NEWTABLE):enter image description here
Nummer | Betreff | Datum |
---- | ---- | ---- |
1 2.000 EUR Rente 06.03.2017
1 2.000 EUR Rente NULL
1 2.000 Rente 04.03.2017
1 2.000 Rente 12.02.2017
1 2.000 Rente 12.03.2017
1 2.000 Rente 19.02.2017
1 2.000 Rente 19.03.2017
1 2.000 Rente 27.02.2017
1 2.000 Rente NULL
1 2.000 Rente 27.02.2017
1 2.000 Rente NULL
2 2.000 EUR Rente 06.03.2017
2 2.000 EUR Rente NULL
2 2.000 Rente 04.03.2017
2 2.000 Rente 12.02.2017
2 2.000 Rente 12.03.2017
2 2.000 Rente 19.02.2017
2 2.000 Rente 19.03.2017
2 2.000 Rente 27.02.2017
2 2.000 Rente NULL
2 2.000 Rente 27.02.2017
2 2.000 Rente NULL
所以你有nummer,即employee,betreff和datum,收到的日期
预期输出应为:enter image description here
nummer mindate maxdate alldates
1 12.02.2017 19.03.2017 12.02.2017,
19.02.2017,
27.02.2017,
27.02.2017,
04.03.2017,
06.03.2017,
12.03.2017,
19.03.2017
2 12.02.2017 19.03.2017 12.02.2017,
19.02.2017,
27.02.2017,
27.02.2017,
04.03.2017,
06.03.2017,
12.03.2017,
19.03.2017
最诚挚的问候,坦率地说
答案 0 :(得分:2)
以下是FOR XML PATH('')
的解决方案:
SELECT Nummer,min(Datum) miDat, max(Datum) maDat,
STUFF( (SELECT ','+char(10) + CONVERT(char(10),Datum,104) FROM #tmp
WHERE NOT Datum is null AND Nummer=o.Nummer
ORDER BY Datum
FOR XML PATH('')), 1, 2, '' ) as dates
FROM #tmp o GROUP BY Nummer
STUFF()
函数只删除前两个字符。
可在此处找到工作演示: http://rextester.com/PUG64308
(我稍微改变了样本数据,以便得到两个数字略有不同的结果。)
修改:您可以通过以下方式修改查询来轻松实现更改后的结果:
SELECT min(Datum) miDat, max(Datum) maDat,
STUFF( (SELECT ','+char(10) + CONVERT(char(10),Datum,104) FROM #tmp
WHERE NOT Datum is null AND Nummer=1
ORDER BY Datum
FOR XML PATH('')), 1, 2, '' ) as dates1,
STUFF( (SELECT ','+char(10) + CONVERT(char(10),Datum,104) FROM #tmp
WHERE NOT Datum is null AND Nummer=2
ORDER BY Datum
FOR XML PATH('')), 1, 2, '' ) as dates2
FROM #tmp
虽然在我看来,“Sender”和Betreff这两个专栏“在这样的结果场景中不再有意义。”
可在此处找到一个工作示例:http://rextester.com/EATE35782
答案 1 :(得分:2)
希望这有帮助
;WITH cte_Table (Nummer,Betreff,Datum) AS
(
SELECT 1,'2.000 EUR Rente',CAST('2017-03-07' AS DATE) UNION ALL
SELECT 1,'2.000 EUR Rente',NULL UNION ALL
SELECT 1,'2.000 Rente','2017-03-04' UNION ALL
SELECT 1,'2.000 Rente','2017-02-12' UNION ALL
SELECT 1,'2.000 Rente','2017-03-12' UNION ALL
SELECT 1,'2.000 Rente','2017-02-19' UNION ALL
SELECT 1,'2.000 Rente','2017-03-19' UNION ALL
SELECT 1,'2.000 Rente','2017-02-27' UNION ALL
SELECT 1,'2.000 Rente',NULL UNION ALL
SELECT 1,'2.000 Rente','2017-02-27' UNION ALL
SELECT 1,'2.000 Rente',NULL UNION ALL
SELECT 2,'2.000 EUR Rente','2017-03-06' UNION ALL
SELECT 2,'2.000 EUR Rente',NULL UNION ALL
SELECT 2,'2.000 Rente','2017-03-04' UNION ALL
SELECT 2,'2.000 Rente','2017-02-12' UNION ALL
SELECT 2,'2.000 Rente','2017-03-12' UNION ALL
SELECT 2,'2.000 Rente','2017-02-19' UNION ALL
SELECT 2,'2.000 Rente','2017-03-19' UNION ALL
SELECT 2,'2.000 Rente','2017-02-27' UNION ALL
SELECT 2,'2.000 Rente',NULL UNION ALL
SELECT 2,'2.000 Rente','2017-02-27' UNION ALL
SELECT 2,'2.000 Rente',NULL
)
,cte_MinMax(nummer,mindate,maxdate) AS
(
SELECT Nummer,MIN(Datum),MAX(Datum)
FROM cte_Table
GROUP BY Nummer
)
,cte_CSV AS
(
SELECT DISTINCT a.Nummer, STUFF(( SELECT ', ' + CONVERT(VARCHAR(10),b.Datum,104) AS [text()]
FROM cte_Table b
WHERE a.Nummer = b.Nummer
FOR XML PATH('')), 1, 1, '' ) AS alldates
FROM cte_Table a
)
SELECT b.nummer,b.mindate,b.maxdate,a.alldates
FROM cte_CSV a
JOIN cte_MinMax b
ON a.Nummer = b.nummer
答案 2 :(得分:1)
它就像一个魅力...... 但我犯了一个错误,对不起,预期的输出应该是这样的(带有支点) enter image description here
sender | betreff | mindate | maxdate | 1 | 2
blubb | blah | 12.02.2017 | 19.03.2017 | 12.02.2017, | 12.02.2017,
19.02.2017, 19.02.2017,
27.02.2017, 27.02.2017,
27.02.2017, 27.02.2017,
04.03.2017, 04.03.2017,
06.03.2017, 06.03.2017,
12.03.2017, 12.03.2017,
19.03.2017 19.03.2017
最诚挚的问候,坦率地说
答案 3 :(得分:0)
我明白了:
SELECT ...
FROM
(SELECT sender,Nummer,min(Datum) MinDatum, max(Datum) MDatum,betreff,
STUFF( (SELECT '',''+char(10) + CONVERT(char(10),Datum,104) FROM #NEWTABLE
WHERE NOT Datum is null AND Nummer=o.Nummer and sender = o.sender and betreff = o.betreff
group by Datum,sender,Nummer,betreff
ORDER BY Datum
FOR XML PATH('''')), 1, 2, '''' ) as VALUE
FROM #NEWTABLE o where sender <> ''total'' GROUP BY sender,Nummer,betreff
) x
PIVOT
(max(VALUE)
FOR [Nummer] IN (' + @cols2 + ')
) p