为每个案例运行一个select语句

时间:2014-09-29 14:22:30

标签: sql

我是一个完整的SQL新手。我想要显示一个简单的网格.....

  1. 注册......> 30 ..... 30-60 ..... 60-90 .....> 90 .....总计
  2. 2月 ............... 4 .......... 30 ........... 6 ......... 0 ....... 40
  3. 3月 ............. 0 ......... 11 ........... 1 ... ....... 4 ....... 16
  4. 可能
  5. Jun
  6. 总计表示每月客户注册次数的总和,以及“30 -60”之类的数字代表自今天日期起30-60天内的数字注册次数< / p>

    我使用的查询如下......

    SELECT case MONTH(Datecreated)
    when   2 then 'Febrary'
    when   3 then 'March'
    when   4 then 'April'
    when   5 then 'May'
    when   6 then 'June'
    end as 'Signup Month',
    
    
    (select count(*) from WS_USER_DETAILS where datediff(day, datecreated, GETUTCDATE()) < 30 )  as  '<= 30 days',
    (select count(*) from WS_USER_DETAILS where datediff(day, datecreated, GETUTCDATE()) between 30 and 60) as  '<= 60 days',
    (select count(*) from WS_USER_DETAILS where datediff(day, datecreated, GETUTCDATE()) between 60 and 90) as  '<= 90 days',
    (select count(*) from WS_USER_DETAILS where datediff(day, datecreated, GETUTCDATE()) >90) as  '<= 120 days', 
    
    count(userid) AS 'Total' FROM WS_USER_DETAILS
    
    
    group by MONTH(Datecreated)
    

    我遇到的问题是...... 我想运行每个月的select语句。然而,它填满所有月份。例如......

    1. 注册......&gt; 30 ..... 30-60 ..... 60-90 .....&gt; 90 .....总计
    2. 2月 ............... 0 .......... 11 ........... 1 ......... 4 ....... 40
    3. 3月 ............. 0 ......... 11 ........... 1 ... ....... 4 ....... 16
    4. 如果有可能,有人可以告诉我如何运行每个月的选择语句吗?感谢

3 个答案:

答案 0 :(得分:1)

我认为这样的事情会更好地满足您的需求。您可以使用Sum Function和case语句像count一样工作。

    SELECT
    DATENAME(MM,Datecreated), --This Function automatically gets the name of the month

    --Here is where you can use case statements like a count if. 
    SUM(CASE WHEN DATEDIFF(dd, datecreated, GETUTCDATE()) < 30 THEN 1 ELSE 0 END) '<= 30 days',
    SUM(CASE WHEN DATEDIFF(dd, datecreated, GETUTCDATE()) < 60 THEN 1 ELSE 0 END) '<= 60 days',
    SUM(CASE WHEN DATEDIFF(dd, datecreated, GETUTCDATE()) < 90 THEN 1 ELSE 0 END) '<= 90 days',
    SUM(CASE WHEN DATEDIFF(dd, datecreated, GETUTCDATE()) < 120 THEN 1 ELSE 0 END) '<= 120 days'

    FROM WS_USER_DETAILS

    GROUP BY Datepart(MM, Datecreated)

来源:

  1. http://msdn.microsoft.com/en-us/library/ms189794.aspx
  2. http://msdn.microsoft.com/en-us/library/ms174420.aspx
  3. http://msdn.microsoft.com/en-us/library/ms174420.aspx

答案 1 :(得分:1)

首先选择每条记录的时间跨度,然后分组和计数。注意不要有重叠的范围。

select
  monthname,
  isnull(sum(lessthan30),0) as lt30,
  isnull(sum(between30and59),0) as btw30a59,
  isnull(sum(between60and89),0) as btw60a89,
  isnull(sum(between90and119),0) as btw90a119,
  isnull(sum(morethan119),0) as mt119,
  isnull(sum(lessthan30),0) + isnull(sum(between30and59),0) + isnull(sum(between60and89),0) + isnull(sum(between90and119),0) + isnull(sum(morethan119),0) as total
from
(
  select
    month(datecreated) as mon,
    datename(month,datecreated) as monthname,
    case when datediff(day, datecreated, GETUTCDATE()) < 30 then 1 else 0 end as lessthan30,
    case when datediff(day, datecreated, GETUTCDATE()) between 30 and 59 then 1 else 0 end as between30and59,
    case when datediff(day, datecreated, GETUTCDATE()) between 60 and 89 then 1 else 0 end as between60and89,
    case when datediff(day, datecreated, GETUTCDATE()) between 90 and 119 then 1 else 0 end as between90and119,
    case when datediff(day, datecreated, GETUTCDATE()) >= 120 then 1 else 0 end as morethan119
  from WS_USER_DETAILS
) as dummy
group by monthname, mon
order by mon;

答案 2 :(得分:1)

基于@ThorstenKettner,但通过删除函数调用并添加sergability来增加性能

declare @today date
declare @_30 date
declare @_31 date
declare @_59 date
declare @_60 date
declare @_89 date
declare @_90 date
declare @_119 date
declare @_120 date


set @today = GETUTCDATE()
set @_30 = datediff(day, @today, 30)
set @_31 = datediff(day, @today, 31)
set @_59 = datediff(day, @today, 59)
set @_60 = datediff(day, @today, 60)
set @_89 = datediff(day, @today, 89)
set @_90 = datediff(day, @today, 90)
set @_119 = datediff(day, @today, 119)
set @_120 = datediff(day, @today, 120)


select
  monthname,
  isnull(sum(lessthan30),0) as lt30,
  isnull(sum(between30and59),0) as btw30a59,
  isnull(sum(between60and89),0) as btw60a89,
  isnull(sum(between90and119),0) as btw90a119,
  isnull(sum(morethan119),0) as mt119,
  isnull(sum(lessthan30),0) + isnull(sum(between30and59),0) + isnull(sum(between60and89),0) + isnull(sum(between90and119),0) + isnull(sum(morethan119),0) as total
from
(
  select
    month(datecreated) as mon,
    datename(month,datecreated) as monthname,
    case when datecreated < @_30 then 1 else 0 end as lessthan30,
    case when datecreated between @_30 and @_59 then 1 else 0 end as between30and59,
    case when datecreated  between @_60 and @_89 then 1 else 0 end as between60and89,
    case when datecreated  between @_90 and @_119 then 1 else 0 end as between90and119,
    case when datecreated  >= @_120 then 1 else 0 end as morethan119
  from WS_USER_DETAILS 
)as dummy
group by monthname, mon
order by mon;