SQL query for this combined expected results - pivot? - sql

Sorry for this long winded question, but I'm not sure how to go about constructing this SQL query needed for the results I want. I'll outline the two queries that I currently run and work fine and I will outline the results I need. Any help will be appreciated.
1st Query:
SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (2, 4, 5)
GROUP BY c.name ORDER BY c.name;
Result:
--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa | 2 | 150 | 75 |
bbbb | 1 | 75 | 75 |
dddd | 1 | 25 | 25 |
--------------------------------------------
2nd query:
SELECT c.name AS name, count(*) AS total, sum(a.views) AS total_views, sum(a.views) / count(*) as average_views
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (1, 3)
GROUP BY c.name ORDER BY c.name;
2nd results:
--------------------------------------------
name | total | total_views | average_views |
--------------------------------------------
aaaa | 2 | 200 | 100 |
bbbb | 1 | 100 | 100 |
dddd | 1 | 25 | 25 |
--------------------------------------------
Given these tables with this data:
Table table_a:
-----------------------------------
id | b_id | views | type | status |
-----------------------------------
1 | 100 | 100 | 2 | 0 |
2 | 200 | 75 | 4 | 0 |
3 | 300 | 50 | 5 | 0 |
4 | 400 | 25 | 2 | 0 |
5 | 500 | 100 | 1 | 0 |
6 | 600 | 100 | 1 | 0 |
7 | 700 | 100 | 3 | 0 |
8 | 800 | 25 | 3 | 0 |
-----------------------------------
Table table_b:
-------------
id | c_id |
-------------
100 | 1000 |
200 | 2000 |
300 | 1000 |
400 | 4000 |
500 | 1000 |
600 | 2000 |
700 | 4000 |
800 | 1000 |
-------------
Table table_c:
-------------
id | name |
-------------
1000 | aaaa |
2000 | bbbb |
3000 | cccc |
4000 | dddd |
-------------
This is the table that I actually want, which is simply a concantenation of the above two tables with the common column being the name column.
-------------------------------------------------------------------------------------------------------------------------------
name | total_type245 | total_views_type245 | average_views_type245 | total_type13 | total_views_type13 | average_views_type13 |
-------------------------------------------------------------------------------------------------------------------------------
aaaa | 2 | 150 | 75 | 2 | 200 | 100 |
bbbb | 1 | 75 | 75 | 1 | 100 | 100 |
dddd | 1 | 25 | 25 | 1 | 25 | 25 |
-------------------------------------------------------------------------------------------------------------------------------
It's most likely quite a simple query, but I cannot work out how to do it.
Thanks.

Just join the results together;
SELECT ResultsA.name,
total_type245,
total_views_type245,
average_views_type245,
total_type13,
total_views_type13,
average_views_type13
FROM
(
SELECT c.name AS name, count(*) AS total_type245, sum(a.views) AS total_views_type245, sum(a.views) / count(*) as average_views_type245
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (2, 4, 5)
GROUP BY name
) as ResultsA
JOIN
(
SELECT c.name AS name, count(*) AS total_type13, sum(a.views) AS total_views_type13, sum(a.views) / count(*) as average_views_type13
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (1, 3)
GROUP BY name
) as ResultsB ON ResultsA.name = ResultsB.name
ORDER BY ResultsA.name

Ok, so with Matt's help this query works:
SELECT c.name, total_type245, total_views_type245, average_views_type245, total_type13, total_views_type13, average_views_type13
FROM table_c c
LEFT JOIN (
SELECT c.name AS name, count(*) AS total_type245, sum(a.views) AS total_views_type245, sum(a.views) / count(*) as average_views_type245
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (2, 4, 5)
GROUP BY name
) as ResultsA ON ResultsA.name = c.name
LEFT JOIN (
SELECT c.name AS name, count(*) AS total_type13, sum(a.views) AS total_views_type13, sum(a.views) / count(*) as average_views_type13
FROM table_a a
JOIN table_b b ON b.id = a.b_id
JOIN table_c c ON c.id = b.c_id
WHERE a.status = 0 AND a.type in (1, 3)
GROUP BY name
) as ResultsB ON ResultsB.name = c.name;
Is this the most efficient query for the job though? It seems I'm repeating lots of the query with the only change being the a.type value being the difference.

Related

Join & Group By - Column is invalid in the select list

