代码之家  ›  专栏  ›  技术社区  ›  Dennis G

SQL monthly join和monthly total percentage

  •  3
  • Dennis G  · 技术社区  · 14 年前

    我的头在冒烟 JOIN , WITH GROUP BY 为我常见的情况想出一个解决方案-我就是没法完全理解。让我马上给你举个例子:

    我有两张表(ColorCount和Colorname):

    ColorCount:
    ColorID Count Date
    1       42    2010-09-07
    1       1     2010-09-08
    2       22    2010-09-14
    1       20    2010-10-10
    3       4     2010-10-14
    

    ColorName:
    ColorID  Name
    1        Purple
    2        Green
    3        Yellow
    4        Red
    

    现在我只想将ColorName表连接到ColorCount表,汇总每个月的所有颜色计数,并从每月总数中计算每个计数的百分比。表胜于言:

    Output:
    Month Color   Count Percentage
    09    Purple  43    66%
    09    Green   22    33%
    09    Yellow  0     0%
    09    Red     0     0%
    10    Purple  20    83%
    10    Green   0     0%
    10    Yellow  4     16%
    10    Red     0     0%
    

    (请注意月总数 09 65 ,因此 66% 对于 Purple 还有 0 对于不存在的颜色):

    我希望有人在SQL中做梦,这是一个简单的任务。。。

    2 回复  |  直到 13 年前
        1
  •  3
  •   Philip Kelley    14 年前

    这是可行的,但需要注意以下几点:

    • 日期时间值必须仅为日期
    • 它只列出那些有数据的月份
    • 如果你有跨年的数据,我会在每月的第一天列出(我假设你不想把2009年1月的数据和2010年1月的数据汇总在一起)
    • 列格式的精确百分比细节由你决定,我得回去工作了

    代码:

    ;with cte (ColorId, Mth, TotalCount)
     as (select
            ColorId
           ,dateadd(dd, -datepart(dd, Date) + 1, Date) Mth
           ,sum(Count) TotalCount
          from ColorCount
          group by ColorId, dateadd(dd, -datepart(dd, Date) + 1, Date))
     select
        AllMonths.Mth [Month]
       ,cn.Name
       ,isnull(AggData.TotalCount, 0) [Count]
       ,isnull(100 * AggData.TotalCount / sum(AggData.TotalCount * 1.00) over (partition by AllMonths.Mth), 0) Percentage
      from (select distinct Mth from cte) AllMonths
       cross join ColorName cn
       left outer join cte AggData
        on AggData.ColorId = cn.ColorId
         and AggData.Mth = AllMonths.Mth
      order by AllMonths.Mth, cn.ColorId
    
        2
  •  2
  •   Alex    14 年前
    SELECT
        [Month],
        [Name],
        [Count],
        CASE WHEN TotalMonth=0 THEN 'INF' ELSE cast(round([Count],0)*100.0/TotalMonth,0) as int) + '%' END as [Percentage]
    FROM 
    (
    SELECT 
        [Months].[Month] as [Month],
        CN.[Name],
        isnull(CC.[Count],0) as [Count],
        (SELECT SUM([Count]) FROM ColorCount WHERE 
                datepart(month,[Date])=datepart(month,CC.[Date])
         ) as [TotalMonth]
    FROM (SELECT DISTINCT datepart(month,[Date]) as [Month] FROM ColorCount) [Months]
    LEFT JOIN ColorName CN ON [Months].[Month]=datepart(month,CC.[Date])
    LEFT JOIN ColorCount CC ON CN.ColorID=CC.ColorID
    ) AS tbl1
    ORDER BY
        [Month] ASC,
        [Name] ASC
    

    像这样的。。。。 它不会显示本月的前零位,但它真的重要吗?

    推荐文章