【问题标题】:SQL Server 2016: how to get a single row viewSQL Server 2016:如何获取单行视图
【发布时间】:2021-06-09 20:25:53
【问题描述】:

我一直在尝试正确地用这个词,但这是我的dbfiddle

表:

    Customerkey int NOT NULL PRIMARY KEY,
    processdate date NULL,
    CCcount int NULL,
    CHKcount int NULL,
    SACount int NULL
);

INSERT INTO products 
(Customerkey, processdate, CCcount, CHKCount, SACount) 
VALUES
(101,'20210501', 12,3,5),
(102,'20210203', 1,3,1),
(103,'20190412', 4,0,2)

SELECT  CustomerKey,  processdate,
ProductMix=STUFF  
(  
     (  
       SELECT distinct ',' + str(CCcount)  + ',' + str(CHKCount)   +  ',' + str(SACount)
       FROM products t2 
       WHERE t2.CustomerKey = t1.CustomerKey   
       and t2.processdate = t1.processdate
        ),1,1,''  
)  
FROM products t1  
GROUP BY CustomerKey,  processdate

SELECT CustomerKey, processdate,
concat(
Case when CCcount >' ' then 'CCcount' 
when CHKCount > '' then 'CHKCount'
when SACount > '' then 'SACount'
end, '') as Product_Mix_Expanded
from products

预期输出:

CustomerKey processdate Product_Mix Product_Mix_Expanded
101 2021-05-01 12,3,5 CCcount, CHKCount, SACount
102 2021-02-03 1,3,1 CCcount, CHKCount, SACount
103 2019-04-12 4,0,2 CCount, SACount

如您所见,我使用了 STUFF,但不确定这是否是正确的方法。我需要产品组合以文字格式显示计数和扩展的产品组合。 随意提出您需要更多输入的问题。提前谢谢你。

【问题讨论】:

  • 您的链接已损坏。
  • 让我检查一下,试试这个dbfiddle.uk/…
  • worded format == CCount, CHKCount, SACount,如果可能的话,我更喜欢 CCCount + CHKCount + SACount。
  • 不要为您的尝试添加指向小提琴的链接,将它们(实际代码)放在问题中。
  • 好的,让我编辑一下

标签: sql sql-server sql-server-2016


【解决方案1】:

根据您的样本数据和预期结果,以下是否适合您?

select CustomerKey, ProcessDate, Concat(CCcount,',',CHKcount,',',SACount) Product_Mix, 
    Concat(case when CCcount>0 then 'CCcount' else '' end,', ',
    case when CHKcount>0 then 'CHKcount' else '' end, ', ',
    case when SACount>0 then 'SACount' else '' end) Product_Mix_Expanded
from products

【讨论】:

    【解决方案2】:

    通过 XQuery 的另一种方法。

    SQL

    -- DDL and sample data population, start
    DECLARE @tbl TABLE (
        Customerkey int NOT NULL PRIMARY KEY,
        processdate date NULL,
        CCcount int NULL,
        CHKcount int NULL,
        SACount int NULL
    );
    INSERT INTO @tbl (Customerkey, processdate, CCcount, CHKCount, SACount) VALUES
    (101,'20210501', 12,3,5),
    (102,'20210203', 1,3,1),
    (103,'20190412', 4,0,2);
    -- DDL and sample data population, end
    
    SELECT Customerkey, processdate
        , REPLACE(x.query('data(/root/*)').value('text()[1]', 'VARCHAR(100)'), SPACE(1), ',') AS Product_Mix
        , REPLACE(x.query('for $x in /root/*[./text()!="0"]
        return local-name($x)').value('text()[1]', 'VARCHAR(100)'), SPACE(1), ',') AS Product_Mix_Expanded
    FROM @tbl
    CROSS APPLY (
          SELECT CCcount, CHKCount, SACount
          FOR XML PATH(''), TYPE, ROOT('root')) AS t(x);
    

    输出

    +-------------+-------------+-------------+--------------------------+
    | Customerkey | processdate | Product_Mix |   Product_Mix_Expanded   |
    +-------------+-------------+-------------+--------------------------+
    |         101 | 2021-05-01  |      12,3,5 | CCcount,CHKCount,SACount |
    |         102 | 2021-02-03  |       1,3,1 | CCcount,CHKCount,SACount |
    |         103 | 2019-04-12  |       4,0,2 | CCcount,SACount          |
    +-------------+-------------+-------------+--------------------------+
    

    【讨论】:

    • @Anu,很高兴听到建议的解决方案对您有用。很简单,没有CASE语句,没有多余的逗号,列名自动拾取。
    • 这对我有用,我想添加另一个元素(Product_bal),你能否参考一些可以帮助的链接,我发现没有帮助。谢谢。
    • @Anu,不幸的是,如果没有一个最小的可重复示例,就不清楚你在问什么。请提出一个新问题。
    • 好的,很快就会做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2021-06-29
    • 2018-12-13
    相关资源
    最近更新 更多