将一个SQL查询的结果与另一个SQL查询的结果合并为一个表。

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

Combine the results one SQL query to the results of another SQL query to make one table

问题

我正在使用SQL Server,我有一些单独工作的查询,但我想将它们合并到一个表中。

我尝试将第一个查询与第二个连接,但我不确定正确的语法。

第一个查询:

  1. SELECT Store, ROUND(AVG(Unemployment),2) AS 平均失业率,
  2. ROUND(AVG(CPI),2) AS 平均CPI,
  3. ROUND(AVG(Temperature),2) AS 平均温度
  4. FROM PortfolioProjects..features
  5. GROUP BY Store
  6. ORDER BY Store

第二个查询:

  1. SELECT tra.Store, sto.Type, sto.Size,
  2. ROUND(SUM(CASE WHEN IsHoliday = 1 OR IsHoliday = 0
  3. THEN Weekly_Sales
  4. END),2) AS 总销售额,
  5. ROUND(SUM(CASE WHEN IsHoliday = 1
  6. THEN Weekly_Sales
  7. END),2) AS 假日销售额,
  8. ROUND(SUM(CASE WHEN IsHoliday = 0
  9. THEN Weekly_Sales
  10. END),2) AS 非假日销售额
  11. FROM PortfolioProjects..train tra, PortfolioProjects..stores sto
  12. WHERE tra.Store = sto.Store
  13. GROUP BY tra.Store, sto.Size, sto.Type

请注意,这只是查询的中文翻译部分,没有其他内容。

英文:

I'm using SQL Server and I have a couple of queries that work individually, but I would like to combine them together into one table.

I've tried to take the first query and join it with the 2nd, but I'm not sure of the right syntax.

  1. SELECT Store, ROUND(AVG(Unemployment),2) AS avg_unempl,
  2. ROUND(AVG(CPI),2) AS avg_CPI,
  3. ROUND(AVG(Temperature),2) AS avg_temp
  4. FROM PortfolioProjects..features
  5. GROUP BY Store
  6. ORDER BY Store
  7. Store avg_unempl avg_CPI avg_temp
  8. 1 7.44 217.27 66.91
  9. 2 7.4 216.92 66.73
  10. 3 7.01 220.69 70.39
  11. 4 5.65 129.2 61.42
  12. 5 6.16 217.84 68.22
  13. 6 6.41 218.84 68.5
  14. SELECT tra.Store, sto.Type, sto.Size,
  15. ROUND(SUM(CASE WHEN IsHoliday = 1 OR IsHoliday = 0
  16. THEN Weekly_Sales
  17. END),2) AS tot_sales,
  18. ROUND(SUM(CASE WHEN IsHoliday = 1
  19. THEN Weekly_Sales
  20. END),2) AS hol_sales,
  21. ROUND(SUM(CASE WHEN IsHoliday = 0
  22. THEN Weekly_Sales
  23. END),2) AS non_hol_sales
  24. FROM PortfolioProjects..train tra, PortfolioProjects..stores sto
  25. WHERE tra.Store = sto.Store
  26. GROUP BY tra.Store, sto.Size, sto.Type
  27. Store Type Size tot_sales hol_sales non_hol_sales
  28. 1 A 151315 222402808.85 16657476.56 205745332.29
  29. 2 A 202307 275382440.98 20792669 254589771.98
  30. 3 B 37392 57586735.07 4378110.5 53208624.57
  31. 4 A 205863 299543953.38 22431026.24 277112927.14
  32. 5 B 34875 45475688.9 3595016.07 41880672.83
  33. 6 A 202505 223756130.64 16809079.27 206947051.37

答案1

得分: 1

将第一个查询的分组结果加入,并将这些列添加到 GROUP BY 中。您可以选择先对这两个查询进行分组,然后再进行连接,因为两者实质上只按独特的 Store 进行分组。

英文:

Just join the grouped results of the first query, and then add those columns to the GROUP BY. You could alternatively group up both queries first and join themm afterwards, as both are essentially grouped only by unique Store.

  1. SELECT
  2. sto.Store,
  3. sto.Type,
  4. sto.Size,
  5. ROUND(SUM(CASE WHEN IsHoliday = 1 OR IsHoliday = 0
  6. THEN Weekly_Sales
  7. END), 2) AS tot_sales,
  8. ROUND(SUM(CASE WHEN IsHoliday = 1
  9. THEN Weekly_Sales
  10. END), 2) AS hol_sales,
  11. ROUND(SUM(CASE WHEN IsHoliday = 0
  12. THEN Weekly_Sales
  13. END), 2) AS non_hol_sales,
  14. f.avg_unempl,
  15. f.avg_CPI,
  16. f.avg_temp
  17. FROM train tra
  18. JOIN stores sto ON tra.Store = sto.Store
  19. JOIN (
  20. SELECT
  21. f.Store,
  22. ROUND(AVG(f.Unemployment), 2) AS avg_unempl,
  23. ROUND(AVG(f.CPI), 2) AS avg_CPI,
  24. ROUND(AVG(f.Temperature), 2) AS avg_temp
  25. FROM features f
  26. GROUP BY f.Store
  27. ) f ON f.Store = sto.Store
  28. GROUP BY
  29. sto.Store,
  30. sto.Size,
  31. sto.Type,
  32. f.avg_unempl,
  33. f.avg_CPI,
  34. f.avg_temp;

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

发表评论

匿名网友

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

确定