SQL如何查找非空列?

发布于 2024-08-03 12:34:25 字数 192 浏览 5 评论 0原文

我有一个有很多列的表,假设我有列

A、B、C、D

中,任何一条记录中仅填充一列,其他列始终为 NULL。

我需要一个 select 语句来返回非空列的列。

我尝试过合并,但这返回一个值,而不是该值所属的列。

有人知道最简单的方法吗?

I have a table with lots of columns, say I have columns

A, B, C, D

in each of these columns, only one column in any one record will be filled and the others will always be NULL.

I need a select statement that will return the Column of the non null Column.

I've tried coalesce, but this return a value, not the column to which the value belongs to.

Anyone know the simplest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

街角迷惘 2024-08-10 12:34:25
SELECT
    CASE
        WHEN A IS NOT NULL THEN 'A'
        WHEN B IS NOT NULL THEN 'B'
        WHEN C IS NOT NULL THEN 'C'
        WHEN D IS NOT NULL THEN 'D'
    END
FROM
    MyTable
SELECT
    CASE
        WHEN A IS NOT NULL THEN 'A'
        WHEN B IS NOT NULL THEN 'B'
        WHEN C IS NOT NULL THEN 'C'
        WHEN D IS NOT NULL THEN 'D'
    END
FROM
    MyTable
带刺的爱情 2024-08-10 12:34:25

尝试案例...

SELECT
CASE WHEN A IS NOT NULL THEN 'A' WHEN B IS NOT NULL THEN 'B' WHEN C IS NOT NULL THEN 'C' WHEN D IS NOT NULL THEN 'D' END as NotNullCol, OtherCols
FROM YourTable

Try case...

SELECT
CASE WHEN A IS NOT NULL THEN 'A' WHEN B IS NOT NULL THEN 'B' WHEN C IS NOT NULL THEN 'C' WHEN D IS NOT NULL THEN 'D' END as NotNullCol, OtherCols
FROM YourTable
清秋悲枫 2024-08-10 12:34:25

每当您发现自己尝试使用多列集执行操作时,您的架构可能是错误的。

几乎可以肯定,将 A、B、C 和 D 分成单独表中的单独行,将它们绑定回原始表中的行并创建 JOIN 类型查询会更容易。

或者,如果只有一列非 NULL,我会选择两列:类型(A、B、C 或 D)和值。这样您就不会浪费每一行中的列,并且查询会变得更加容易(假设类型相同)。

但是,您可以使用case这样做:

select case
    when A is not null then 'A'
    when B is not null then 'B'
    when C is not null then 'C'
    else                    'D'
    end
from ...

语法可能不完全正确,您需要查找它。这不是我通常做的事情,因为我认为这是一个坏主意(select 中的每行函数永远无法很好地扩展)。

Whenever you find yourself trying to do things with multi-column sets, you've probably got your schema wrong.

It would almost certainly be easier to separate A, B, C and D into separate rows in a separate table, tie them back to the row in the original table and create a JOIN-type query.

Alternatively, if only one is ever non-NULL, I would opt for two columns, type (A, B, C or D) and value. Then you're not wasting the columns in every row, and the queries are immeasurably easier (assuming the types are the same).

However, you can do it this way with case:

select case
    when A is not null then 'A'
    when B is not null then 'B'
    when C is not null then 'C'
    else                    'D'
    end
from ...

The syntax may not be exactly correct, you'll need to look it up. IIt's not something I usually do since I consider it a bad idea (per-row functions in select never scale well).

森林散布 2024-08-10 12:34:25

不漂亮,但这就是你想要的:

select case 
    when a is not null then 'a' 
    when b is not null then 'b' 
    when c is not null then 'c' 
    when d is not null then 'd' 
end

Not pretty but this does what you want:

select case 
    when a is not null then 'a' 
    when b is not null then 'b' 
    when c is not null then 'c' 
    when d is not null then 'd' 
end
奢欲 2024-08-10 12:34:25

我会重新设计。事实证明,以这种方式存储数据而不是在相关表中存储数据几乎是一个坏主意。

在相关表中您可以更方便地查询信息。您还可以更轻松地对数据施加约束,仅允许一条记录,因此您可能会获得更好的数据完整性。

更少的表并不一定意味着关系数据库中的访问速度更快或设计更好。设计合理且索引良好的相关表通常相当不错。虽然您可能认为永远不需要第五列,但业务规则发生了变化,而且我看到了许多人们认为不需要扩展的东西,结果却需要可扩展性。

I would redesign. It almost alawys turns out to be a bad idea to store data this way instead of in a related table.

In a related table you can more easily query the information. You can also more easily put a constraint on the data that only one record is allowed, so you will will probably have better data integrity.

Fewer tables does not necessarily mean faster access or better design in relational databases. Porperly designed and indexed related tables are generally quite good. And while you may think you won't ever need a fifth column, business rules change and I've seen many things that people thought wouldn't need expansion that turned out to need the scalibilty.

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