简体   繁体   English

SQL Pivot在这种情况下?

[英]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

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. 我想我可以利用PIVOT语句,但是我看到的所有示例对此似乎都很简单。

Thanks in advance. 提前致谢。

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 参见带有演示的SQL Fiddle

If the pivot columns (your Dates) are known then you can use the PIVOT operator to get your results: 如果知道枢轴列(您的日期),则可以使用PIVOT运算符获取结果:

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 一个很好的例子在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM