3

我的查询如下

SELECT SupplierId,StakeholderSupplierId,Percentage
FROM std_Stakeholders    
ORDER BY SupplierId

这产生了如下结果

SupplierId        StakeholderSupplierId             Percentage
1                          3175                        68.00
2929                       5504                        25.00
5504                        1                          68.25
5504                       3238                        50.00
5504                       2810                        23.00

我想将此输出转换为 .

  SupplierId    StakeholderSupplierId1  Percentage  StakeholderSupplierId2  Percentage StakeholderSupplierId3   Percentage
    1                    3175            68.00  
    2929                 5504            25.00
    5504                  1              68.68             3238                50.00                2810            23.00

我尝试使用 joins 和 pivoting 。无法成功编写正确的查询以获得所需的输出。谁能帮我吗

注意:StakeholderSupplierId 不是静态的。它可能从 1 到 n 不等

4

3 回答 3

1

我不知道这是否是您问题的解决方案(取决于您在此查询后需要做什么),但对于此类问题,我使用了 MySQLGROUP_CONCAT函数。对于 SQL Server 中的类似解决方案,请查看问题。

使用GROUP_CONCAT函数,您可以获得如下结果表:

SupplierId    StakeholderSupplierIds  Percentages  
1             3175                    68.00  
2929          5504                    25.00
5504          1, 3238, 2810           68.68, 50.00, 23.00
于 2012-04-06T07:57:29.900 回答
0

好吧,您可以通过两步过程来做到这一点,但同样我希望它可以通过PIVOT- 但是作为示例(最后一步的旧式方法):

declare @stakeholdersForPivot table
(
  SupplierId int,
  StakeholderId int,
  StakeholderSupplierId int,
  Percentage decimal(19,2)
)

; with orderedStakeholders as
(
  select SupplierId
  , ROW_NUMBER() over (partition by SupplierId order By StakeholderSupplierId) StakeholderId
  ,StakeholderSupplierId, Percentage
  from std_Stakeholders

)

insert into @stakeholdersForPivot (SupplierId, StakeholderId, StakeholderSupplierId, Percentage)
select SupplierId, StakeholderId, StakeholderSupplierId, Percentage
from orderedStakeholders

select SupplierId
, sum(case when StakeholderId = 1 then StakeholderSupplierId else null end) StakeholderSupplierId1
, sum(case when StakeholderId = 1 then Percentage else null end) Percentage1
, sum(case when StakeholderId = 2 then StakeholderSupplierId else null end) StakeholderSupplierId2
, sum(case when StakeholderId = 2 then Percentage else null end) Percentage2
, sum(case when StakeholderId = 3 then StakeholderSupplierId else null end) StakeholderSupplierId3
, sum(case when StakeholderId = 3 then Percentage else null end) Percentage3
from @stakeholdersForPivot
group by SupplierId
order by SupplierId

显然,您必须明确说明您期望每个供应商的利益相关者供应商的最大数量,但您可以根据需要扩展它。

于 2012-04-06T07:40:48.053 回答
0

这是使用“GROUP BY MAX”技巧的查询。应该跨数据库工作。缺点是列列表是静态的。

SELECT
    supplierid,
    max(StakeholderSupplierId1) AS StakeholderSupplierId1,
    max(percentage1) AS percentage,
    max(StakeholderSupplierId2) AS StakeholderSupplierId2,
    max(percentage2) AS percentage,
    max(StakeholderSupplierId3) AS StakeholderSupplierId3,
    max(percentage3) AS percentage
FROM
    (
        SELECT
            supplierid,
            CASE WHEN ranking = 1 THEN stakeholdersupplierid END AS StakeholderSupplierId1,
            CASE WHEN ranking = 1 THEN percentage END AS percentage1,
            CASE WHEN ranking = 2 THEN stakeholdersupplierid END AS StakeholderSupplierId2,
            CASE WHEN ranking = 2 THEN percentage END AS percentage2,
            CASE WHEN ranking = 3 THEN stakeholdersupplierid END AS StakeholderSupplierId3,
            CASE WHEN ranking = 3 THEN percentage END AS percentage3
        FROM
            (
                SELECT
                    supplierid,
                    stakeholdersupplierid,
                    percentage,
                    rank() OVER (PARTITION BY supplierid ORDER BY percentage DESC) AS ranking
                FROM
                    std_stakeholders
            ) AS t
    ) AS t
GROUP BY
    supplierid
;
于 2012-04-06T09:57:20.360 回答