MySQL 按行分组

标签: sql MySQL
发布时间: 2015/6/12 0:35:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个表,看起来像这样:


Associate Pay_Code Hours    Site    Date       Week     Year
Bill    REG  8.0    US  3/3/2014    10      2014
Bill    REG  8.0    US  3/4/2014    10      2014
Bill    REG  8.0    US  3/5/2014    10      2014
Bill    REG  8.0    US  3/6/2014    10      2014
Bill    VTO  6.0    US  3/7/2014    10      2014
Bill    PERS 2.0    US  3/7/2014    10      2014
Mary    REG  8.0    Canada  3/3/2014    10      2014
Mary    REG  8.0    Canada  3/4/2014    10      2014
Mary    REG  8.0    Canada  3/5/2014    10      2014
Mary    REG  8.0    Canada  3/6/2014    10      2014
Mary    REG  6.0    Canada  3/7/2014    10      2014
Mary    PERS 2.0    Canada  3/7/2014    10      2014
Fred    REG  8.0    England 3/3/2014    10      2014
Fred    REG  8.0    England 3/4/2014    10      2014
Fred    REG  8.0    England 3/5/2014    10      2014
Fred    REG  8.0    England 3/6/2014    10      2014
Fred    REG  8.0    England 3/7/2014    10      2014
Wilma   REG  8.0    Jamaica 3/3/2014    10      2014
Wilma   REG  8.0    Jamaica 3/4/2014    10      2014
Wilma   REG  8.0    Jamaica 3/5/2014    10      2014
Wilma   REG  8.0    Jamaica 3/6/2014    10      2014
Wilma   REG  8.0    Jamaica 3/7/2014    10      2014
Wilma   OT   5.0    Jamaica 3/7/2014    10      2014
Jethro  REG  8.0    Africa  3/3/2014    10      2014
Jethro  REG  8.0    Africa  3/4/2014    10      2014
Jethro  REG  8.0    Africa  3/5/2014    10      2014
Jethro  REG  8.0    Africa  3/6/2014    10      2014
Jethro  REG  8.0    Africa  3/7/2014    10      2014
Jethro  OT   8.0    Africa  3/7/2014    10      2014
Frank   REG  8.0    Iraq    3/3/2014    10      2014
Frank   PERS 8.0    Iraq    3/4/2014    10      2014
Frank   PERS 8.0    Iraq    3/5/2014    10      2014
Frank   VACP 8.0    Iraq    3/6/2014    10      2014
Frank   VACP 8.0    Iraq    3/7/2014    10      2014

此查询:

SELECT Pay_Code, Site, SUM(Hours), Week(Date,4), YEAR(Date)

FROM table

GROUP BY Pay_Code, Site, YEAR(Date), WEEK(Date,4)

给我:

Pay Code    Site    Sum of Hours     Week     Year
OT          Africa   8.0             10      2014 
            Jamaica  5.0             10      2014 
PERS        Canada   2.0             10      2014 
            Iraq     16.0             10      2014 
            US       2.0             10      2014 
REG         Africa   40.0             10      2014 
            Canada   38.0             10      2014 
            England  40.0             10      2014 
            Iraq     8.0             10      2014 
            Jamaica  40.0             10      2014 
            US       32.0             10      2014 
VACP        Iraq     16.0             10      2014 
VTO         US       6.0             10      2014 

想要做是组 Pay_Code,以便他们可以进一步总结。看起来像这样的东西:

Pay Code2           Pay Code    Site    Sum of Hours      Week     Year
OT                  OT          Africa   8.0              10      2014
                                Jamaica  5.0              10      2014
Paid Shrink         PERS        Canada   2.0              10      2014
                                Iraq     16.0             10      2014
                                US       2.0              10      2014
                    VACP        Iraq     16.0              10      2014
REG                 REG         Africa   40.0              10      2014
                                Canada   38.0              10      2014
                                England  40.0              10      2014
                                Iraq     8.0              10      2014
                                Jamaica  40.0              10      2014
                                US       32.0              10      2014
Unpaid Shrink       VTO             US       6.0             10      2014

目标是按周报告 Pay_Code2 字段由站点,但表中不存在该 Pay_Code2 字段。现在,我查询了大量的数据和使用数据透视表我行进行分组。为此在 MySQL 如果可能的话!

解决方法 1:

它听起来好像你正在创建一个新的类别,基于 PayCode 的值。你可以尝试这样的事情:

SELECT CASE
         WHEN Pay_Code = 'OT' THEN 'OT'
         WHEN Pay_Code IN ('VACP','PERS') THEN 'Paid Shrink'
         WHEN Pay_Code = 'REG' THEN 'REG'
         WHEN Pay_Code IN ('VTO','LOA','BER') THEN 'Unpaid Shrink'
         ELSE 'Unknown'
       END as PayCode_2
       ,YEAR(Date), WEEK(Date,4), SUM(Hours), Site, Pay_Code
FROM table
GROUP BY PayCode_2, Pay_Code, Site, YEAR(Date), WEEK(Date,4)
赞助商