公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了SQL Server 2016:如何获取单行视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图正确地表达这一点,但以下是我的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 处理日期 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 计数、存储计数

如您所见,我使用了一些东西,但不确定这是否是正确的方法。我需要的产品组合显示的计数和产品组合在文字格式扩展。
如果您需要更多的信息,请随时提问。提前谢谢您。

推荐答案

根据您的样本数据和预期结果,您是否可以执行以下操作?

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

这篇关于SQL Server 2016:如何获取单行视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:errorSQLServer2016:howtogetasinglerowview exception IT资讯 SQL sql-serve
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定