如何知道某列中所有单元格是否具有相同的值
如何知道某些列中的所有单元格是否具有相同的值(标题已更改)
我想要一个标量值来告诉我列中的所有值是否等于某个值:
DECLARE @bit bit
SELECT @bit = TRUEFORALL(Name IS NOT NULL) FROM Contact
更新
我现在意识到我实际上不需要 TrueForAll,我需要的是确保列中的所有值都相等,例如,我想知道所有 Group.Items 是否具有相同的价格。
How to know if all the cells have the same value in some column (title changed)
I want to have a bit scalar value that tells me if all the values in a column equal something:
DECLARE @bit bit
SELECT @bit = TRUEFORALL(Name IS NOT NULL) FROM Contact
UPDATE
I now realized that I actually don't need the TrueForAll, what I do need is to make sure, that all values in a column are equal, for example, I want to know whether all Group.Items have the same price.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为什么不呢?
如果返回 1,则所有值都相同...
添加
where Price is not null
如果需要,
Why not?
If returns 1, all values are the same... Add
where price is not null
if need be
对于您更新的要求,类似这样的事情似乎可以满足您的要求:
当计数大于 1 时,您有两个不同的名称(或价格取决于您分组的内容)
For your updated requirement something like this would appear to do what you want:
When the count is greater the 1 you have two different names (or prices depending on what you group on)
对于 NULL 不太好,但 2008 可以做到:
OR
UPDATE
所有相同的颜色
Not very good for NULLs, but 2008 can do:
OR
UPDATE
All same color
也许是这个?
Maybe this?
这解决了您的第一个问题:
添加:
这将解决您的第二个问题:
顺便说一下,当您使用存在函数时,最好选择 1 (常量),以便返回更少的数据
This solves your first question:
ADDED:
This will solve your second:
By the way, when you use the exists function, its better to SELECT 1 (a constant) so less data gets returned