Represent multiple rows of data in one column as m

2019-08-15 12:09发布

问题:

I have a table with the following structure and data:

batsman | runs | year

 1      | 800  | 2012
 1      | 950  | 2011
 1      | 1050 | 2010
 2      | 550  | 2012
 2      | 650  | 2011
 2      | 400  | 2010
 3      | 900  | 2012

This data needs to be Selected through a sql query as:

batsman | 2012 | 2011 | 2010

  1     | 800  | 950  | 1050
  2     | 550  | 650  | 400
  3     | 900  |  -   |  -

I'm trying to do this through a stored proc. The assumption can be made that the number of columns (in terms of years) is fixed: 3. Also note, there are no arithmetic operations necessary - all the numbers I need are already there, they just need to be represented column-wise.

回答1:

There are several ways that you can convert the rows of data into columns.

In SQL Server you can use the PIVOT function:

select batsman, [2012], [2011], [2010]
from 
(
  select batsman, runs, year
  from yourtable
) d
pivot
(
  sum(runs)
  for year in ([2012], [2011], [2010])
) piv;

Or you can use an aggregate function with a CASE expression:

select batsman,
  sum(case when year = 2012 then runs else 0 end) [2012],
  sum(case when year = 2011 then runs else 0 end) [2011],
  sum(case when year = 2010 then runs else 0 end) [2010]
from yourtable
group by batsman;

The other version will work great if you have a known number of columns. But if you are going to have an unknown number of year values, then you will need to use dynamic SQL:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(year) 
                    from yourtable
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT batsman,' + @cols + ' 
            from 
            (
                select batsman, runs, year
                from yourtable
            ) x
            pivot 
            (
                sum(runs)
                for year in (' + @cols + ')
            ) p '

execute(@query)


回答2:

Please try PIVOT:

declare @tbl as table(batsman int, runs int, yearr int)
insert into @tbl values
(1, 800, 2012),
(1, 950, 2011),
(1, 1050, 2010),
(2, 550, 2012),
(2, 650, 2011),
(2, 400, 2010),
(3, 900, 2012)

select * From @tbl

select *
from
(
  select *
  from @tbl
) d
pivot
(
  max(runs)
  for yearr in ([2012], [2011], [2010])
) piv;


回答3:

You would need to use Pivot Tables as detailed here: http://blogs.msdn.com/b/spike/archive/2009/03/03/pivot-tables-in-sql-server-a-simple-sample.aspx

For example:

select * from batsman
pivot (runs for Year in ([2012], [2011], [2010])) as runsperyear