SQL Pivot-但不要求和 [英] Sql pivot - but dont sum the values

查看:167
本文介绍了SQL Pivot-但不要求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的表架构

I have a table schema that looks like this

CREATE TABLE [dbo].[Discounts](
      [Id] [int] NOT NULL,
      [ProductId] [varchar(50)] NOT NULL,
      [LowerBoundDays] [int] NOT NULL,
      [UpperBoundDays] [int] NOT NULL,
      [Discount] [decimal](18, 4) NOT NULL,

还有一些这样的数据

        lower       upper discount(%)
product1  0         10          0
product1  10        30          1
product1  30        60          2
product1  60        90          3
product1  90        120         4
product2  0         10          0
product2  10        30          1
product2  30        60          2
product2  60        90          3
product2  90        120         4

如何进行数据透视查询以获取两行,如下所示:

How can I do a pivot query to get 2 rows that look like this:

            0-10    10-30   30-60   60-90   90-120
product1    0       1       2       3       4
product2    0       1       2       3       4

推荐答案

由于使用的是SQL Server,因此有几种方法可以将数据行转换为列.

Since you are using SQL Server, there are several ways that you can convert the rows of data into columns.

您可以将聚合函数与CASE表达式一起使用以获取结果:

You can use an aggregate function with a CASE expression to get the result:

select productid,
  max(case when lower = 0 and upper = 10 then discount end) [0-10],
  max(case when lower = 10 and upper = 30 then discount end) [10-30],
  max(case when lower = 30 and upper = 60 then discount end) [30-60],
  max(case when lower = 60 and upper = 90 then discount end) [60-90],
  max(case when lower = 90 and upper = 120 then discount end) [90-120]
from CorporateSpread
group by productid;

请参见带演示的SQL提琴.

如果您使用的是SQL Server 2005+,则可以使用 PIVOT 函数:

If you are using SQL Server 2005+, then you can use the PIVOT function:

select productid, [0-10], [10-30], [30-60], [60-90],[90-120]
from 
(
  select productid,
    discount,
    cast(lower as varchar(10)) + '-' + cast(upper as varchar(10)) rng
  from CorporateSpread
) d
pivot
(
  max(discount)
  for rng in ([0-10], [10-30], [30-60], [60-90],[90-120])
) piv;

请参见带有演示的SQL提琴.

如果值的数量已知,则上述两个版本非常有用,但是如果范围的数量未知,则需要使用动态SQL:

The above two version work great if you have a known number of values, but if you have an unknown number of ranges, then you will need to to use dynamic SQL:

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

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(cast(lower as varchar(10)) + '-' + cast(upper as varchar(10))) 
                    from CorporateSpread
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT productid, ' + @cols + ' 
            from 
            (
                select productid,
                  discount,
                  cast(lower as varchar(10)) + ''-'' + cast(upper as varchar(10)) rng
                from CorporateSpread
            ) x
            pivot 
            (
                max(discount)
                for rng in (' + @cols + ')
            ) p '

execute sp_executesql @query;

请参见带演示的SQL提琴.所有版本都会给出结果:

See SQL Fiddle with Demo. All versions will give a result:

| PRODUCTID | 0-10 | 10-30 | 30-60 | 60-90 | 90-120 |
-----------------------------------------------------
|  product1 |    0 |     1 |     2 |     3 |      4 |
|  product2 |    0 |     1 |     2 |     3 |      4 |

这篇关于SQL Pivot-但不要求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