我的查询返回一个看起来像这样的表(请注意,我发布了一个非常简化的输出版本):
+-------+------+------+------+------+
| | 1 | 2 | 3 | 4 |
+-------+------+------+------+------+
| 14234 | 3 | null | null | null |
| 14234 | null | null | 32 | null |
| 33334 | 4 | null | null | 5 |
| 33334 | null | 3 | null | null |
+-------+------+------+------+------+
我想重新格式化它看起来像这样:
+-------+---+------+------+------+
| | 1 | 2 | 3 | 4 |
+-------+---+------+------+------+
| 14234 | 3 | null | 32 | null |
| 33334 | 4 | 3 | null | 5 |
+-------+---+------+------+------+
正如您所看到的,这些行已经放在一起(压缩),我不知道正确的术语是什么。
有没有一种快速的方法可以用 sql server 做到这一点?
这是原始查询:
;WITH
PivotQuery as (
select client_id,
[1],[2],[3],[4],[5],[6],[7]
from
( SELECT DISTINCT CLIENT_ID
, PATIENT_ID
, count(*) over (partition by client_id, patient_id) AS patientcount
from f_accession_daily) as SourceTable
PIVOT
(
count(patient_id)
for patientcount in ([1],[2],[3],[4],[5],[6],[7])
) as pivottable),
MinMaxTimes as (
SELECT a.client_id AS client_id
,a.patientcount TimesTested
, count(a.patientcount)/a.patientcount AS count
, max(f.received_date) AS maxRecDate
, min(f.received_date) AS minRecDate
FROM
(
SELECT DISTINCT CLIENT_ID
, PATIENT_ID
, count(*) over (partition by client_id, patient_id) AS patientcount
from f_accession_daily
) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
AND a.PATIENT_ID = f.PATIENT_ID
GROUP BY a.CLIENT_ID, a.patientcount),
maxDates as (
SELECT client_id, [1] maxdate1, [2] maxdate2, [3] maxdate3, [4] maxdate4, [5] maxdate5, [6] maxdate6, [7] maxdate7
FROM MinMaxTimes t
PIVOT (max(maxRecDate)
for TimesTested IN ([1], [2], [3], [4], [5], [6], [7])
) as p),
minDates as (
SELECT client_id, [1] mindate1, [2] mindate2, [3] mindate3, [4] mindate4, [5] mindate5, [6] mindate6, [7] mindate7
FROM MinMaxTimes t
PIVOT (max(minRecDate)
for TimesTested IN ([1], [2], [3], [4], [5], [6], [7])
) as p)
SELECT i.client_id,
p.[1], maxdate1, mindate1,
p.[2], maxdate2, mindate2,
p.[3], maxdate3, mindate3,
p.[4], maxdate4, mindate4,
p.[5], maxdate5, mindate5,
p.[6], maxdate6, mindate6,
p.[7], maxdate7, mindate7
FROM PivotQuery p
LEFT OUTER JOIN maxDates a ON p.client_id = a.client_id
LEFT OUTER JOIN mindates i ON a.client_id = i.client_id