如何在使用范围时显示包括null的所有值

时间:2017-01-13 12:50:31

标签: c# asp.net sql-server

我正在开发一个diagonistic bill centre management的项目,用户将从日期和日期中选择,然后所有测试列表将显示其请求数量和请求的总费用是否请求了这些测试在特定的日期范围之间。如果在该日期范围之间没有请求任何特定测试,那么对于该测试请求no和费用将为零,当我指定日期范围时,仅显示所请求的日期范围,而不是所有测试列表,任何人都可以帮助请如何也显示带有日期范围的空值,我也使用完全外部连接但不起作用

结果应该是这样的,这是没有范围的

results

更新结果

updated result

更新了已解决的查询

Updated solved query

2 个答案:

答案 0 :(得分:0)

我认为这就是你想要的,在这个例子中,我只使用了两个表,但如果需要,你可以将它扩展到更多。

DECLARE @testType TABLE(    
TestID INT,
TestName varchar(128)

DECLARE @testRequest TABLE( 
TestRequestID INT,
TestID INT,
TestCost INT,
TestDate DATETIME

INSERT INTO @testType (TestID,TestName) VALUES (1, 'Bone');
INSERT INTO @testType (TestID,TestName) VALUES (2, 'Chest');
INSERT INTO @testType (TestID,TestName) VALUES (3, 'Head');
INSERT INTO @testType (TestID,TestName) VALUES (4, 'Infection');

INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (1, 1, 10, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (2, 1, 10, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (3, 2, 20, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 2, 20, GETDATE())


SELECT 
TT.TestName, 
COUNT(TR.TestRequestID) [TestCount],   
ISNULL(SUM(TR.TestCost),0) [TotalCost]
FROM @testType TT
LEFT JOIN @testRequest TR ON TT.TestID = TR.TestID
AND TR.TestDate BETWEEN GETDATE() AND GETDATE()
GROUP BY TT.TestName;

答案 1 :(得分:0)

下面是代码的另一个版本,包含结果和更多测试。

DECLARE @testType TABLE(    
TestID INT,
TestName varchar(128)
)

DECLARE @testRequest TABLE( 
TestRequestID INT,
TestID INT,
TestCost INT,
TestDate DATETIME
)

INSERT INTO @testType (TestID,TestName) VALUES (1, 'Bone');
INSERT INTO @testType (TestID,TestName) VALUES (2, 'Chest');
-- These 2 test will no get a match and will be null
INSERT INTO @testType (TestID,TestName) VALUES (3, 'Head');
INSERT INTO @testType (TestID,TestName) VALUES (4, 'Infection');
INSERT INTO @testType (TestID,TestName) VALUES (5, 'Leg');

INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (1, 1, 10, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (2, 1, 10, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (3, 2, 20, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 2, 20, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 2, 20, GETDATE())
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 2, 20, GETDATE())

-- TestID 3 will get a 1 match here 
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 3, 50, GETDATE())

-- TestID 3 will not get a match b/c of the date 
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 3, 50, '2015-01-01')

-- TestID 4 will not get a match b/c of the date, null record 
INSERT INTO @testRequest (TestRequestID, TestID, TestCost, TestDate) VALUES (4, 4, 100, '2018-01-01')

-- TestID 5 will not get a match b/c none one scheduled it yet, null record 


SELECT 
TT.TestName, 
COUNT(TR.TestRequestID) [TestCount],   
ISNULL(SUM(TR.TestCost),0) [TotalCost]
FROM @testType TT
LEFT JOIN @testRequest TR ON TT.TestID = TR.TestID
AND TR.TestDate BETWEEN GETDATE() AND GETDATE()
GROUP BY TT.TestName;

/*  
Results:
    Bone        2   20
    Chest       4   80
    Head        1   50
    Infection   0   0
    Leg         0   0   
*/


-- To see null values run the query below
SELECT 
*
FROM @testType TT
LEFT JOIN @testRequest TR ON TT.TestID = TR.TestID
AND TR.TestDate BETWEEN GETDATE() AND GETDATE()

/*  
Results:
1   Bone    1   1   10  2017-01-16 20:17:31.030
1   Bone    2   1   10  2017-01-16 20:17:31.030
2   Chest   3   2   20  2017-01-16 20:17:31.030
2   Chest   4   2   20  2017-01-16 20:17:31.030
2   Chest   4   2   20  2017-01-16 20:17:31.030
2   Chest   4   2   20  2017-01-16 20:17:31.030
3   Head    4   3   50  2017-01-16 20:17:31.030
4   Infection   NULL    NULL    NULL    NULL
5   Leg NULL    NULL    NULL    NULL
*/