使用AVG和count的复杂SQL查询

huangapple go评论102阅读模式
英文:

Complex SQL Query using AVG and count

问题

1: 显示所有租金高于其所属公司平均租金值的住宅。

2: 显示注册时间在0-3个月、4-6个月、6-12个月、1-2年和2年以上的住宅数量。

英文:

I am looking for some help with two queries using the following table/data. It would be great to see the most efficient way to achieve these.

Queries

1: Show all homes with rent above the average rent value for their Company

2: Show the number of homes registered 0-3 months, 4-6 months, 6-12 months, 1- 2 years, and 2+ years ago

Thank you for looking.

  1. CREATE SCHEMA homes;
  2. GO
  3. CREATE TABLE homes.Companies (CompanyId INT, CompanyName NVARCHAR(50));
  4. INSERT INTO homes.Companies VALUES
  5. (1,'Company A')
  6. ,(2,'Company B')
  7. ,(3,'Company C')
  8. ,(4,'Company D')
  9. CREATE TABLE homes.Branches(BranchId INT, CompanyId INT, BranchName NVARCHAR(20));
  10. INSERT INTO homes.Branches VALUES
  11. (1,1,'Liverpool')
  12. ,(2,1,'Blackpool')
  13. ,(3,1,'Stockport')
  14. ,(4,2,'Durham')
  15. ,(5,2,'Oxford')
  16. ,(6,2,'Cambridge')
  17. ,(7,2,'Manchester')
  18. ,(8,3,'Newcastle')
  19. ,(9,3,'Hull')
  20. ,(10,3,'York')
  21. ,(11,4,'Birmingham')
  22. ,(12,4,'Stoke on Trent')
  23. -- DROP TABLE homes.Properties;
  24. CREATE TABLE homes.Properties (PropertyId INT, BranchId INT, Address NVARCHAR(200), Rent INT, RegisteredOn DATETIME);
  25. INSERT INTO homes.Properties VALUES
  26. (1,1,'12a, Station Street, Liverpool',500,CAST('2019-12-31' AS DATETIME))
  27. ,(2,1,'12c, Station Street, Liverpool',475,CAST('2021-06-15' AS DATETIME))
  28. ,(3,3,'39, Main Road, Stockport',410,CAST('2022-09-30' AS DATETIME))
  29. ,(4,3,'12, Dovetrees, Manchester',700,CAST('2022-08-19' AS DATETIME))
  30. ,(5,5,'16, St Giles, Oxford',825,CAST('2023-02-28' AS DATETIME))
  31. ,(6,5,'32, George Street, Oxford',1100,CAST('2022-04-01' AS DATETIME))
  32. ,(7,5,'16, Main Road, Eynsham',680,CAST('2021-03-24' AS DATETIME))
  33. ,(8,6,'3b, University Road, Cambridge',960,CAST('2022-01-14' AS DATETIME))
  34. ,(9,7,'98, Heaton Road, Manchester',795,CAST('2022-09-07' AS DATETIME))
  35. ,(10,8,'64, St James Park, Newcastle',550,CAST('2022-11-26' AS DATETIME))
  36. ,(11,8,'47, St James Park, Newcastle',630,CAST('2023-01-16' AS DATETIME))
  37. ,(12,10,'6b, Station Lane, York',520,CAST('2022-07-19' AS DATETIME))
  38. ,(13,10,'24, Newcastle Road, York',510,CAST('2022-03-15' AS DATETIME))
  39. ,(14,11,'64, Bull Ring, Birmingham',1025,CAST('2022-12-03' AS DATETIME))
  40. ,(15,12,'Willowmead, Pottery Lane, Stoke on Trent',765,CAST('2022-05-12' AS DATETIME))

答案1

