T-SQL:使用 CASE 和 COALESCE 将多列数据显示到一个输出列中

发布于 2024-08-25 06:39:41 字数 1173 浏览 12 评论 0原文

我有以下针对更改请求数据库运行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最笨的告白 2024-09-01 06:39:41

尝试这样的操作:

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 = 
        CASE WHEN [allregions]=1 THEN 'All Regions, ' ELSE '' END + 
        CASE WHEN [midamerica]=1 THEN 'Mid-America, ' ELSE '' END + 
        CASE WHEN [northcentral]=1 THEN 'North Central, ' ELSE '' END + 
        CASE WHEN [northeast]=1 THEN 'Northeast, ' ELSE '' END + 
        CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest, ' ELSE '' END + 
        CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest, ' ELSE '' END + 
        CASE WHEN [southeast]=1 THEN 'Southeast' ELSE '' 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)))    

您可能需要修剪尾随的逗号。

Try something like this:

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 = 
        CASE WHEN [allregions]=1 THEN 'All Regions, ' ELSE '' END + 
        CASE WHEN [midamerica]=1 THEN 'Mid-America, ' ELSE '' END + 
        CASE WHEN [northcentral]=1 THEN 'North Central, ' ELSE '' END + 
        CASE WHEN [northeast]=1 THEN 'Northeast, ' ELSE '' END + 
        CASE WHEN [pacificnorthwest]=1 THEN 'Pacific Northwest, ' ELSE '' END + 
        CASE WHEN [pacificsouthwest]=1 THEN 'Pacific Southwest, ' ELSE '' END + 
        CASE WHEN [southeast]=1 THEN 'Southeast' ELSE '' 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)))    

You'll likely need to trim a trailing comma.

梦魇绽荼蘼 2024-09-01 06:39:41

你有能力改变这张桌子的设计吗?这似乎不太正确 - 您最好使用带有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文