'How do I combine two queries together in SQL
I need help figuring out how to write two queries in the same query.
I have created a table "tempview" with the following columns:
ApplicationNum | MonthYear | TotalFinancedAmount
I wrote two queries to calculate to calculate the most and least amounts financed: To calculate the most financed:
SELECT MonthYear, SUM(TotalFinancedAmount) AS TotalFinanced
FROM tempview
GROUP BY MonthYear
Order By Total Financed DESC LIMIT 1:
The other query to calculate the minimum is identical but instead of DESC I am using ASC
How could I write a query that does both together.
I am using databricks.
Solution 1:[1]
the following query joining both queries results in the same row if this what you want :
SELECT min_monthyear, min_total , max_monthyear , max_total
from (SELECT MonthYear as min_monthyear , SUM(TotalFinancedAmount) AS min_total
FROM tempview
GROUP BY MonthYear
Order By min_total ASC LIMIT 1) min_table ,
(SELECT MonthYear as max_monthyear , SUM(TotalFinancedAmount) AS max_total
FROM tempview
GROUP BY MonthYear
Order By max_total DESC LIMIT 1) max_table;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omar Tammam |
