How can I combine two select queries for the same table, which have two different where in Sqlite?
I have a table with more than 10k rows. Two example rows looks like this:
year | project | finance | flow
1990 | water | 300500 | grant
1999 | energy | 200500 | loan
My attempt does not work:
SELECT sum(finance), (select sum(finance) from table where flow = grant)
FROM table where flow = loan group by year
The result should have all results group by year and list the sum(budget) in a column for grants and a column for loans.
year | grant | loan
1990 | 62662 | 383983
1991 | 28928 | 278272
UPDATE: The first example did not correspond correctly to my use case. I had to change it.
In this case, use CASE:
The logic is different than what you outlined (it’s a single SELECT with a single WHERE clause) but it will effectively pivot the data you need from rows to columns.