我为 Microsoft SQL Server 2008 R2 编写了以下查询...
with
downloads as
(
select convert(varchar(10), timestamp, 112) as downloadDate, COUNT(*) as counter
from <download_table>
group by convert(varchar(10), timestamp,112)
),
uploads as
(
select CONVERT(varchar(10), dateadded, 112) as uploadDate, COUNT(*) as counter
from <upload_table>
group by CONVERT(varchar(10), dateadded, 112)
)
select
downloads.downloadDate,
uploads.uploadDate,
downloads.counter as dCount,
uploads.counter as uCount
from downloads
full join uploads on uploads.uploadDate = downloads.downloadDate
order by downloadDate desc;
它返回下表...
downloadDate uploadDate dCount uCount
20121211 NULL 40 NULL
20121210 NULL 238 NULL
20121207 20121207 526 4
20121206 20121206 217 12
20121205 NULL 108 NULL
20121204 20121204 190 13
20121203 NULL 141 NULL
20121130 20121130 248 187
20121129 NULL 134 NULL
20121128 NULL 102 NULL
20121127 20121127 494 57
20121126 NULL 153 NULL
20121119 20121119 319 20
20121118 NULL 4 NULL
20121116 20121116 215 16
20121112 20121112 431 144
20121109 20121109 168 48
20121108 20121108 132 181
NULL 20121125 NULL 3
但是如果没有一些 NULL 条目,我无法将两个日期组合成一个“日期”列,也无法让 dCount 或 uCount 中的 NULL 值显示 0 而不是 NULL。
有人可以帮我吗?