T-SQL:使用 CASE 和 COALESCE 将多列数据显示到一个输出列中
我有以下针对更改请求数据库运行的 SQL 代码。每条记录都有几列代表受影响的美国地区。如果更改影响某个区域,则该值将为 1,否则为 NULL。
因此,我在每一列中添加值,以确定是否有多个区域受到影响,答案将大于 1。
我需要帮助来更改我的 COALESCE 和/或 CASE 语句,以列出所有受影响的区域在“Affected_Area”输出中值为 1。
过去 7 天受影响的地区
SELECT ID,
(ISNULL(southeast,0) + ISNULL(allregions,0) + ISNULL(midamerica,0) + ISNULL(northcentral,0) + ISNULL(northeast,0) + ISNULL(pacificnorthwest,0) + ISNULL(pacificsouthwest,0)),
Affected_Area = COALESCE(
CASE WHEN [allregions]=1 THEN 'All Regions' ELSE NULL END,
CASE WHEN [midamerica]=1 THEN 'Mid-America' ELSE NULL END,
CASE WHEN [northcentral]=1 THEN 'North Central' ELSE NULL END,
CASE WHEN [northeast]=1 THEN 'Northeast' ELSE NULL END,
CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest' ELSE NULL END,
CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest' ELSE NULL END,
CASE WHEN [southeast]=1 THEN 'Southeast' ELSE NULL END
),
FROM [DB_Reporting].[dbo].change c with (nolock)
WHERE convert(varchar(10),([needed_by_date]),110) BETWEEN (DATEADD(DD,-7,CONVERT(VARCHAR(10),GetDATE(),110))) AND (DATEADD(DD,-1,CONVERT(VARCHAR(10),GetDATE(),110)))
感谢任何帮助!
汉克·斯托林斯
I have the following SQL code that runs against a Change Request database. Each record has several columns that represent affected US regions. If the change affects a region the value will be 1 otherwise NULL.
So I am adding the values in each column to determine if more than one region is affected, the answer will be greater than 1.
I need help in how to make a change to my COALESCE and/or CASE statements to list all affected areas that have a value of 1, in the "Affected_Area" output.
Affected Regions Last Seven Days
SELECT ID,
(ISNULL(southeast,0) + ISNULL(allregions,0) + ISNULL(midamerica,0) + ISNULL(northcentral,0) + ISNULL(northeast,0) + ISNULL(pacificnorthwest,0) + ISNULL(pacificsouthwest,0)),
Affected_Area = COALESCE(
CASE WHEN [allregions]=1 THEN 'All Regions' ELSE NULL END,
CASE WHEN [midamerica]=1 THEN 'Mid-America' ELSE NULL END,
CASE WHEN [northcentral]=1 THEN 'North Central' ELSE NULL END,
CASE WHEN [northeast]=1 THEN 'Northeast' ELSE NULL END,
CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest' ELSE NULL END,
CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest' ELSE NULL END,
CASE WHEN [southeast]=1 THEN 'Southeast' ELSE NULL END
),
FROM [DB_Reporting].[dbo].change c with (nolock)
WHERE convert(varchar(10),([needed_by_date]),110) BETWEEN (DATEADD(DD,-7,CONVERT(VARCHAR(10),GetDATE(),110))) AND (DATEADD(DD,-1,CONVERT(VARCHAR(10),GetDATE(),110)))
Any help is appreciated!
Hank Stallings
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的操作:
您可能需要修剪尾随的逗号。
Try something like this:
You'll likely need to trim a trailing comma.
你有能力改变这张桌子的设计吗?这似乎不太正确 - 您最好使用带有 FK 的区域列到区域表。如果您必须添加另一个区域,您将如何处理上述内容...
如果您不这样做,您可能需要使用 PIVOT 命令并创建一个更加规范化的视图,然后可以使用它来查询。
Do you have any ability to change the design of this table? It seems less than correct - you'd be better with a region column with an FK off to a regions table. What will you do with the above if you have to add another region...
If you don't, you might want to use the PIVOT command and create a more normalised view that you can then use to query.