如何在一年中和所有月份中找到最高价值

时间:2017-06-26 13:32:54

标签: sql sql-server

我希望返回一年中签订的合同最多的一年和所有年份的一个月 - 这个月的合同数量最多。

我试过了:

yyyy-MM-dd HH:mm:ss.SSS

和...

SELECT
     cal.CalendarYear
    ,cal.MonthOfYear
    ,COUNT(*) AS Cnt
FROM dim.Application app
    JOIN dim.Calendar cal ON app.ApplicationDateID = cal.DateId
--WHERE (CalendarYear IS NULL) OR (MonthOfYear IS NULL)
GROUP BY
     cal.CalendarYear
    ,cal.MonthOfYear
WITH CUBE
ORDER BY COUNT(*) DESC

任何帮助将不胜感激。感谢。

3 个答案:

答案 0 :(得分:1)

这将ORDER BY UNION的每个部分独立,并且在一个SELECT中仍然有结果joined ...

SELECT x.* FROM (
    SELECT TOP 1
        cal.CalendarYear AS [Year]
        ,0               AS [Month]
        ,COUNT(*) AS Cnt
    FROM dim.Application app
        JOIN dim.Calendar cal ON app.ApplicationDateID = cal.DateId
    GROUP BY cal.CalendarYear
    ORDER BY COUNT(*) DESC
) x

UNION ALL

SELECT x.* FROM (
    SELECT TOP 1
         0                      AS [Year]
        ,cal.MonthOfYear        AS [Month]
        ,COUNT(*)               AS Cnt
    FROM dim.Application app
        JOIN dim.Calendar cal ON app.ApplicationDateID = cal.DateID
    GROUP BY cal.MonthOfYear
    ORDER BY COUNT(*) DESC
) x

答案 1 :(得分:1)

获取每年和每月的计数,并使用row_number获得合同最高的年份和月份。

SELECT 
MAX(CASE WHEN year_rank=1 then Year END) as Highest_contracts_year,
MAX(CASE WHEN year_rank=1 then Year_count END) as Year_Contracts_count
MAX(CASE WHEN month_year_rank=1 then Month END) as Highest_contracts_Month,
MAX(CASE WHEN month_year_rank=1 then Month_Year_count END) as MonthYear_Contracts_count    
FROM (SELECT T.*
      ,ROW_NUMBER() OVER(ORDER BY Year_Cnt DESC) as Year_rank
      ,ROW_NUMBER() OVER(ORDER BY Month_Year_Cnt DESC) as Month_Year_rank
      FROM (SELECT 
            cal.CalendarYear AS [Year]
            ,cal.MonthOfYear AS [Month]
            ,COUNT(*) OVER(PARTITION BY cal.CalendarYear) AS Year_Cnt
            ,COUNT(*) OVER(PARTITION BY cal.MonthOfYear) AS Month_Year_Cnt
            FROM dim.Application app
            JOIN dim.Calendar cal ON app.ApplicationDateID = cal.DateId
           ) T
      ) T

您应该指定在存在最高计数关系时需要执行的操作。假设有关系时需要所有最高计数行,请使用

With ranks as 
         (SELECT T.*
          ,RANK() OVER(ORDER BY Year_Cnt DESC) as Year_rank
          ,RANK() OVER(ORDER BY Month_Year_Cnt DESC) as Month_Year_rank
          FROM (SELECT 
                cal.CalendarYear AS [Year]
                ,cal.MonthOfYear AS [Month]
                ,COUNT(*) OVER(PARTITION BY cal.CalendarYear) AS Year_Cnt
                ,COUNT(*) OVER(PARTITION BY cal.MonthOfYear) AS Month_Year_Cnt
                FROM dim.Application app
                JOIN dim.Calendar cal ON app.ApplicationDateID = cal.DateId
               ) T
          ) 
SELECT *
FROM (SELECT DISTINCT Year,Year_Cnt FROM RANKS WHERE Year_rank=1) ry
CROSS JOIN (SELECT DISTINCT Month,Month_Year_Cnt FROM RANKS WHERE Month_Year_rank=1) rm 

答案 2 :(得分:0)

编辑:这可能是你想要的,除非你想要它在单行上:

select calendaryear AS 'year/month', cnt from (
SELECT TOP 1
     cal.CalendarYear
    ,COUNT(*) AS Cnt
FROM dim.Application AS app
    JOIN dim.Calendar AS cal ON app.ApplicationDateID = cal.DateId
GROUP BY
     cal.CalendarYear
ORDER BY COUNT(*) DESC
) as year
UNION ALL
select MonthOfYear, Cnt FROM ( 
SELECT TOP 1
     cal.CalendarYear
    ,cal.MonthOfYear
    ,COUNT(*) AS Cnt
FROM dim.Application AS app
    JOIN dim.Calendar AS cal ON app.ApplicationDateID = cal.DateId
GROUP BY
     cal.CalendarYear
    ,cal.MonthOfYear
ORDER BY COUNT(*) DESC
) AS month

它返回以下结果,其中第3个月实际上是2016年:

year/month  cnt
2017        4
3           2

我使用了以下数据作为输入

create table #calendar (DateId int, calendaryear int, monthofyear int)
create table #application (applicationdateId int)

insert into #calendar values (1,2017,01)
insert into #calendar values (2,2017,02)
insert into #calendar values (3,2017,03)
insert into #calendar values (4,2016,01)
insert into #calendar values (5,2016,03)

insert into #application values (1)
insert into #application values (1)
insert into #application values (2)
insert into #application values (3)
insert into #application values (4)
insert into #application values (5)
insert into #application values (5)