使用SQL从多个表中获取Count(*)

时间:2016-12-27 08:27:15

标签: sql sql-server

我有两张表Registration_TableApplicationStatus_Table。我仅从Registration_Table获取了一列用于理解目的。在ApplicationStatus_Table根据Registrationdate和FinalSubmissiondate的日期检查状态。

Registration_Table

 Registrationdate
 -----------------------
 2016-12-22 12:52:48.493
 2016-12-21 15:05:24.533

ApplicationStatus_Table

FinalSubmissiondate              InnovationStatus
-------------------------------------------------
2016-12-22 12:52:48.493          Completed
2016-12-21 15:05:24.533           11

我需要如下结果。

 Registrationdate    RegistrationsCount   InnovationsStatus
 ----------------------------------------------------------
 2016-12-22               1                1
 2016-12-21               1                0

我需要特定日期的结果有多少注册和InnovationsStatus。如果InnovationStatus完成意味着1,其他则全部为0。

3 个答案:

答案 0 :(得分:2)

考虑具有完整计数和条件计数的聚合查询。下面还从日期时间值中提取时间部分,条件聚合中返回零:

SELECT CONVERT(date, r.Registrationdate) As [Date], Count(*) As RegistrationsCount,
       SUM(CASE WHEN a.Status = 'Completed' THEN 1 ELSE 0 END) As InnovationsStatus
FROM Registration_Table r
INNER JOIN ApplicationStatus_Table a
ON r.Registrationdate = a.FinalSubmissiondate
GROUP BY CONVERT(date, r.Registrationdate)

答案 1 :(得分:0)

您可以执行以下操作:

SELECT  CAST(r.Registrationdate AS DATE) AS as Registrationdate,
        COUNT(Registrationdate) OVER (PARTITION BY Registrationdate) AS RegistrationsCount,
        COUNT(CASE WHEN  a.InnovationStatus='Completed' THEN 1 ELSE NULL END) as InnovationsStatus
FROM Registration_Table   r
INNER JOIN ApplicationStatus_Table  a
ON a.FinalSubmissiondate=r.Registrationdate
GROUP BY r.Registrationdate
ORDER BY r.Registrationdate DESC

测试数据:

create table #Registration_Table (
 Registrationdate datetime
)

insert into #Registration_Table
values('2016-12-22 12:52:48.493'),('2016-12-21 15:05:24.533')

create table #ApplicationStatus_Table(
 FinalSubmissiondate datetime,
 InnovationStatus varchar(20)
) 

insert into #ApplicationStatus_Table
values ('2016-12-22 12:52:48.493','Completed'),('2016-12-21 15:05:24.533','11')



SELECT  CAST(r.Registrationdate AS DATE),
        COUNT(Registrationdate) OVER (PARTITION BY Registrationdate) AS RegistrationsCount,
        COUNT(CASE WHEN  a.InnovationStatus='Completed' THEN 1 ELSE NULL END) as InnovationsStatus
FROM #Registration_Table   r
INNER JOIN #ApplicationStatus_Table  a
ON a.FinalSubmissiondate=r.Registrationdate
GROUP BY r.Registrationdate
ORDER BY r.Registrationdate DESC

<强>结果:

  Registrationdate       RegistrationsCount InnovationsStatus
     2016-12-22                 1                1
     2016-12-21                 1                0

答案 2 :(得分:0)

         *****Using this query*****

 select t1.[Registrationdate],
 COUNT(t1.[Registrationdate]) as [RegistrationsCount],
 max(case when t2.[InnovationStatus]='completed' then 1 else 0 end) as [InnovationStatus]
 from [#yourtable1] t1
 Join [#youttable2] t2 on
 t1.[Registrationdate]=t2.[FinalSubmissiondate]
 Group by t1.[Registrationdate] Order by t1.[Registraiondate] Desc;