Sql server 选择满足条件的所有值和一些附加值

Sql server 选择满足条件的所有值和一些附加值,sql-server,select,conditional-statements,Sql Server,Select,Conditional Statements,我有一个带有字段的表: | Accompanying measures (bool) | Category (String) | Instrument (String) | 因为应用程序的设计并不是要真正替换这些,所以我使用ValidUntil Date类型字段更改了table Instrument,该字段的日期要么是不推荐使用的日期,要么是NULL(如果仍然有效)。 视图显示旧数据和新数据的混合。旧数据可能包含已弃用的工具,而新数据则不会。 如果我只显示新仪器ValidUntil为空,我将中

我有一个带有字段的表:

| Accompanying measures (bool) | Category (String) | Instrument (String) |
因为应用程序的设计并不是要真正替换这些,所以我使用ValidUntil Date类型字段更改了table Instrument,该字段的日期要么是不推荐使用的日期,要么是NULL(如果仍然有效)。 视图显示旧数据和新数据的混合。旧数据可能包含已弃用的工具,而新数据则不会。 如果我只显示新仪器ValidUntil为空,我将中断页面,因为有选定的伴随测量无法显示,因为它们无法从数据库检索

Tl;博士 编辑:如何选择所有ValidUntil为空以及ValidUntil!=NULL,但hasbeenselectedasaccompanymeasure为true


如果我理解您的问题,您必须使用UNION ALL/UNION组合两种不同条件的数据集

--ValidUntil IS NULL
SELECT
    a.AccompanyingMeasureID, a.InstrumentID, a.TaskID,         
    a.HasBeenSelectedAsAccompanyingMeasure, a.CategoryID
    , i.Name AS InstrumentName,     ic.Name AS CategoryName
FROM wc_AccompanyingMeasures AS a 
INNER JOIN wc_Instruments AS i ON a.InstrumentID = i.ID 
INNER JOIN wc_InstrumentCategories AS ic ON i.CategoryID = ic.ID
WHERE (a.TaskID = @TaskID) 
AND (i.ValidUntil IS NULL) 
UNION ALL
--ValidUntil != NULL, but HasBeenSelectedAsAccompanyingMeasure is true
SELECT
    a.AccompanyingMeasureID, a.InstrumentID, a.TaskID,         
    a.HasBeenSelectedAsAccompanyingMeasure, a.CategoryID
    , i.Name AS InstrumentName,     ic.Name AS CategoryName
FROM wc_AccompanyingMeasures AS a 
INNER JOIN wc_Instruments AS i ON a.InstrumentID = i.ID 
INNER JOIN wc_InstrumentCategories AS ic ON i.CategoryID = ic.ID
WHERE (a.TaskID = @TaskID) 
AND (i.ValidUntil IS NOT NULL AND HasBeenSelectedAsAccompanyingMeasure='TRUE')

现在还不清楚你到底在找什么。你能在TL:DR之后澄清一下位吗?
--ValidUntil IS NULL
SELECT
    a.AccompanyingMeasureID, a.InstrumentID, a.TaskID,         
    a.HasBeenSelectedAsAccompanyingMeasure, a.CategoryID
    , i.Name AS InstrumentName,     ic.Name AS CategoryName
FROM wc_AccompanyingMeasures AS a 
INNER JOIN wc_Instruments AS i ON a.InstrumentID = i.ID 
INNER JOIN wc_InstrumentCategories AS ic ON i.CategoryID = ic.ID
WHERE (a.TaskID = @TaskID) 
AND (i.ValidUntil IS NULL) 
UNION ALL
--ValidUntil != NULL, but HasBeenSelectedAsAccompanyingMeasure is true
SELECT
    a.AccompanyingMeasureID, a.InstrumentID, a.TaskID,         
    a.HasBeenSelectedAsAccompanyingMeasure, a.CategoryID
    , i.Name AS InstrumentName,     ic.Name AS CategoryName
FROM wc_AccompanyingMeasures AS a 
INNER JOIN wc_Instruments AS i ON a.InstrumentID = i.ID 
INNER JOIN wc_InstrumentCategories AS ic ON i.CategoryID = ic.ID
WHERE (a.TaskID = @TaskID) 
AND (i.ValidUntil IS NOT NULL AND HasBeenSelectedAsAccompanyingMeasure='TRUE')