2014-11-24 36 views
0
计数使用组多个字段由

我有以下表结构也有我提到我预期的输出,请帮我查询,因为我不知道很多关于SQL查询获得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 

但问题是,我想这上面的表格枢纽即

的预期输出

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 
+0

1)该如何与C#? 2)你没有发布源表结构,只是几个查询和它们的输出。 – Dennis 2014-11-24 06:55:53

+0

@Dennis源表是我第一次查询 – Nilesh 2014-11-24 06:57:55

回答

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) 
+0

谢谢你的回答......我得到使用此查询正确的输出,但实际上我想这个支点请检查更新的问题 – Nilesh 2014-11-24 09:23:21

+0

请张贴,作为一个新的问题。 – 2014-11-24 15:39:17

+0

请检查我的新问题http://stackoverflow.com/questions/27109077/using-pivot-in-sql-query – Nilesh 2014-11-24 16:07:16

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) 

上面的查询给你加入日期计数同样的事情你可以做的终止日期数