如何进行单个查询

时间:2015-12-14 12:09:19

标签: sql-server

我正在使用mssql。我在循环中有以下查询以获得12个月的记录。

是否可以将以下查询作为单个查询。

我需要根据团队和部门获得所有月份的报告。

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='1' and team='M&T' and dept='GEF-SW') and
month_val='1' and team='M&T' and dept='GEF-SW'

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='2' and team='M&T' and dept='GEF-SW') and
month_val='2' and team='M&T' and dept='GEF-SW'

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='3' and team='M&T' and dept='GEF-SW') and
month_val='3' and team='M&T' and dept='GEF-SW'

.
.
.

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='12' and team='M&T' and dept='GEF-SW') and
month_val='12' and team='M&T' and dept='GEF-SW'

表格结构

[proj_duration_map]( 
    [id] [int] IDENTITY(1,1) NOT NULL,  
    [uid] [int] NOT NULL,  
    [dept] [nchar](25) NOT NULL,    
    [team] [nchar](200) NOT NULL,    
    [pid] [int] NOT NULL,    
    [week_val] [smallint] NOT NULL,   
    [month_val] [smallint] NULL,    
    [hour_val] [float] NOT NULL,
    [cur_month] [smallint] NULL,
    [year_val] [smallint] NOT NULL,
    [last_updated] [datetime] NOT NULL
   )

任何帮助都会很棒。

6 个答案:

答案 0 :(得分:4)

您可以使用以下联盟执行此操作:

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='1' and team='M&T' and dept='GEF-SW') and
month_val='1' and team='M&T' and dept='GEF-SW'

UNION ALL

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='2' and team='M&T' and dept='GEF-SW') and
month_val='2' and team='M&T' and dept='GEF-SW'

UNION ALL

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='3' and team='M&T' and dept='GEF-SW') and
month_val='3' and team='M&T' and dept='GEF-SW'

UNION ALL

select sum(hour_val) from proj_duration_map where
cur_month=(select max(cur_month) from proj_duration_map where 
month_val='12' and team='M&T' and dept='GEF-SW') and
month_val='12' and team='M&T' and dept='GEF-SW'

答案 1 :(得分:4)

我想你只想要一个聚合。要获取最新的month_val使用窗口函数:

select team, dept, month_val, sum(hour_val)
from (select dm.*,
             max(cur_month) over (partition by month_val, team, dept) as max_curmonth
      from proj_duration_map dm
     ) dm
where team = 'M&T' and dept = 'GEF-SW' and cur_month = max_curmonth
group by team, dept, month_val;

答案 2 :(得分:3)

亚历克斯的答案是正确的,你可以使用工会全部,但为什么不按月过滤,而不是GROUP BY他们??? 我认为单个查询比通过UNION ALL子句链接的更优雅的答案...

答案 3 :(得分:2)

试试这个,

SELECT SUM(hour_val) AS hour_val, 
       max(cur_month) AS cur_month, 
       month_val,year_val, team, dept 
FROM proj_duration_map 
GROUP BY month_val, year_val, team, dept

答案 4 :(得分:0)

使用PIVOT功能试试,

SELECT  * 
FROM 
(SELECT id, uid, dept, team , week_val, month_val, hour_val HourWorked
FROM proj_duration_map) AS P 
PIVOT (sum(HourWorked) FOR month_val IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as PVT
ORDER by uid, week_val

答案 5 :(得分:0)

此查询将为您提供按部门和团队

分组的个别月份的总计
j_password=password&j_username=username