选择返回动态列

Posted

技术标签:

【中文标题】选择返回动态列【英文标题】:Select return dynamic columns 【发布时间】:2010-04-28 13:08:19 【问题描述】:

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

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

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

【问题讨论】:

【参考方案1】:

您可以为此使用 PIVOT 功能。

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

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

使用FOR XML 并将您的产品转为一列Concatenating Row Values in Transact-SQL

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

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

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

【讨论】:

【参考方案2】:

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

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

【讨论】:

【参考方案3】:

菲利普是对的。如果您总是有 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

如果列可能会有所不同,您可以尝试以下方法:

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

【讨论】:

非常感谢您提供的示例。我尝试运行它,显然我需要更新数据库的兼容性级别。一旦完成,我一定会检查一下。谢谢!

以上是关于选择返回动态列的主要内容,如果未能解决你的问题,请参考以下文章

如何在函数中返回动态列数?

根据第三列动态选择两列之一[重复]

动态选择 django orm 列

PostgreSQL 动态列选择

SQL Server:从查询返回的动态列

使用实体框架在运行时动态选择列