Postgres pivot table with crosstab - sql

I have a table like the next one:
+------------+---------+---------+---------+
| date | value 1 | value 2 | value 3 |
+------------+---------+---------+---------+
| 01/01/2017 | 263 | 7 | 222 |
| 02/01/2017 | 275 | -9 | 209 |
| 03/01/2017 | 331 | -9 | 243 |
| . | . | . | . |
| . | . | . | . |
| . | . | . | . |
+------------+---------+---------+---------+
I want to create this other one in postgres:
+---------+---------------+------------+------------+
| | 01/01/2017 | 02/01/2017 | 03/01/2017 |
+---------+---------------+------------+------------+
| value 1 | 263 | 275 | 331 |
| value 2 | 7 | -9 | -9 |
| value 3 | 222 | 209 | 243 |
+---------+---------------+------------+------------+
But my problem is that I dont know how many dates I will have, so I have to use something like this:
SELECT * FROM crosstab(
$$ SELECT value1, date FROM myTable ORDER BY 1 $$,
$$ SELECT m FROM generate_series((select min(date) from myTable) ,(select max(date) from myTable), '1 month'::interval) m $$
) AS (
".." date, ".." date, ".." date, ".." date
);
Does someone can help me? Thanks.

Your basic issue is that PostgreSQL needs to know what the columns look like in order to plan the query. Consequently you need to return some sort of fixed-column structure. There are a number of ways you can do this:
Query dates first or allow them to be input, and then generate your query in the db client.
Wrap this in a stored procedure which returns a refcursor
Wrap in a stored procedure which returns a list of JSON representations of rows.
But either way you cannot do it in a query without dynamically generating the query somewhere.

Related

I want to create an Excel like pivot using SQL

This is my database:
| ID | Repeat_Times |
| ------| -------------|
| 99 | 3 |
| 100 | 4 |
| 99 | 5 |
The results I need:
ID
Repeat_Times
99
8
100
4
I'd just take a pivot in Excel, what should I use in SQL?
I assume that the table name is "repeat_table", so in general you can do the query as below to get that expected result
SELECT ID, SUM(Repeat_Times) AS Repeat_Times FROM repeat_table GROUP BY ID;

In Oracle SQL how can i find all values in one column for which in another column exist more than one distinct value

I have an Oracle table like this
| id | code | info | More cols |
|----|------|------------------|-----------|
| 1 | 13 | The Thirteen | dggf |
| 1 | 18 | The Eighteen | ghdgffg |
| 1 | 18 | The Eighteen | |
| 1 | 9 | The Nine | ghdfgjgf |
| 1 | 9 | Die Neun | ghdfgjgf |
| 1 | 75 | The Seventy-five | ghfgh |
| 1 | 75 | The Seventy-five | ghfgh |
| 1 | 2 | The Two | ghfgh |
| 1 | 27 | The Twenty-Seven | |
| 1 | 27 | The Twenty-Seven | |
| 1 | 27 | el veintisiete | fghfg |
| . | . | . | . |
| . | . | . | . |
| . | . | . | . |
In this table I want to find all rows with values in column code which have more than one distinct value in the info column. So from the listed rows this would be the values 9 and 27 and the associated rows.
I tried to construct a first query like
SELECT code FROM mytable
WHERE COUNT(DISTINCT info) >1
but I get a "ORA-00934: group function is not allowed here" error. Also I don't know how to express the condition COUNT(DISTINCT info) "with a fixed postcode".
You need having with group by - aggregate functions don't work with where clause
SELECT code
FROM mytable
group by code
having COUNT(DISTINCT info) >1
I would write your query as:
SELECT code
FROM yourTable
GROUP BY code
HAVING MIN(info) <> MAX(info);
Writing the HAVING logic this ways leaves the query sargable, meaning that an index on (code, info) should be usable.
You could also do this using exists logic:
SELECT DISTINCT code
FROM yourTable t1
WHERE EXISTS (SELECT 1 FROM yourTable WHERE t2.code = t1.code AND t2.info <> t1.info);

SQL Combine two tables with two parameters

I searched forum for 1h and didn't find nothing similar.
I have this problem: I want to compare two colums ID and DATE if they are the same in both tables i want to put number from table 2 next to it. But if it is not the same i want to fill yearly quota on the date. I am working in Access.
table1
id|date|state_on_date
1|30.12.2013|23
1|31.12.2013|25
1|1.1.2014|35
1|2.1.2014|12
2|30.12.2013|34
2|31.12.2013|65
2|1.1.2014|43
table2
id|date|year_quantity
1|31.12.2013|100
1|31.12.2014|150
2|31.12.2013|200
2|31.12.2014|300
I want to get:
table 3
id|date|state_on_date|year_quantity
1|30.12.2013|23|100
1|31.12.2013|25|100
1|1.1.2014|35|150
1|2.1.2014|12|150
2|30.12.2013|34|200
2|31.12.2013|65|200
2|1.1.2014|43|300
I tried joins and reading forums but didn't find solution.
Are you looking for this?
SELECT id, date, state_on_date,
(
SELECT TOP 1 year_quantity
FROM table2
WHERE id = t.id
AND date >= t.date
ORDER BY date
) AS year_quantity
FROM table1 t
Output:
| ID | DATE | STATE_ON_DATE | YEAR_QUANTITY |
|----|------------|---------------|---------------|
| 1 | 2013-12-30 | 23 | 100 |
| 1 | 2013-12-31 | 25 | 100 |
| 1 | 2014-01-01 | 35 | 150 |
| 1 | 2014-01-02 | 12 | 150 |
| 2 | 2013-12-30 | 34 | 200 |
| 2 | 2013-12-31 | 65 | 200 |
| 2 | 2014-01-01 | 43 | 300 |
Here is SQLFiddle demo It's for SQL Server but should work just fine in MS Accesss.

