微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQL Server-删除组中带有NULL的结果

如何解决SQL Server-删除组中带有NULL的结果

试试这个:

select CircumstanceGroupID 
from CircumstanceMaster cm 
left join @given g on cm.CircumstanceType=g.CircumstanceTypeGivenID and cm.CircumstanceValue=g.CircumstanceValueGiven
group by CircumstanceGroupID
having sum(case when CircumstanceValueGiven is null then 0 else 1 end) = count(*)

解决方法

不确定标题是否正确,但是我不太确定该如何写。我有一张表格,circumstances我需要根据circumstances给定的传入数据组显示某些问题。

我传入的数据如下所示:

----------------------------------------------------
| CircumstanceTypeGiven | CircumstanceValueGiven |
----------------------------------------------------
|      Equipment        |           X            |
----------------------------------------------------
|      Customer         |        BEEFORE         |
----------------------------------------------------

我的CircumstanceMaster桌子看起来像这样:

--------------------------------------------------
| ID | CircumstanceType  |  GroupID  |  Value    |
--------------------------------------------------
| 1  |    Equipment      |    1      |  Reefer   |
--------------------------------------------------
| 2  |    Customer       |    1      |  BEEFOR   |
--------------------------------------------------

然后,我得到了一个表格,其中包含这样的组的问题ID:

--------------------------------------------------
| ID | CircumstanceGroupID  |    QuestionID      |
--------------------------------------------------
| 1  |         1            |         1          |
--------------------------------------------------

我的问题表:

--------------------------------------------------
| ID | Question                                  |
--------------------------------------------------
| 1  |  This is my question for Reefer & BEEFOR  |
--------------------------------------------------

因此,我获得了 X 的设备类型和 BEEFOR 的客户价值。我的情况表说,如果我通过了 reefer BEEFORE
,那么我需要得到1的问题groupID。但是,如果只传入1(我给出的情况),我不希望这些问题。

这是SQL:

CREATE TABLE CircumstanceMaster
(
    CircumstanceID INT NOT NULL PRIMARY KEY IDENTITY(1,1),CircumstanceGroupID INT NOT NULL,CircumstanceType INT NOT NULL,CircumstanceValue VARCHAR(MAX) NOT NULL
)
INSERT INTO CircumstanceMaster ( CircumstanceType,CircumstanceGroupID,CircumstanceValue ) VALUES ( 1,1,'R' )
INSERT INTO CircumstanceMaster ( CircumstanceType,CircumstanceValue ) VALUES ( 2,'DEEFOR' )


CREATE TABLE QuestionMaster
(
    QuestionID INT NOT NULL PRIMARY KEY IDENTITY(1,Question VARCHAR(MAX) NOT NULL
)
INSERT INTO QuestionMaster ( Question ) VALUES ( 'Reefer & DEEFOR question' )


CREATE TABLE CircumstanceQuestion
(
    ID INT NOT NULL PRIMARY KEY IDENTITY(1,QuestionID INT NOT NULL
)
INSERT INTO CircumstanceQuestion ( CircumstanceGroupID,QuestionID  ) VALUES ( 1,1 )

declare @given Table(CircumstanceTypeGivenID INT,CircumstanceValueGiven varchar(50))
insert into @given(CircumstanceTypeGivenID,CircumstanceValueGiven) VALUES ( 1,'X')
insert into @given(CircumstanceTypeGivenID,CircumstanceValueGiven) VALUES ( 2,'DEEFOR')



select *
from CircumstanceMaster cm
    left outer join @given g on cm.CircumstanceType=g.CircumstanceTypeGivenID and cm.CircumstanceValue=g.CircumstanceValueGiven

当我加入circumstancemaster餐桌时,我得到了这个结果。我希望得到结果,但是我需要一种方式说“如果SAME组中的任何行具有空值,请不要返回任何内容”:

----------------------------------------------------------------
| CircumstanceID | CircumstanceGroupID  | CircumstanceValueGiven|
----------------------------------------------------------------
|       1        |           1          |         NULL          |
----------------------------------------------------------------
|       2        |           1          |        BEEFOR         | 
----------------------------------------------------------------

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。