MS SQL - 按月分组的计数

时间:2017-01-04 11:17:31

标签: sql-server

I have a table [tblCalls] containing a date and a telephone number.[logdate] & [telephone]

I'm trying to produce an output similar to the following:

Date        Mobiles       Landlines
Nov 2016     28           47
Dec 2016     65           98
Jan 2017     11           17
... and so on

Using the following:
    SELECT MONTH(logdate) as myDate,
    SUM(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END) mobiles,
    sum(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END) landlines
    FROM tblCalls
    WHERE username='myusername' AND YEAR(logdate)='2016'
    GROUP BY MONTH(logdate)

I can produce this:
myDate    Mobiles    Landlines
1         28         47
2         65         98
3         11         17
4         09         14
5         32         8
... and so on

My question is in 2 parts.
1. How do I combine the month and year together and span years (not just 2016).
2. I have another table [tblUsersLogs] which also contains a date [logdate] and a username, I'd like include a count from this table on the number of entries for a specific user grouped by months in the year to produce something like this...
Date        Mobiles       Landlines    UsernameCount
Nov 2016     28           47           50
Dec 2016     65           98           44
Jan 2017     11           17           45
... and so on

Is this possible?

UPDATE (some sample data)

[tblCalls]
logDate                     telephone
2017-01-04 12:18:36.243     01507443000
2017-01-04 11:23:17.313     07880507000
2017-01-04 11:23:16.760     01216286000
2017-01-04 11:23:15.837     07541360000
2017-01-04 11:23:15.570     01970611000

[tblUserLogs]
logDate                     username
2017-01-04 12:23:51.530     usera
2017-01-04 12:23:38.350     usera
2017-01-04 12:23:08.530     userb
2017-01-04 12:22:45.020     userc
2017-01-04 12:22:35.437     usera

Hope that helps

2 个答案:

答案 0 :(得分:2)

在UserNameCount上不清楚所以我做了一个假设并删除了WHERE

Select   Date      = Format(myDate,'MMM yyyy')
        ,mobiles   = sum(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END) 
        ,landlines = sum(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END) 
        ,UserNamesCount = count(Distinct Usernames)
From     tblCalls
Where    Year(logdate)=2016
Group By Format(myDate,'MMM yyyy')
Order By Month(myDate)

答案 1 :(得分:0)

您只需将日期更改为该月的同一天,然后将格式保留到演示文稿/报告层:

SELECT dateadd(m,datediff(m,0,logdate),0) as myDate    -- This will return the first day of the month.
      ,SUM(CASE WHEN telephone LIKE '07%' THEN 1 ELSE 0 END) mobiles
      ,SUM(CASE WHEN telephone NOT LIKE '07%' THEN 1 ELSE 0 END) landlines
FROM tblCalls
WHERE username = 'myusername' -- Avoid using functions in your WHERE criteria.
  AND logdate >= '20160101'   -- Doing so makes your filtering much less efficient.
  AND logdate < '20170101'    -- For more info, google 'SARGability'.
GROUP BY dateadd(m,datediff(m,0,logdate),0)

如果您绝对 要在SQL查询中进行格式化,则可以使用:

left(datename(month,logdate),3) + ' ' + cast(year(logdate) as nvarchar(4))