问题描述
试试这个:
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 |
----------------------------------------------------------------