得分: 1

  1. 显示所有租金高于其公司平均租金值的住宅:
  1. SELECT p.PropertyId, b.BranchName, p.Address, p.Rent
  2. FROM homes.Properties p
  3. JOIN homes.Branches b ON p.BranchId = b.BranchId
  4. JOIN (
  5. SELECT c.CompanyId, AVG(p.Rent) AS AvgRent
  6. FROM homes.Properties p
  7. JOIN homes.Branches b ON p.BranchId = b.BranchId
  8. JOIN homes.Companies c ON b.CompanyId = c.CompanyId
  9. GROUP BY c.CompanyId
  10. ) AS subquery ON b.CompanyId = subquery.CompanyId
  11. WHERE p.Rent > subquery.AvgRent;

上述查询将Properties表与Branches表连接,然后与一个子查询连接,该子查询计算每个公司的平均租金。它仅选择租金高于其公司平均租金的属性。

  1. 显示0-3个月、4-6个月、6-12个月、1-2年和2年以上之前注册的住宅数量:
  1. SELECT
  2. CASE
  3. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 3 THEN '0-3 months'
  4. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 6 THEN '4-6 months'
  5. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 12 THEN '6-12 months'
  6. WHEN DATEDIFF(YEAR, p.RegisteredOn, GETDATE()) <= 2 THEN '1-2 years'
  7. ELSE '2+ years'
  8. END AS TimeRange,
  9. COUNT(*) AS HomeCount
  10. FROM homes.Properties p
  11. GROUP BY
  12. CASE
  13. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 3 THEN '0-3 months'
  14. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 6 THEN '4-6 months'
  15. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) <= 12 THEN '6-12 months'
  16. WHEN DATEDIFF(YEAR, p.RegisteredOn, GETDATE()) <= 2 THEN '1-2 years'
  17. ELSE '2+ years'
  18. END;

使用DATEDIFF函数计算每个属性的RegisteredOn日期与当前日期GETDATE()之间的月份/年份差异。然后,根据计算出的时间范围对属性进行分组,并统计每个范围内的属性数量。

英文:
  1. To show all homes with rent above the average rent value for their
    Company :
  1. SELECT p.PropertyId, b.BranchName, p.Address, p.Rent
  2. FROM homes.Properties p
  3. JOIN homes.Branches b ON p.BranchId = b.BranchId
  4. JOIN (
  5. SELECT c.CompanyId, AVG(p.Rent) AS AvgRent
  6. FROM homes.Properties p
  7. JOIN homes.Branches b ON p.BranchId = b.BranchId
  8. JOIN homes.Companies c ON b.CompanyId = c.CompanyId
  9. GROUP BY c.CompanyId
  10. ) AS subquery ON b.CompanyId = subquery.CompanyId
  11. WHERE p.Rent &gt; subquery.AvgRent;

The query above joins the Properties table with the Branches table and then joins with a subquery which calculates the average rent per company.
It selects only the properties where the rent is above the average rent for their company.

  1. To show the number of homes registered 0-3 months, 4-6 months, 6-12
    months, 1-2 years, and 2+ years ago
  1. SELECT
  2. CASE
  3. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 3 THEN &#39;0-3 months&#39;
  4. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 6 THEN &#39;4-6 months&#39;
  5. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 12 THEN &#39;6-12 months&#39;
  6. WHEN DATEDIFF(YEAR, p.RegisteredOn, GETDATE()) &lt;= 2 THEN &#39;1-2 years&#39;
  7. ELSE &#39;2+ years&#39;
  8. END AS TimeRange,
  9. COUNT(*) AS HomeCount
  10. FROM homes.Properties p
  11. GROUP BY
  12. CASE
  13. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 3 THEN &#39;0-3 months&#39;
  14. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 6 THEN &#39;4-6 months&#39;
  15. WHEN DATEDIFF(MONTH, p.RegisteredOn, GETDATE()) &lt;= 12 THEN &#39;6-12 months&#39;
  16. WHEN DATEDIFF(YEAR, p.RegisteredOn, GETDATE()) &lt;= 2 THEN &#39;1-2 years&#39;
  17. ELSE &#39;2+ years&#39;
  18. END;

The use of DATEDIFF function is to calculate the difference in months/years between the RegisteredOn date of each property and the current date GETDATE(). Then, it groups the properties based on the calculated time range and counts the number of properties in each range.

huangapple
  • 本文由 发表于 2023年5月30日 03:12:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定