SQL query to get the same set of results

This should be a simple one, but say I have a table with data like this:
| ID | Date | Value |
| 1 | 01/01/2013 | 40 |
| 2 | 03/01/2013 | 20 |
| 3 | 10/01/2013 | 30 |
| 4 | 14/02/2013 | 60 |
| 5 | 15/03/2013 | 10 |
| 6 | 27/03/2013 | 70 |
| 7 | 01/04/2013 | 60 |
| 8 | 01/06/2013 | 20 |
What I want is the sum of values per week of the year, showing ALL weeks.. (for use in an excel graph)
What my query gives me, is only the weeks that are actually in the database.
With SQL you cannot return rows that don't exist in some table. To get the effect you want you could create a table called WeeksInYear with only one field WeekNumber that is an Int. Populate the table with all the week numbers. Then JOIN that table to this one.
The query would then look something like the following:
SELECT w.WeekNumber, SUM(m.Value)
FROM MyTable as m
RIGHT OUTER JOIN WeeksInYear AS w
ON DATEPART(wk, m.date) = w.WeekNumber
GROUP BY w.WeekNumber
The missing weeks will not have any data in MyTable and show a 0.

Aggregate function across two tables

I need for further working routine a query which calculates several functions across two (maybe more) tables. But once I import more than one table I got odd results caused by JOIN conditions. First I used that query:
SELECT
sum(s.bedarf2050_kwh_a) AS bedarf_kWh_a,
sum(s.bedarf2050_kwh_a)*0.2 AS netzverlust,
sum(s.bedarf2050_kwh_a) + sum(s.bedarf2050_kwh_a)*0.2 AS gesamtbedarf,
sum(pv.modulflaeche_qm) AS instbar_modulflaeche_qm
FROM
siedlungsareale_wbm s, pv_st_potenziale_gis pv
WHERE
s.vg_solar LIKE '%NWS 2%'
AND
ST_Covers(s.geom, pv.geom);
Using sum with DISTINCT returns some accurate values but only if all input values are unique. That's not a solution I can use:
SELECT
SUM(DISTINCT s.bedarf2050_kwh_a) AS bedarf_kWh_a,
SUM(DISTINCT s.bedarf2050_kwh_a)*0.2 AS netzverlust,
SUM(DISTINCT s.bedarf2050_kwh_a) + SUM(DISTINCT s.bedarf2050_kwh_a)*0.2 AS gesamtbedarf,
SUM(pv.modulflaeche_qm) AS instbar_modulflaeche_qm,
(SUM(DISTINCT s.bedarf2050_kwh_a) + SUM(DISTINCT s.bedarf2050_kwh_a)*0.2)*0.01499 AS startwert_speichergroesse
FROM
siedlungsareale_wbm s, pv_st_potenziale_gis pv
WHERE
pv.vg_solar LIKE '%NWS 2%'
AND
ST_Covers(s.geom, pv.geom);
DISTINCT would be a proper solution if the DISTINCT refers to another column, not the column to use in the function. Or some subquery or other JOIN condition. But all I tried run in errors or false result values.
I found some solutions using UNION dealing with aggregate function on multiple tables. But as I tried to fit the code on my query I got errors.
For example like there:
Can SQL calculate aggregate functions across multiple tables?
Hope someone can help me to build a working query for my task.
[EDIT] simple example
siedlungsareale
id | bedarf2050_kWh_a | a | b | c | vg_solar | geom
---|------------------|---|---|---|----------|-----
1 | 20 | | | | NWS 2 | xxxxx
2 | 10 | | | | NWS 2 | xxxxx
3 | 30 | | | | NWS 2 | xxxxx
4 | 5 | | | | NWS 2 | xxxxx
5 | 15 | | | | NWS 2 | xxxxx
sum = 80
pv_st_potenziale_gis
id | modulflaeche_qm | x | y | z | geom
---|------------------|---|---|---|---------
1 | 10 | | | | xxxxx
2 | 10 | | | | xxxxx
3 | 20 | | | | xxxxx
4 | 10 | | | | xxxxx
5 | 30 | | | | xxxxx
6 | 30 | | | | xxxxx
7 | 10 | | | | xxxxx
8 | 10 | | | | xxxxx
9 | 10 | | | | xxxxx
10 | 10 | | | | xxxxx
sum = 140
SELECT sum(s.bedarfxxxx) AS bedarf, sum(pv.mflaeche) As mflaeche
FROM siedlungsareale s, pv_st_potenziale_gis pv
WHERE s.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom,pv.geom);
Expected correct result:
bedarf | mflaeche
---------|----------
80 | 140
There I would get the sum of all values for column 'bedarf' from 'siedlungsareale' and all for 'mflaeche' from 'pv_st_potenziale_gis'
But the real calculated values of column 'bedarf' using this query are much higher caused of the CROSS JOIN condition.
And the other query:
SELECT sum(DISTINCT s.bedarfxxxx) AS bedarf, sum(DISTINCT pv.mflaeche) As mflaeche
FROM siedlungsareale s, pv_st_potenziale_gis pv
WHERE s.vg_solar LIKE '%NWS 2%' AND ST_Covers(s.geom,pv.geom);
returns:
bedarf | mflaeche
---------|-----------
80 | 60
Accurate value for 'bedarf' caused the values are unique. But for mflaeche where some values occurre several times the result is wrong.