I have the following tables:
Table A:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| ID | User | Enterpr_id |
| 1 | test1 | 1 |
| 2 | test2 | 2 |
| 3 | test3 | 3 |
| 4 | test4 | 4 |
| 5 | test5 | 1 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Table B:
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Enterpr_id | Name |
| 1 | Nespresso |
| 2 | what |
| 3 | else |
| 4 | need |
| 5 | help |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
I have a foreign key on Enterpr_id with Table A, how can I make a count alternate and get the expected Output:
Nespresso - 2 users
what - 1 user
else - 1 user
need - 1 user
help - 0 user
That's a simple join:
select a.user, b.name
from tablea a
inner join tableb b on b.entrepr_id = a.entrepr_id
Edit: from your updated question, you seem to want aggregation and a left join:
select b.name, count(a.id) cnt_users
from tableb b
left join tablea a on a.entrepr_id = b.entrepr_id
group by b.entrepr_id, b.name
order by b.entrepr_id
It's a left join query with a count:
SELECT Name, COUNT(TableA.ID)
FROM TableB LEFT JOIN TableA ON TableB.Enterpr_id = TableA.Enterpr_id
GROUP BY TableB.Enterpr_id, TableB.Name;
Related
I have this data in a table called PROD
| Project | Position | Status |
|---------|----------|--------|
| 1 | 1 | A |
| 1 | 2 | A |
| 2 | 1 | A |
| 2 | 2 | B |
| 3 | 1 | B |
| 3 | 2 | B |
| 4 | 1 | A |
| 4 | 2 | A |
I'm trying to get all the Projects that has at least one Position with Status = B.
| Project | Position | Status |
|---------|----------|--------|
| 2 | 1 | A |
| 2 | 2 | B |
| 3 | 1 | B |
| 3 | 2 | B |
I've tried using a JOIN like this:
SELECT * FROM PROD A JOIN PROD B ON A.PROD-Project = B.PROD-Project WHERE B.PROD-Status = 'B'
This give me an empty response.
With EXISTS:
SELECT p.* FROM PROD p
WHERE EXISTS (
SELECT 1 FROM PROD
WHERE Project = p.Project AND Status = 'B'
)
or with IN:
SELECT * FROM PROD
WHERE Project IN (SELECT Project FROM PROD WHERE Status = 'B')
If you want a solution with JOIN:
SELECT DISTINCT p.*
FROM PROD p JOIN PROD pp
ON pp.Project = p.Project
WHERE pp.Status = 'B'
See the demo.
Results:
> Project | Position | Status
> ------: | -------: | :-----
> 2 | 1 | A
> 2 | 2 | B
> 3 | 1 | B
> 3 | 2 | B
You could try using a join wit the subquery
select * from PROD
INNER JOIN (
select distinct project
from PROD
where status ='B';
) t on t.project = PROD.project
I'm trying to get all the Projects that has at least one Position with Status = B.
No need for a JOIN, just do:
SELECT DISTINCT PROD.Project WHERE PROD.Status='B'
I have two tables. One table is with master data
Table tbl1:
+-------------+------------+------------+
| ID | Name | Total |
+-------------+------------+------------+
| 1 | a | 10 |
| 2 | b | 5 |
| 3 | c | 4 |
| 4 | a | 4 |
+-------------+------------+------------+
Second table tbl2 contains child data. The key between tables is ID
Table tbl2:
+-------------+------------+
|id | qty |
+-------------+------------+
| 1 | 4 |
| 1 | 3 |
| 1 | 1 |
| 3 | 1 |
| 3 | 3 |
+-------------+------------+
I need to get output like this:
Output:
+-------------+------------+------------+
| name | sum_tot | sum_qty |
+-------------+------------+------------+
| a | 14 | 8 |
| b | 5 | 0 |
| c | 4 | 4 |
+-------------+------------+------------+
I had tried with this:
select tbl1.name, SUM(tbl1.total), SUM(tbl2.qty)
from tbl1
left join tbl2 ON tbl1.id = tbl2.id
GROUP by tbl1.name
The output that I get is:
Output:
+-------------+------------+------------+
| name | sum_tot | sum_qty |
+-------------+------------+------------+
| a | 34 | 8 |
| b | 5 |null |
| c | 8 | 4 |
+-------------+------------+------------+
Which is not correct.
Here is the sql fiddle:
The summary from first table is not in relation with second table. It seems that somehow query runs three times.
You can simply have a correlated sub-query that calculates the tbl2 sum:
select tbl1.name,
SUM(tbl1.total),
SUM(COALESCE((select SUM(tbl2.qty)
from tbl2
where tbl1.id = tbl2.id), 0)) as qty_tot
from tbl1
GROUP by tbl1.name
SELECT A.name, SUM(A.total) as sum_tot, COALESCE(B.sum_qty, 0) as sum_qty
FROM tbl1 A
LEFT JOIN (
SELECT id, SUM(qty) as sum_qty
FROM tbl2
GROUP BY id
) B ON B.id = A.id
GROUP BY A.name
select tbl1.name, SUM(tbl1.total), SUM(COALESCE(tbl2.qty, 0))
from tbl1
left join tbl2 ON tbl1.id = tbl2.id
GROUP by tbl1.name
I am trying to optimize a query and I have it down to something like this,
select a.* from
(select id, count(oid) as cnt from stuff1 s1 inner join stuff2 s2 on s1.id=s2.id group by id) as a
right join
(select id,'0' as cnt from stuff2) as b
on a.id = b.id
Basically the goal was to get the count for each oid, where those having 0 count are also included. I had a query previous to this that worked fine but it took 30 seconds to execute. I am looking to optimize the old query with this one, but I am getting NULL values from table b. I need the values from table b to show up with id and 0. Any help would be greatly appreciated.
An example of the data set could be,
Stuff1
| oid | id |
|---- |----|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
Stuff2
| id |
|----|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
the query should produce
| id | cnt |
|----|-----|
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
| 4 | 0 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
Your query is syntactically incorrect (oid may not be defined; id in the select is ambiguous). However, I suspect you want a simple left join:
select s2.id, count(s1.id) as cnt
from stuff2 s2 left join
stuff1 s1
on s1.id = s2.id
group by s2.id;
I have 3 tables like this
SecretAgents
| id | name |
|----|------|
| 1 | A |
| 2 | B |
Victims
| id | name | agent_id |
|----|------|----------|
| 1 | Z | 1 |
| 2 | Y | 1 |
| 3 | X | 2 |
Data
| id | keys | values | victim_id | form_id |
|----|------|--------|-----------|---------|
| 1 | a1 | x | 1 | 1 |
| 2 | a2 | xx | 1 | 2 |
| 3 | a3 | xxx | 2 | 1 |
| 4 | a5 | xxx | 1 | 1 |
I have to get the count of forms(here victim_id and form_id are composite primary keys) and the count of victims for each agent.
I have tried this for any 2 tables with left joins and group by but I am not able to achieve the same together. If anyone can be generous enough to offer a pointer/solution, that would be super awesome..
EDIT 1: The query
This is definitely not the right query but anyways
SELECT count(DISTINCT v.id) as victimcount, `sa`.`username`, `sa`.`id`, count(DISTINCT d.form_id) as submissions
FROM `SecretAgents` as `sa`
LEFT JOIN `Victims` as `v` ON `v`.`agent_id`=`sa`.`id`
LEFT JOIN `Data` as `d` ON `d`.`victim_id`=`v`.`id`
GROUP BY `v`.`agent_id`
ORDER BY `sa`.`id` ASC
The victimcount is correct but the submissions count becomes wrong. Tried lots of other things too but this is the most relevant...
Thanks
I believe you can count the forms-per-agent like so:
SELECT COUNT(*) as form_count, a.id as id, a.name as agent
FROM Data d
LEFT JOIN Victims v ON v.id = d.victim_id
LEFT JOIN SecretAgents a on v.agent_id = a.id
GROUP BY a.id;
To count the victims, just leave off the Data table.
I'm facing a very poorly designed database with a non-normalized table X.
This table X should have a N:M relationship with another table Y.
The problem is that this relationship is currently 1:N and the jerry-rigged solution until now was to duplicate the entries when there was various registries to be related.
Simplifying, I have this:
| ID | TEXT | LOCATION_ID |
| 1 | foo | 1 |
| 2 | foo | 2 |
| 3 | bar | 1 |
| 4 | bar | 4 |
| 5 | bar | 3 |
I have to normalize this table. So, my first idea was try to obtain pairs of similar registries. Something like this:
| a.ID | b.ID |
| 1 | 2 |
| 3 | 4 |
| 3 | 5 |
Experimenting a little bit:
SELECT a.id, b.id
FROM mytable AS a
INNER JOIN mytable AS b
ON a.text = b.text AND a.id != b.id
GROUP BY a.id, b.id
This lead to a problem like this:
| a.ID | b.ID |
| 1 | 2 |
| 2 | 1 |
| 3 | 4 |
| 3 | 5 |
| 4 | 3 |
| 4 | 5 |
| 5 | 3 |
| 5 | 4 |
The pairs were duplicated.
After some digging, I realized that this was more efficient:
SELECT a.id, b.id
FROM mytable AS a
INNER JOIN mytable AS b
ON a.text = b.text AND a.id < b.id
GROUP BY a.id, b.id
So, I got this:
| a.ID | b.ID |
| 1 | 2 |
| 3 | 4 |
| 3 | 5 |
| 4 | 5 |
But I still need to get rid of that last register.
Group on only one side and take the MIN() of the other:
SELECT MIN(a.ID) a, b.ID b
FROM mytable a JOIN mytable b ON b.text = a.text AND b.ID > a.ID
GROUP BY b.ID
See it on sqlfiddle.