开发者

SQL Pivot in this case?

I'm trying to make this

DATE             Insurance  Type    Billed  Rate
2/28/2011 0:00   BC/BS      INP     B       0.6383
2/28/2011 0:00   BC/BS      OUT     B       0.5216
2/28/2011 0:00   BC/BS      INP     U       0.1988
2/28/2011 0:00   BC/BS      OUT     U       0.3493
3/31/2011 0:00   BC/BS      INP     B       0.69
3/31/2011 0:00   BC/BS      OUT     B       0.6136
3/31/2011 0:00   BC/BS      INP     U       0.1877
3/31/2011 0:00   BC/BS      OUT     U       0.3567

Look like this

Insurance   Type    Billed  2/28/2011 0:00  3/31/2011 0:00
BC/BS       INP     B       0.6383           0.69
BC/BS       OUT     B       0.5216           0.6136
BC/BS       INP     U       0.1988           0.1877
BC/BS       OUT     U       0.3493           0.3567
开发者_C百科

So that the date field row data ends up becoming the column headers for each distinct value. I think I can utilize the PIVOT statement but all of the examples I look at seem to be to simple for this.

Thanks in advance.


If the pivot columns (your Dates) are known then you can use the PIVOT operator to get your results:

declare @t table (date datetime, Insurance varchar(10), Type char(3), Billed char(1), Rate decimal(10,4));

    insert into @t
        values  ('2/28/2011 0:00',   'BC/BS',      'INP',     'B',       '0.6383'),
                ('2/28/2011 0:00',   'BC/BS',      'OUT',     'B',       '0.5216'),
                ('2/28/2011 0:00',   'BC/BS',      'INP',     'U',       '0.1988'),
                ('2/28/2011 0:00',   'BC/BS',      'OUT',     'U',       '0.3493'),
                ('3/31/2011 0:00',   'BC/BS',      'INP',     'B',       '0.69'),
                ('3/31/2011 0:00',   'BC/BS',      'OUT',     'B',       '0.6136'),
                ('3/31/2011 0:00',   'BC/BS',      'INP',     'U',       '0.1877'),
                ('3/31/2011 0:00',   'BC/BS',      'OUT',     'U',       '0.3567')

    select  *
    from    (   select  [date] as pivot_col, 
                        Insurance, 
                        [Type], 
                        Billed, 
                        Rate 
                from    @t
            ) as d
    pivot   (   sum(Rate) for pivot_col in ([2/28/2011],[3/31/2011])
            ) as p;

Its likely you dont know the values and if that's the case you will need to look into using the same technique but in a dynamic way. A great example is here


Based on the sample data that you provided, I am guessing you will have a long list of dates that you will need to turn into columns. While you can use a static pivot like the other answer, you can use a dynamic pivot similar to below:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(char(10), date, 101)) 
                    from test
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query 
      = 'SELECT Insurance, [Type], Billed,' + @cols + ' from 
         (
            SELECT [date], 
               Insurance, 
               [Type], 
               Billed, 
               Rate 
            FROM test
         ) x
         pivot 
         (
            sum(Rate)
            for [date] in (' + @cols + ')
         ) p 
         order by billed'

execute(@query)

See SQL Fiddle with Demo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