SQL Server结果并排

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

标签: sql-server

我有三个表,我想加入或并排放置在sql server中,但是我做不到

select download_date, count(download_date) as CDC 
from cdc 
group by Download_Date

select COUNT(download_date) as cdcVillage 
from Sec0_cdcvillage 
group by Download_Date

select COUNT(download_date) as Sec1_Participants 
from Sec1_Participants 
group by Download_Date

当我使用join时,所有值都变为相同;还有其他办法吗?

enter image description here

我想要这样的结果

1 个答案:

答案 0 :(得分:1)

与CTE一起尝试。我认为download_date存在于所有表中:

;with cdc as
    (select download_date, count(download_date) as CDC from cdc group by Download_Date),
cdcVillage as
    (select COUNT(download_date) as cdcVillage from Sec0_cdcvillage group by Download_Date),
Sec1_Participants as
    (select COUNT(download_date) as Sec1_Participants from Sec1_Participants group by Download_Date)
select cdc.download_date, cdc.cdc, cdcVillage.cdcVillage, Sec1_Participants.Sec1_Participants
from cdc
    inner join cdcVillage on cdc.download_date = cdcVillage.download_date
    inner join Sec1_Participants on cdc.download_date = Sec1_Participants.download_date

编辑

不确定为什么(可能是我的别名cdc匹配表cdc),请尝试以下操作:

;with tcdc as
    (select download_date, count(download_date) as CDC from cdc group by Download_Date),
tcdcVillage as
    (select download_date, COUNT(download_date) as cdcVillage from Sec0_cdcvillage group by Download_Date),
tSec1_Participants as
    (select download_date, COUNT(download_date) as Sec1_Participants from Sec1_Participants group by Download_Date)
select tcdc.download_date,
       tcdc.cdc,
       tcdcVillage.cdcVillage,
       tSec1_Participants.Sec1_Participants
from tcdc
    inner join tcdcVillage on tcdc.download_date = tcdcVillage.download_date
    inner join tSec1_Participants on tcdc.download_date = tSec1_Participants.download_date;