I'm running a query to join 2 tables and then group the rows by 2 fields and select the rows with the min ID from these groups and I get an error. The joined table looks like:
+--------+---------+-----------------+-----------+-----------+
| ID | CODE | NAME | VRACHAR01 | VRACHAR02 |
+--------+---------+-----------------+-----------+-----------+
| 290861 | 1110896 | PRODUCT NAME XX | 001 | 706 |
| 290864 | 1110899 | PRODUCT NAME XX | 001 | 706 |
| 290865 | 1110900 | PRODUCT NAME XX | 003 | 721 |
| 290870 | 1110905 | PRODUCT NAME XX | 004 | 743 |
| 290871 | 1110906 | PRODUCT NAME XX | 004 | 743 |
| 290878 | 1110913 | PRODUCT NAME XX | 006 | 806 |
| 290879 | 1110914 | PRODUCT NAME XX | 007 | 807 |
| 290908 | 1110943 | PRODUCT NAME XX | 008 | 815 |
+--------+---------+-----------------+-----------+-----------+
If I run the script below to group the results by the last 2 fields I get an error:
Column 'A.CODE' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
SELECT
min(A.ID),
A.CODE,
A.NAME,
B.VARCHAR01,
B.VARCHAR02
FROM
PRODUCTS A
INNER JOIN
EXTRAS B
ON
A.ID = B.ID
WHERE
A.COMPANY = 1002
AND
A.TYPE = 50
GROUP BY
B.VARCHAR01,
B.VARCHAR02
Any help is appreciated.
If you want the min id, then you don't need other columns:
SELECT MIN(P.ID), E.VARCHAR01, E.VARCHAR02
FROM PRODUCTS P INNER JOIN
EXTRAS E
ON P.ID = E.ID
WHERE P.COMPANY = 1002 AND P.TYPE = 50
GROUP BY E.VARCHAR01, E.VARCHAR02;
Note that this replaces the meaningless table aliases with abbreviations for the table names.
If you want the entire row, then you can use window functions:
SELECT PE.*
FROM (SELECT P.*, E.VARCHAR01, E.VARCHAR02,
ROW_NUMBER() OVER (PARTITION BY E.VARCHAR01, E.VARCHAR02 ORDER BY P.ID ASC) as seqnum
FROM PRODUCTS P INNER JOIN
EXTRAS E
ON P.ID = E.ID
WHERE P.COMPANY = 1002 AND P.TYPE = 50
) PE
WHERE seqnum = 1;

Can someone help me figure out if I'm making a mistake in my query?

I'm trying to create a query that returns the names of all people in my database that have less than half of the money of the person with the most money.
These is my query:
select P1.name
from Persons P1 left join
AccountOf A1 on A1.person_id = P1.id left join
BankAccounts B1 on B1.id = A1.account_id
group by name
having SUM(B1.balance) < MAX((select SUM(B1.balance) as b
from AccountOf A1 left join
BankAccounts B1 on B1.id = A1.account_id
group by A1.person_id
order by b desc
LIMIT 1)) * 0.5
This is the result:
+-------+
| name |
+-------+
| Evert |
+-------+
I have the following tables in the database:
+---------+--------+--+
| Persons | | |
+---------+--------+--+
| id | name | |
| 11 | Evert | |
| 12 | Xavi | |
| 13 | Ludwig | |
| 14 | Ziggy | |
+---------+--------+--+
+--------------+---------+
| BankAccounts | |
+--------------+---------+
| id | balance |
| 11 | 525000 |
| 12 | 750000 |
| 13 | 1900000 |
| 14 | 1600000 |
+--------------+---------+
+-----------+-----------+------------+
| AccountOf | | |
+-----------+-----------+------------+
| id | person_id | account_id |
| 301 | 11 | 12 |
| 302 | 13 | 12 |
| 303 | 13 | 14 |
| 304 | 14 | 11 |
| 305 | 14 | 13 |
+-----------+-----------+------------+
What am I missing here? I should get two entries in the result (Evert, Xavi)
I wouldn't approach the logic this way (I would use window functions). But your final having has two levels of aggregation. That shouldn't work. You want:
having SUM(B1.balance) < (select 0.5 * SUM(B1.balance) as b
from AccountOf A1 join
BankAccounts B1 on B1.id = A1.account_id
group by A1.person_id
order by b desc
limit 1
)
I also moved the 0.5 into the subquery and changed the left join to a join -- the tables need to match to get balances.
I would recommend window functions, if your - undisclosed! - database supports them.
You can join and aggregate just once, and then use a window max() to get the top balance. All that is then left to is to filter in an outer query:
select *
fom (
select p.id, p.name, coalesce(sum(balance), 0) balance,
max(sum(balance)) over() max_balance
from persons p
left join accountof ao on ao.person_id = p.id
left join bankaccounts ba on ba.id = ao.account_id
group by p.id, p.name
) t
where balance > max_balance * 0.5

Get left table data completely even when there is no reference in right joined table

