在SQL中使用group by获取多个字段的计数

时间:2014-11-24 06:45:12

标签: sql sql-server sql-server-2008

我有以下表结构也提到我的预期输出请帮我查询,因为我对SQL查询不太了解

查询:

SELECT fname, 
       lname, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esilocation)   AS ESILocation, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esidispensary) AS ESIDispensary, 
       dateofjoining, 
       terminationdate 
FROM   dbo.employeedetail 

输出:

FName     LName    ESILocation   ESIDispensary  DateOfJoining   TerminationDate
Pratik    Sawant   pune          mumbai         2014-06-08        2014-08-01 
Nilesh    Gajare   pune          pune           2014-09-12        2014-11-19 
Praveen   SONi     mumbai        mumbai         2014-08-13        2014-11-13 
Prshant   Sawant   mumbai        mumbai         2014-11-18        NULL
rohit     bhora    pune          pune           2014-09-20        2014-11-20 
sujit     patil    pune          mumbai         2014-10-20        2014-11-20 
Akshay    patil    pune          pune           2015-09-24        NULL

查询2:

SELECT category, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esilocation)   AS ESILocation, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esidispensary) AS ESIDispensary, 
       Month(dateofjoining)        AS month, 
       Year(dateofjoining)         AS year, 
       Count(*)                    AS [Joining Count] 
FROM   dbo.employeedetail 
WHERE  category IN ( 1, 2 ) 
       AND dateofjoining >= '2014-01-01' 
       AND dateofjoining <= '2014-12-31' 
GROUP  BY category, 
          esilocation, 
          esidispensary, 
          Month(dateofjoining), 
          Year(dateofjoining) 

输出:

Category    ESILocation  ESIDispensary  month   year    Joining Count
1            mumbai       mumbai         8       2014     1
1            pune         mumbai         6       2014     1
2            pune         mumbai         10      2014     1
2            pune         pune           9      2014      2

查询3:

SELECT category, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esilocation)   AS ESILocation, 
       (SELECT combovalue 
        FROM   dbo.combovalues 
        WHERE  id = esidispensary) AS ESIDispensary, 
       Month(terminationdate)      AS month, 
       Year(terminationdate)       AS year, 
       Count(*)                    AS [Termination Count] 
FROM   dbo.employeedetail 
WHERE  category IN ( 1, 2 ) 
       AND ( Month(terminationdate) IS NOT NULL 
              OR Month(terminationdate) != '' ) 
       AND ( Year(terminationdate) IS NOT NULL 
              OR Year(terminationdate) != '' ) 
GROUP  BY category, 
          esilocation, 
          esidispensary, 
          Month(terminationdate), 
          Year(terminationdate) 

输出:

Category    ESILocation ESIDispensary   month   year    Termination Count
1            mumbai     mumbai           11      2014      1
1            pune       mumbai           8       2014      1
2            pune       mumbai           11      2014      1
2            pune       pune             11     2014       2

第二次和第三次查询给出终止和加入计数的计数,预期结果应该在单个表中显示两个计数

预期产出

Category    ESILocation ESIDispensary   Joining     Termination    Joining     Termiation  
                                        Count       Count         Count        Count
                                        Jun-2014    jun-2014      Aug-2014     Aug-2014

1           mumbai          mumbai       Null       Null             1            Null
1           pune            mumbai        1         Null            Null            1
2           pune            mumbai       Null       Null            Null          Null
2           pune            pune         Null       Null            Null          Null  

@Update

根据@Markus Jarderot的回答我得到了这个输出

category    esilocation esidispensary   year    month   Joining Count   Termination Count
1                mumbai     mumbai       2014    8          1             0
1                mumbai     mumbai       2014    11         0             1
1                pune       mumbai       2014    6          1             0
1                pune       mumbai       2014    8          1             1
2                pune       mumbai       2014    10         1             0
2                pune       mumbai       2014    11         0             1
2                pune       pune         2014    9          2             0
2                pune       pune         2014    11         0             2

但问题是我想要上表中的Pivot,即

预期产出

 category   esilocation esidispensary   8/2014 join   8/2014 term   11/2014 join   11/2014 term    
   1        mumbai        mumbai             1             0            0             1
   1        pune          mumbai             1             1            null          null
   2        pune          mumbai            null           null         0             1
   2        pune          pune              null           null         0             2

2 个答案:

答案 0 :(得分:1)

select data.category, cl.combovalue as esilocation, cd.combovalue as esidispensary,
    year(date) as year, month(date) as month,
    sum(data.joins) as [Joining Count], sum(data.terms) as [Termination Count]
from (
    select category, esilocation, esidispensary, dateofjoining as date,
           1 as joins, 0 as terms
    from dbo.employeedetail
    where dateofjoining is not null
    union all
    select category, esilocation, esidispensary, terminationdate as date,
           0 as joins, 1 as terms
    from dbo.employeedetail
    where terminationdate is not null
) data
left join dbo.combovalues cl on cl.id = data.esilocation
left join dbo.combovalues cd on cd.id = data.esidispensary
where category in ( 1, 2 ) 
and date >= '2014-01-01' 
and date <= '2014-12-31'
group by data.category, cl.combovalue, cd.combovalue, year(date), month(date)

答案 1 :(得分:0)

   SELECT category, 
   (SELECT combovalue 
    FROM   dbo.combovalues 
    WHERE  id = esilocation)   AS ESILocation, 
   (SELECT combovalue 
    FROM   dbo.combovalues 
    WHERE  id = esidispensary) AS ESIDispensary, 
   Month(dateofjoining)        AS month, 
   Year(dateofjoining)         AS year, 
     convert(varchar,DateName(month , DateAdd( month , Month, 0 ) - 1 ))+'  
     '+convert(varchar,year) as DOJ
  into #temp
  FROM   dbo.employeedetail 
  WHERE  category IN ( 1, 2 ) 
   AND dateofjoining >= '2014-01-01' 
   AND dateofjoining <= '2014-12-31' 
  GROUP  BY category, 
      esilocation, 
      esidispensary, 
      Month(dateofjoining), 
      Year(dateofjoining) 




  DECLARE @cols NVARCHAR(2000)
  SELECT  @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT
                            ‘],[' + t2.ColName
                    FROM    #temp AS t2
                    ORDER BY '],[' + t2.ColName
                    FOR XML PATH('')
                  ), 1, 2, '') + ']‘


  DECLARE @query NVARCHAR(4000)
  SET @query = N’SELECT tID, ’+
  @cols +‘
  FROM
  (SELECT t1.columnname
  FROM    #temp AS t1 Group By DOJ  
    ) p
  PIVOT
  (
  Count(DOJ)    
  FOR ColName IN
  ( ’+
  @cols +‘ )
  ) AS pvt
  execute(@query)

以上查询为您提供连接日期计数您可以执行的终止日期计数