所以我必须编写一个查询,显示城市,平均要价和房产数量,以及超过100个房产的城市的总数。我有代码:
SELECT Property.City, '$' + CONVERT(NVARCHAR, AVG(Listing.AskingPrice),1) AS 'Avg. Asking Price', COUNT(Property.PropertyID) AS 'No. of Properties'
FROM Property
INNER JOIN Listing
ON Property.PropertyID = Listing.PropertyID
GROUP BY Property.City
HAVING COUNT(Property.PropertyID) > 100
但是,结果应该有3行Arcata,Eureka和NULL。我只是让前两个出现..我如何制作它以便我的代码也计算空值?
答案 0 :(得分:0)
INNER JOIN忽略匹配的NULL值。只需尝试以下示例进行验证:
DECLARE @T1 TABLE
(
a int,
b int
)
DECLARE @T2 TABLE
(
a int,
b int
)
INSERT INTO @T1 (a,b)
VALUES(1,2)
INSERT INTO @T1 (a,b)
VALUES(NULL,3)
INSERT INTO @T2 (a,b)
VALUES(1,2)
INSERT INTO @T2 (a,b)
VALUES(NULL,3)
SELECT *
FROM @T1 AS T1
INNER JOIN @T2 AS T2 ON T1.a = T2.a
如果要将结果集设为NULL,请改用LEFT JOIN
DECLARE @T1 TABLE
(
a int,
b int
)
DECLARE @T2 TABLE
(
a int,
b int
)
INSERT INTO @T1 (a,b)
VALUES(1,2)
INSERT INTO @T1 (a,b)
VALUES(NULL,3)
INSERT INTO @T2 (a,b)
VALUES(1,2)
INSERT INTO @T2 (a,b)
VALUES(NULL,3)
SELECT *
FROM @T1 AS T1
LEFT JOIN @T2 AS T2 ON T1.a = T2.a