相同的数据库,相同的表,相同的查询,但不同的where子句

时间:2013-05-07 15:52:38

标签: sql database where

我是一个非常基本的用户,所以对我来说很容易。这是我的问题,我试图得到相同列的计数,但有不同的子句。我希望来自同一个数据库/表的2个查询加入到一个中。我确信这很简单......

1

Select count(inquiry_id) as 'Emails Tickets'
    , owner_rep
From sv_inquiry
Where logged_dt > '5/6/2013 0:00:00'
    and owner_grp in ('it service desk','corporate support')
    and it_source = 'email'
group by owner_rep

2

Select count(inquiry_id) as 'Tele Tickets'
    , owner_rep
From sv_inquiry
Where logged_dt > '5/6/2013 0:00:00'
    and owner_grp in ('it service desk','corporate support')
    and it_source = 'telephone'
Group by owner_rep

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这是一种应该有效的方法。

Select sum(case when it_source='email' THEN 1 ELSE 0 END) emailTicketCount,
       sum(case when it_source='Telephone' THEN 1 ELSE 0 END) TelephoneTicketCount,
       owner_rep 
From sv_inquiry 
Where logged_dt > '5/6/2013 0:00:00' and owner_grp in ('it service desk','corporate support') 
group by owner_rep

您还可以在it_source字段上的其他一个原始查询中添加另一个GROUP by item。