选择返回动态列

时间:2010-04-28 13:08:19

标签: sql sql-server views

我有两个表:标准和服务产品。标准可以有多个服务产品。每个标准可以有不同数量的服务产品。

我需要做的是编写一个视图,它将返回一些常见数据,然后在一行中列出服务产品。例如:

Standard Id | Description | SO #1 | SO #2 | SO #3 | ... | SO #21 | SO Count
1           | One         | A     | B     | C     | ... | G      |  21
2           | Two         | A     |       |       | ... |        |  1
3           | Three       | B     | D     | E     | ... |        |  3

我不知道怎么写这个。 SO列的数量设置为特定的数字(在这种情况下为21),因此我们不能超过该数量。

有关如何处理此事的任何想法?

我开始的地方如下。它只是为每个服务产品返回多行,当它们需要在一行时。

SELECT *
  FROM SERVICE_OFFERINGS
 WHERE STANDARD_KEY IN (SELECT STANDARD_KEY
                          FROM STANDARDS)

其他SQL

所以这是我拥有的SQL返回我想要的所有内容,但由于有11个服务产品,将返回11行。我一直在尝试数据透视表,似乎无法弄清楚这一点。有人可以帮助代码示例吗?

SELECT DISTINCT stpc.standard_key,
                stpc.test_id,
                NULL AS pricebook_id,
                stpc.stabdard_name AS description,
                stpc.date_start AS begin_date,
                stpc.date_end AS end_date,
                sopd.service_offering_id
  FROM STANDARDS stpc,
       SERVICE_OFFERINGS sopd
 WHERE 1=1
   AND sopd.standard_key = stpc.standard_key
 ORDER BY stpc.standard_key, sopd.service_offering_id

更新

由于数据库没有假设PIVOT表(并且无法弄清楚XML建议),因此我不得不做一些棘手的SQL来使其工作。这是我使用的:

select stpc.oracle_product_code AS test_id,
       CASE WHEN stpc.store_key = 200 THEN 'CE_USAUSD09'
            WHEN stpc.store_key = 210 THEN 'CE_CANCAD09' END AS pricebook_id,
       stpc.standard_name AS its_test_desc,
       CONVERT(VARCHAR(10), stpc.date_start, 101) AS begin_date,
       CONVERT(VARCHAR(10), stpc.date_end, 101) AS end_date,
       MAX(CASE WHEN rn = 1 THEN b.service_offering_id END) AS SERVICE_OFFERING_1,
       MAX(CASE WHEN rn = 2 THEN b.service_offering_id END) AS SERVICE_OFFERING_2,
       MAX(CASE WHEN rn = 3 THEN b.service_offering_id END) AS SERVICE_OFFERING_3,
       MAX(CASE WHEN rn = 4 THEN b.service_offering_id END) AS SERVICE_OFFERING_4,
       MAX(CASE WHEN rn = 5 THEN b.service_offering_id END) AS SERVICE_OFFERING_5,
       MAX(CASE WHEN rn = 6 THEN b.service_offering_id END) AS SERVICE_OFFERING_6,
       MAX(CASE WHEN rn = 7 THEN b.service_offering_id END) AS SERVICE_OFFERING_7,
       MAX(CASE WHEN rn = 8 THEN b.service_offering_id END) AS SERVICE_OFFERING_8,
       MAX(CASE WHEN rn = 9 THEN b.service_offering_id END) AS SERVICE_OFFERING_9,
       MAX(CASE WHEN rn = 10 THEN b.service_offering_id END) AS SERVICE_OFFERING_10,
       MAX(CASE WHEN rn = 11 THEN b.service_offering_id END) AS SERVICE_OFFERING_11,
       MAX(CASE WHEN rn = 12 THEN b.service_offering_id END) AS SERVICE_OFFERING_12,
       MAX(CASE WHEN rn = 13 THEN b.service_offering_id END) AS SERVICE_OFFERING_13,
       MAX(CASE WHEN rn = 14 THEN b.service_offering_id END) AS SERVICE_OFFERING_14,
       MAX(CASE WHEN rn = 15 THEN b.service_offering_id END) AS SERVICE_OFFERING_15,
       MAX(CASE WHEN rn = 16 THEN b.service_offering_id END) AS SERVICE_OFFERING_16,
       MAX(CASE WHEN rn = 17 THEN b.service_offering_id END) AS SERVICE_OFFERING_17,
       MAX(CASE WHEN rn = 18 THEN b.service_offering_id END) AS SERVICE_OFFERING_18,
       MAX(CASE WHEN rn = 19 THEN b.service_offering_id END) AS SERVICE_OFFERING_19,
       MAX(CASE WHEN rn = 20 THEN b.service_offering_id END) AS SERVICE_OFFERING_20,
       MAX(CASE WHEN rn = 21 THEN b.service_offering_id END) AS SERVICE_OFFERING_21,
       MAX(rn) AS service_offering_count
