简体   繁体   English

SQL查询(T-SQL)

[英]SQL query (T-SQL)

I'm looking for a little assistance trying to write an SQL query because, well... I suck at it. 我正在寻找一个尝试编写SQL查询的帮助,因为,嗯...我很烂。

This is a snippet of what my table looks like: 这是我的表格的摘要:

CUSTOMERID  DEVICEID    STATE
GUID-1      DEVICE-1    UP
GUID-1      DEVICE-2    UP
GUID-1      DEVICE-3    UP
GUID-2      DEVICE-1    DOWN
GUID-2      DEVICE-2    DOWN
GUID-2      DEVICE-3    DOWN
GUID-3      DEVICE-1    UP
GUID-3      DEVICE-2    UP
GUID-3      DEVICE-3    DOWN

In the above example, the customer GUID-1 and GUID-2 have all of their devices UP or all of them DOWN. 在上面的示例中,客户GUID-1和GUID-2的所有设备均为UP或全部为DOWN。 I want to suppress those results from showing in my query. 我想抑制那些结果显示在查询中。 It is customer GUID-3 that I'm trying to find. 我要查找的是客户GUID-3。 This is the customer I want to show in my query results because they have devices that are both UP and DOWN. 这是我要在查询结果中显示的客户,因为他们的设备同时处于UP和DOWN状态。

For what it is worth, all clients have the same number of devices, in the above example 3, but in my live database more than that. 就其价值而言,在上面的示例3中,所有客户端都具有相同数量的设备,但在我的实时数据库中,该数量更多。

Any query writing gurus out there? 任何查询那里写大师?

Chuck 卡盘

If you just want the customerid, then this is a group by query: 如果您只需要customerid,那么这是一个group by查询group by

select customerid
from snippet t
group by customerid
having min(state) <> max(state);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM