I have the following table :
city | state | numOrder | date | deadlineDate |
---|---|---|---|---|
NY | NY | 111 | 2022/11/05 | 2022/11/06 |
LA | CA | 222 | 2022/11/01 | 2022/10/01 |
SD | CA | 333 | 2022/05/05 | 2022/11/06 |
LA | CA | 444 | 2022/11/01 | 2022/05/01 |
I need to calculate the number of orders placed before the deadline divided by the number of orders placed by each state and city:
(SELECT state, city ,count(*)
FROM orders
WHERE date <= deadlineDate
group by state, city) /
(SELECT state, city ,count(*)
FROM orders
group by state, city)
I tried:
SELECT (
SELECT state, city ,count(*)
FROM orders
WHERE serviceDate <= limitDate
group by state, city
)/
(
SELECT state, city ,count(*)
FROM orders
group by state, city
)
FROM orders
But the I got ERROR:
Subquery must return only one column
2
Answers
Join the tables as two subquery tables and do the math in the select
Try the following:
See a demo.