Database used: SQL Server
I have three tables A,B,C.
TABLE A:
------------------
| ID | Name |
------------------
| 1 | X |
------------------
| 2 | Y |
------------------
TABLE B:
----------------------
| ID | Date |
----------------------
| 1 | 2019-11-06 |
----------------------
| 2 | 2019-11-05 |
----------------------
TABLE C:
----------------------------------
| ID | B.ID | A.ID | Amount |
----------------------------------
| 1 | 1 | 1 | 500 |
----------------------------------
| 2 | 2 | 2 | 1000 |
----------------------------------
The result I would like to get is all entries of table A.Name with their amount in table C.amount where table B.Date = 2019-11-06. The result set should include all A.name entries even it have no reference in Table C.
Required result is:
-----------------------
| A.Name | C.Amount |
-----------------------
| X | 500 |
-----------------------
| Y | NULL |
-----------------------
Code I tried with :
SELECT A.Name,C.Amount
FROM A
LEFT OUTER JOIN C ON C.A_ID=A.ID
LEFT OUTER JOIN B ON B.ID = C.B_ID ON
WHERE B.Date='2019-11-06'
The result I obtained with above code is :
------------------
| Name | Amount |
------------------
| X | 500 |
------------------
There is no Y in the result, its because there is no entry for Y on that particular date. I just want to show Y and amount as null or zero.
SQL Fiddle with my query
Please help me with this.
There's is no relationship between your A and B, so we need to group B and C using a subquery to filter with date before doing the left join.
SELECT A.Name, t1.Amount
FROM A
LEFT JOIN
(SELECT C.A_ID, C.Amount FROM C
INNER JOIN B ON B.ID = C.B_ID
WHERE B.Date='2019-11-06') t1
ON t1.A_ID=A.ID
see dbfiddle
Try this-
Fiddle Here
SELECT A.Name,C.Amount
FROM A
LEFT JOIN B ON A.ID = B.ID AND B.Date = '2019-11-06'
LEFT JOIN C ON B.ID = C.ID
Output is-
Name Amount
X 500
Y (null)

Select Customers where not have value in other table with join

I need to select all Customers from the table Customer where Value in table Customer_Value is not 4.
Customers:
+------------+-------+
| Customer | ... |
+------------+-------+
| 312 | ... |
| 345 | ... |
| 678 | ... |
+------------+-------+
Customer_Value:
+------------+-------+
| Customer | Value |
+------------+-------+
| 312 | 1 |
| 312 | 2 |
| 345 | 1 |
| 345 | 2 |
| 345 | 3 |
| 678 | 1 |
| 678 | 2 |
| 678 | 4 |
+------------+-------+
To get my result I've used the following query:
SELECT C.Customer FROM [Customer] C
Left join Customer_Value V ON (C.Customer = V.Customer)
WHERE C.Customer NOT IN (SELECT Customer FROM [Customer_Value] WHERE Value = '4')
GROUP BY C.Customer
So my question is:
Is that a fast and good query? Or are there some other better solutions to get all the Customer Ids?
You can avoid Negative condition using Left Join and IS NULL Filter in where Condition.
SELECT C.Customer FROM [Customer] C
Left join Customer_Value V ON (C.Customer = V.Customer) and V.Value = '4'
WHERE V.Value is null
GROUP BY C.Customer
Your method is overkill; the JOIN is not necessary. I would use not exists:
select c.Customer
from Customer c
where not exists (select 1
from customer_value cv
where c.Customer = v.Customer and
cv.value = 4
);
You can also use aggregation, if you assume that all customers have at least one row in customer_value:
select cv.customer
from customer_value cv
group by cv.customer
having sum(case when cv.value = 4 then 1 else 0 end) = 0;
I would do
select * from customer c
join (
select distinct customer from customer_value where value!=4) v on c.customer = v.customer

Postgres group by columns and within group select other columns by max aggregate

This is probably a standard problem, and I've keyed off some other greatest-n-per-group answers, but so far been unable to resolve my current problem.
A B C
+----+-------+ +----+------+ +----+------+-------+
| id | start | | id | a_id | | id | b_id | name |
+----+-------+ +----+------+ +----+------+-------+
| 1 | 1 | | 1 | 1 | | 1 | 1 | aname |
| 2 | 2 | | 2 | 1 | | 2 | 2 | aname |
+----+-------+ | 3 | 2 | | 3 | 3 | aname |
+----+------+ | 4 | 3 | bname |
+----+------+-------+
In English what I'd like to accomplish is:
For each c.name, select its newest entry based on the start time in a.start
The SQL I've tried is the following:
SELECT a.id, a.start, c.id, c.name
FROM a
INNER JOIN (
SELECT id, MAX(start) as start
FROM a
GROUP BY id
) a2 ON a.id = a2.id AND a.start = a2.start
JOIN b
ON a.id = b.a_id
JOIN c
on b.id = c.b_id
GROUP BY c.name;
It fails with errors such as:
ERROR: column "a.id" must appear in the GROUP BY clause or be used in an aggregate function Position: 8
To be useful I really need the ids from the query, but cannot group on them since they are unique. Here is an example of output I'd love for the first case above:
+------+---------+------+--------+
| a.id | a.start | c.id | c.name |
+------+---------+------+--------+
| 2 | 2 | 3 | aname |
| 2 | 2 | 4 | bname |
+------+---------+------+--------+
Here is a Sqlfiddle
Edit - removed second case
Case 1
select distinct on (c.name)
a.id, a.start, c.id, c.name
from
a
inner join
b on a.id = b.a_id
inner join
c on b.id = c.b_id
order by c.name, a.start desc
;
id | start | id | name
----+-------+----+-------
2 | 2 | 3 | aname
2 | 2 | 4 | bname
Case 2
select distinct on (c.name)
a.id, a.start, c.id, c.name
from
a
inner join
b on a.id = b.a_id
inner join
c on b.id = c.b_id
where
b.a_id in (
select a_id
from b
group by a_id
having count(*) > 1
)
order by c.name, a.start desc
;
id | start | id | name
----+-------+----+-------
1 | 1 | 1 | aname