一个命令中的SQL查询

时间:2018-03-18 02:39:46

标签: sql sql-server tsql pivot

两个表:
 第一个表名:employee_1 |第二个表名:employee_2

code | Name | Salary | Month|Year    code | Name | Salary |Post |Month |Year
-----+------+--------+------+--      -----+------+--------+-----+------+----
1    | A    |   1000 | May  | 2017    1   | A    | 2000   |clerk|April |2017
2    | B    |   1150 | May  | 2017    2   | B    | 3000   |clerk|April |2017
1    | A    |   1200 | June | 2017
2    | B    |   1150 | June | 2017
1    | A    |   4000 | July | 2017
2    | B    |   3500 | July | 2017

年份= 2017年的输出应该是这样的:

code  | Name |April | May  | June  | July
------+------+------+-------+------+-
 1    | A    |2000  | 1000 | 1200  | 4000
 2    | B    |3000  | 1150 | 1150  | 3500

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以在一个命令中为输出编写SQL查询,因为您希望将行(Month)更改为列。所以理想的解决方案是编写一个数据透视查询。

您可以使用PIVOT函数将数据从行转换为列。

如果月份未知或动态更改,您似乎需要使用动态SQL。

create table employee (code int , name varchar(100), salary money, Month1 varchar(100));


INSERT INTO employee (code, name, salary, Month1)
VALUES
(1   ,   'A'   ,    1000   ,   'may'),
(2   ,   'B'   ,    1150   ,   'may'),
(1   ,   'A'   ,    1200   ,   'June'),
(2   ,   'B'   ,    1150   ,   'June'),
(1    ,  'A'    ,   4000  ,    'July'),
(2    ,  'B'     ,  3500  ,    'July')

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Month1) 
                    from employee
                    group by Month1
                    order by Month1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

        select @cols

set @query = 'SELECT code,name,' + @cols + ' from 
             (
                select code, name, salary,Month1
                from employee
            ) x
            pivot 
            (
                sum(salary)
                for month1 in (' + @cols + ')
            ) p '

execute (@query)

#与评论中要求的年份

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @year As varchar(100)

select @cols = STUFF((SELECT ',' + QUOTENAME(Month1) 
                    from employee1
                    group by Month1
                    order by Month1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @year = '2018'

set @query = 'SELECT code,name,' + @cols + ' from 
             (
                select code, name, salary,Month1,year
                from employee1
            ) x
            pivot 
            (
                sum(salary)
                for month1 in (' + @cols + ')
            ) p  where year ='+ @year +''

execute (@query)

关于两个表

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX),
    @year As varchar(100),
    @cols1 as varchar(1000),
    @cols2 as varchar(1000)

select @cols = STUFF((SELECT ',' + QUOTENAME(Month1) 
                    from employee1
                    group by Month1
                    order by Month1
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @cols1 = STUFF((SELECT ',' + QUOTENAME(Month) 
                    from employee2
                    group by Month
                    order by Month
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @cols2 = @cols1 +',' + @cols


set @year = '2017'

set @query = 'SELECT code,name,' + @cols2 + ' from 
             (
                select code, name, salary,Month1,year
                from employee1 
                union all
                select code,name,salary,Month,year from employee2
            ) x
            pivot 
            (
                sum(salary)
                for month1 in (' + @cols2 + ')
            ) p  where year ='+ @year +''

execute (@query)