【问题标题】:Is it possible to count per Month?可以按月计算吗?
【发布时间】:2014-08-09 12:20:36
【问题描述】:

我有这张桌子:

table tblCountry 
   name char(30)

table tblSite
   name char(30) primary key not null,
   country char(30) 

table tblDivingClub
   number int
   name char(30)
   country char(30)

table tblDiving
   diving_number int 
   number_of_divers int
   diving_club int
   guide int

我写了这个查询:

select 
    tblSite.name,tblSite.site_length,tblSite.site_depth,
    tblSite.water_type,tblSite.country,
    COUNT(distinct tblDivingClub.number) as number_of_clubs,
    COUNT(distinct tblDiving.diving_number) as number_of_divings,
    COUNT(distinct tblDiving.guide) as number_of_guids,
    sum(tblDiving.number_of_divers) as number_of_divers
from tblCountry 
inner join tblSite 
    on tblCountry.name=tblSite.country
inner join tblDiving 
    on tblSite.name = tblDiving.diving_site
inner join tblDivingClub 
    on tblDiving.diving_club = tblDivingClub.number
where tblSite.name in (
         select tblSite.name 
         from tblSite 
         where tblSite.country = 'New York' 
     ) 
     and tblDiving.date_of_diving >= DATEADD(year,-1, GETDATE())
group by tblSite.name,tblSite.site_length,tblSite.site_depth,
    tblSite.water_type,tblSite.country

我想显示潜水次数最多的月份 ('tblDiving.date_of_diving') 和本月的潜水次数。

有可能吗?

【问题讨论】:

    标签: sql sql-server group-by


    【解决方案1】:

    唉,这真是一个不同的查询。您的上述查询总结了多个月。而count(distinct) 不是累积的。因此,您可以每个月安排一个人参与。月份的计数为 1。一年的计数可能是 1 到 12 之间的任何值,具体取决于同一个人是否在多个月内潜水。

    所以,你可以做你想做的事:

    select t.*
    from (select tblSite.name, tblSite.site_length, tblSite.site_depth, tblSite.water_type,   
                 tblSite.country,
                 COUNT(distinct tblDivingClub.number) as number_of_clubs,
                 COUNT(distinct tblDiving.diving_number) as number_of_divings,
                 COUNT(distinct tblDiving.guide) as number_of_guids,
                 sum(tblDiving.number_of_divers) as number_of_divers,
                 year(tblDiving.date_of_diving) as yr, month(tblDiving.date_of_diving) as mon,
                 row_number() over (partition by tblSite.name, tblSite.site_length, tblSite.site_depth, tblSite.water_type, tblSite.country
                                    order by COUNT(distinct tblDiving.diving_number)
                                   ) as seqnum
          from tblCountry inner join
               tblSite
               on tblCountry.name = tblSite.country inner join
               tblDiving
               on tblSite.name = tblDiving.diving_site inner join
               tblDivingClub
             on tblDiving.diving_club = tblDivingClub.number
          where tblSite.name in (select tblSite.name from tblSite where tblSite.country = 'New York' ) and
                tblDiving.date_of_diving >= DATEADD(year, -1, GETDATE())
          group by tblSite.name, tblSite.site_length, tblSite.site_depth,
                   tblSite.water_type, tblSite.country,
                   year(tblDiving.date_of_diving), month(tblDiving.date_of_diving)
         ) t
    where seqnum = 1;
    

    要按年份获取数据摘要,您需要将其加入到原始查询中。

    【讨论】:

    • Ty,但我需要显示月份和本月的潜水次数。当我使用您的查询时,我没有看到本月的潜水次数,只有月份和年份
    • @HilaGal 。 . .如果我理解正确,number_of_divings 应该是您要查找的号码。
    • 我会尽力解释得更好。 number_of_divings 计算全年潜水。除了全年统计,我还需要显示潜水人数最多的月份的名称,以及本月的潜水人数
    • @HilaGal 。 . .正如我在答案开头所解释的那样,您无法轻松获得年度潜水次数和每月潜水次数,因为您使用的是count(distinct)。这仅解决了获得潜水次数最多的月份的问题。您需要将其加入到您的查询中以将两个总数放在一起。
    • 什么是运行查询时总是返回 1 的“seqnum”?
    【解决方案2】:

    当然:

    ...
    group by month(tblDiving.date_of_diving), ...
    

    只要它只有 1 年期限就可以工作。如果此报告将跨越数年,则必须按 year()、month() 等进行分组。或者,如果您使用 MSSQL 2012+,则有一个 EOMONTH() 函数专门用于此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 2017-02-09
      • 2011-03-24
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多