没有聚合的动态透视

时间:2015-08-02 13:13:18

标签: sql-server

您好我看过很多文章,但没有人回复我的问题。 我有一张表ProductPropertyPrice

CREATE TABLE [dbo].[Product_PropertyPrice](
[id] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NULL,
[PropertyID] [int] NULL,
[PropertyValueID] [int] NULL,
[PriceID] [int] NULL,
[ValidFrom] [date] NULL,

我希望每个产品都有一行,每个产品从日期开始有效,所有属性id和propertiesvaluesid以及价格ID为列

我尝试了这个,但没有工作可以有人帮助我吗?

抱歉,我无法发布我的代码,我收到错误,我是stachoverflow的新手

     create PROCEDURE [dbo].[GetPriceTable] (@ProductTypeID  int)
       AS
    BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

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

    SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.PropertyID) 
            FROM (SELECT Product_PropertiesLookup.PropertyID,Product_PropertiesLookup.PropertyName
FROM         Product_PropertiesLookup INNER JOIN
                      Product_PropertiesperProductType ON Product_PropertiesLookup.PropertyID = Product_PropertiesperProductType.ProductPropertyID
WHERE     (Product_PropertiesperProductType.ProductTypeID = 1)) c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

        --pivot query 
set @query = 'SELECT  ProductID,PriceID,ValidFrom, ' + @cols + ',rownum from 
            ( SELECT     ProductID, PriceID, ValidFrom,PropertyValueID,PropertyID,
            row_number() over (partition by  propertyid order by productid ) as RowNum
      FROM         Product_PropertyPrice ) x
            pivot 
            (
              max(PropertyValueID)
                for PropertyID in (' + @cols + ')

            ) p '
           exec (@query)
       END

我收到了这个结果

ProductID   PriceID     ValidFrom  1           2           3           4           RowNum
----------- ----------- ---------- ----------- ----------- ----------- ----------- --------------------
110         1           2015-01-01 0           NULL        1           1           1
110         1           2015-01-01 NULL        NULL        NULL        1           2
110         1           2015-01-01 NULL        NULL        1           1           3
110         1           2015-01-01 1           NULL        1           1           4
110         1           2015-01-01 2           NULL        NULL        1           5
110         1           2015-01-01 NULL        NULL        NULL        1           6
110         1           2015-01-01 NULL        NULL        NULL        1           7
110         1           2015-01-01 3           NULL        NULL        1           8
110         1           2015-01-01 4           NULL        1           1           9
110         1           2015-01-01 NULL        NULL        1           1           10
110         1           2015-01-01 5           NULL        1           1           11
110         1           2015-01-01 6           NULL        1           1           12
110         1           2015-01-01 7           NULL        1           1           13
110         1           2015-01-01 8           NULL        1           1           14
110         1           2015-01-01 9           NULL        1           1           15
110         1           2015-01-01 10          NULL        1           1           16
110         1           2015-01-01 11          NULL        1           1           17
110         1           2015-01-01 12          NULL        1           1           18
110         1           2015-01-01 13          NULL        1           1           19
110         1           2015-01-01 14          NULL        1           1           20
110         1           2015-01-01 15          NULL        1           1           21
110         1           2015-01-01 16          NULL        1           1           22
110         1           2015-01-01 17          NULL        1           1           23
110         1           2015-01-01 NULL        NULL        1           1           24
110         1           2015-01-01 NULL        NULL        1           1           25
110         1           2015-01-01 NULL        NULL        1           1           26
110         1           2015-01-01 NULL        NULL        1           1           27
110         1           2015-01-01 NULL        NULL        1           1           28
110         1           2015-01-01 NULL        NULL        1           1           29

sample data

id          ProductID   PropertyID  PropertyValueID PriceID     ValidFrom
----------- ----------- ----------- --------------- ----------- ----------
70          110         1           23              2           2015-01-01
71          110         3           1               2           2015-01-01
72          110         4           1               2           2015-01-01
73          110         1           24              2           2015-01-01
74          110         3           1               2           2015-01-01
75          110         4           1               2           2015-01-01
76          110         1           25              3           2015-01-01
77          110         3           1               3           2015-01-01
78          110         4           1               3           2015-01-01
79          110         1           26              3           2015-01-01
80          110         3           1               3           2015-01-01
81          110         4           1               3           2015-01-01
82          110         1           27              3           2015-01-01
83          110         3           1               3           2015-01-01
84          110         4           1               3           2015-01-01

Result i need
 ProductID   PropertyID(1)  PropertyID(3) PropertyID(4) PriceID     ValidFrom
  110         23            1              1            2           2015-01-01
  110         24            1              1            2           2015-01-01
  110         25            1              1            3           2015-01-01
  110         26            1              1            3           2015-01-01
  110         27            1              1            3           2015-01-01

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

SELECT ProductID,
       [PropertyID(1)]=Max([PropertyID(1)]),
       [PropertyID(3)]=Max([PropertyID(3)]),
       [PropertyID(4)]=Max([PropertyID(4)]),
       ValidFrom=Max(ValidFrom)
FROM   (SELECT ProductID,
               rn=Row_number()
                    OVER(
                      partition BY PropertyID
                      ORDER BY (SELECT NULL)),
               PropertyID,
               CASE WHEN PropertyID = 1 THEN PropertyValueID END   AS [PropertyID(1)],
               CASE WHEN PropertyID = 3 THEN PropertyValueID END   AS [PropertyID(3)],
               CASE WHEN PropertyID = 4 THEN PropertyValueID END   AS [PropertyID(4)],
               PriceID,
               ValidFrom AS ValidFrom
        FROM   Yourtable)a
GROUP  BY ProductID,
          rn 

您需要将上述内容转换为dynamic query。如果您在转换为dynamic query

时需要任何帮助,请告诉我