FROM (
select standard_key,
       service_offering_id, 
       row_number() over (partition by standard_key order by standard_key) rn
from SERVICE_OFFERINGS
) B,
SERVICE_OFFERINGS sopd,
STANDARDS stpc
where b.service_offering_id = sopd.service_offering_id
AND b.standard_key = stpc.standard_key
AND sopd.standard_key = stpc.standard_key
AND stpc.store_key IN (200,210)
AND stpc.create_date > '03/29/2010'
group by stpc.oracle_product_code,stpc.store_key,stpc.standard_name,stpc.date_start,stpc.date_end

3 个答案:

答案 0 :(得分:3)

您可以使用PIVOT功能。

查看http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=PIVOTData

而不是PIVOT,您应该使用FOR XML和SplitToColumns的组合。

使用FOR XML并将您的产品展示到单个列Concatenating Row Values in Transact-SQL

然后使用CTE样式函数将单个单元格分解为列,如下所示http://www.sqlservercentral.com/articles/CTE/67974/

这将为您提供一个以您需要的方式转出的表格。

然后进行算术以获得非空列的计数,并且最后得到所需的计数。

答案 1 :(得分:1)

是的,您需要使用数据透视查询。

21列是否始终相同,或者您是否可以显示不超过21列(例如,数百个)?如果实际的colmns可能因查询而异,那么您将不得不考虑编写动态查询(将查询构建为字符串 - 合并要旋转的列 - 然后执行字符串)。

答案 2 :(得分:1)

菲利普是对的。如果您总是有21列,那么它就是一个简单的Pivot查询。我在这里粘贴了一个你可以使用的示例代码。 但是如果列数在1和21之间变化,则必须编写动态查询。

SELECT standard_key, stabdard_name, [A] as SO1, [B] as SO2, [C] as SO3, [D] as SO4, [E] as SO5....-- and so on with the other columns
FROM 
(SELECT ST.standard_key, ST.stabdard_name, SO.service_offering_id
FROM SERVICE_OFFERINGS SO
    INNER JOIN STANDARDS ST
        ON SO.standard_key= ST.standard_key)p
PIVOT
(
MAX (service_offering_id)
FOR service_offering_id IN
( [A], [B], [C], [D], [E]....-- and so on with the other values)
) AS pvt
ORDER BY standard_key

如果colums可能会有所不同,你可以试试这样的事情:

declare @sql nvarchar(max)
declare @sql2 nvarchar(max)

SET @sql2=''
set @sql = '
select
    standard_key, stabdard_name,'

select @sql = @sql + '['+ service_offering_id + '] AS [SO' +  convert(varchar, Row_number() OVER (ORDER BY  service_offering_id))+ '],'
from (select distinct [service_offering_id] from [SERVICE_OFFERINGS]) as moduleids

select @sql2 = @sql2 + '['+ service_offering_id + '],'
from (select distinct [service_offering_id] from [SERVICE_OFFERINGS]) as moduleids

set @sql2 = substring(@sql2,1,len(@sql2)-1)

set @sql = substring(@sql,1,len(@sql)-1) + '
FROM 
(SELECT ST.standard_key, ST.stabdard_name, SO.service_offering_id
FROM SERVICE_OFFERINGS SO
    INNER JOIN STANDARDS ST
        ON SO.standard_key= ST.standard_key)p
PIVOT
(
MAX (service_offering_id)
FOR service_offering_id IN
(' + @sql2 + 
')) AS pvt
ORDER BY standard_key'

print @sql

exec sp_executesql @sql