admin

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

sql

不确定标题是否正确,但是我不太确定该如何写。我有一张表格,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, CircumstanceGroupID,  CircumstanceValue ) VALUES ( 2, 1, 'DEEFOR' )


CREATE TABLE QuestionMaster
(
    QuestionID INT NOT NULL PRIMARY KEY IDENTITY(1,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,1),
    CircumstanceGroupID INT NOT NULL,
    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         | 
----------------------------------------------------------------

阅读 209

收藏
2021-07-01

共1个答案

admin

试试这个:

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(*)
2021-07-01