我的查询返回一个看起来像这样的表(请注意我发布了一个非常简化的输出版本):
+-------+------+------+------+------+
| | 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
答案 0 :(得分:4)
我将调用示例数据“fk”
中的第一列SELECT fk,MAX(Col1),MAX(Col2),MAX(Col3),MAX(Col4)
FROM dbo.YourTable
GROUP BY fk
此查询聚合fk列上的行。如果您有多个行具有相同的fk值并且其中一列中的NOT NULL值MAX()将选择最大值。使用SUM()代替计算总和。两者都将忽略NULL值。但是,也会有关于这一事实的警告。