union query only youngest result - mysql

I have two query that are the same except for the two different tables Transit_now & Transit_old, both have the same layout. Transit_old is an archive of transit_now. Both querys will only display the youngest value
I only want the youngest value to display when i use a union between the two querys. Now i have sometines two values from transit_now and tansit_old
Newest result can be in transit_old when there is no result for transit_now. There are multiple results on both tables, but that is coverd with the where statement in both querys
(
SELECT A.Batch, A.Date, B.name, B.Surname,
FROM Transit_now
INNER JOIN DB.USERS B
ON A.Transit_now.ID = B.Users.ID
where A.DATE = (select MAX(DATE) from Transit_now where (A.Transit_now.ID = B.Users.ID))
UNION
SELECT A.Batch, A.Date, B.name, B.Surname,
FROM Transit_old
INNER JOIN DB.USERS B
ON A.Transit_old.ID = B.Users.ID
where A.DATE = (select MAX(DATE) from Transit_now where (A.Transit_old.ID = B.Users.ID))
)

Related

Adding a JOIN to SQL query gives different result, its not an accidental CROSS JOIN

...
Trying to learn SQL and the following query:
SELECT a.id, a.name, w.channel, COUNT(*) use_of_channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
GROUP BY a.id, a.name, w.channel
HAVING COUNT(*) > 6 AND w.channel = 'facebook'
ORDER BY use_of_channel;
returns 46 results (first query results), JUST ADDING A JOIN of an unrelated table returns 220 results.
Its not a CROSS JOIN since it seems properly formatted, just added down here at line 5 a JOIN with "orders" table
SELECT a.id, a.name, w.channel, COUNT(*) use_of_channel
FROM accounts a
JOIN web_events w
ON a.id = w.account_id
JOIN orders o
ON o.account_id = a.id
GROUP BY a.id, a.name, w.channel
HAVING COUNT(*) > 6 AND w.channel = 'facebook'
ORDER BY use_of_channel;
...but why would another table compromise the results?
It is a cross join. That is, each account has multiple events. And each account has multiple orders. So within each account you are getting a Cartesian product.
A quick way to fix this is to use count(distinct) on a primary key:
SELECT a.id, a.name, w.channel,
COUNT(w.id) as use_of_channel

How to combine aggregated queries with different joins?

I've been looking at this SO post and this jooq.org post, trying to figure out how to do my combined aggregations in MySQL, but not having much luck.
Here are my 2 queries:
select a.IsTestAgency, a.ID, a.AgencyName, a.CreateDate, count(*) AS Agents
from Users u
join Agencies a
on u.AgencyID = a.ID
group by a.IsTestAgency, a.ID, a.AgencyName, a.CreateDate
order by a.IsTestAgency, a.AgencyName;
Results:
and:
select a.IsTestAgency, a.ID, a.AgencyName, a.CreateDate, count(*) AS Certs
from Certificates c
join Agencies a
on c.AgencyID = a.ID
group by a.IsTestAgency, a.ID, a.AgencyName, a.CreateDate
order by a.IsTestAgency, a.AgencyName;
Results:
You can see that the columns and columns' datatypes match. I'd like to combine these into a single query and show the Agents count and the Certs count side-by-side, since those are the only 2 column values that are different in the result sets.
How is it done?
You could do this by JOINing to tables of COUNTs:
select a.IsTestAgency, a.ID, a.AgencyName, a.CreateDate, u.Agents, c.Certs
from Agencies a
join (select AgencyID, COUNT(*) as Agents from Users group by AgencyID) u on u.AgencyID = a.ID
join (select AgencyID, COUNT(*) as Certs from Certficates group by AgencyID) c on c.AgencyID = a.ID
order by a.IsTestAgency, a.AgencyName;
This removes the need to group by in the top query and also saves having to do two subquery counts for each row of the output.
Is this what you want?
select a.*,
(select count(*)
from users u
where u.AgencyID = a.ID
) as users_count,
(select count(*)
from Certificates c
where c.AgencyID = a.ID
) as certificates_count
from Agencies a
order by a.IsTestAgency, a.AgencyName;

LEFT JOIN with GROUP BY with WHERE clause and also with OR in sql

I have 3 tables:
Table 1 - "taxonomy_index" where we have 4 columns(Columns are nid,tid,sticky,created)
Table 2 - "node" where we have 15 columns(columns are nid, vid,type etc... )
Table 3 - "taxonomy_term_data" where we have 6 columns (columns are tid, vid, name etc..)
I want to join these 3 tables and also there is some condition.
I know the individual queries but i want to set all the queries into one and form one join
Query 1
SELECT count(*), tid
FROM taxonomy_index
GROUP BY tid HAVING COUNT(*) >3
This query gives me two values, one is count and other is tid. ie how many times the tid are used .
Query 2
select *
from node
where type='book' OR type='student'
OR type='teacher' OR type='class';
here i will get the node which is associated to these content types.
I want to join 3 tables. In output i want to get how many times each tid used(query 1) and where types(query 2) and get tid name from taxonomy_term_data.
Count, tid,name where for given types
Ok, suppose you can join on tid each time, you could possibly use the following syntax. Note that the CTE is not compulsary here, I just find them useful for readability.
;WITH CTE_Count AS
(
SELECT count(*) AS Count, tid
FROM taxonomy_index
GROUP BY tid HAVING COUNT(*) >3
)
, CTE_Node AS
(select *
from node
where type='book' OR type='student'
OR type='teacher' OR type='class' )
, CTE_TermData AS
(SELECT * FROM taxonomy_term_data)
SELECT CC.tid, CC.Count, CN.Node, CT.tid_Name
FROM CTE_Count AS CC
INNER JOIN CTE_Node AS CN ON CN.tid = CC.tid
INNER JOIN CTE_TermData AS CT ON CT.tid = CC.tid
SELECT distinct t1.TID, t3.name, cntt1.count from taxonomy_index t1
inner join
(
SELECT count(*) as 'count', t1.tid
FROM taxonomy_index t1
GROUP BY t1.tid HAVING COUNT(*) >3
) cntt1 on t1.tid = cntt1.tid
inner join node t2 on t1.nid = t2.nid and (type='book' OR type='student' OR
type='teacher' OR type='class')
inner join taxonomy_term_data t3 on t1.tid = t3.tid

Group and count rows and then use them in condition

I have an query like:
SELECT * FROM account AS a
LEFT JOIN (SELECT SUM(bill.amount) total, bill.accountId FROM bill GROUP BY bill.accountId) b ON a.id = b.accountId
WHERE a.partner_id = 1 OR a.partner_id = 2
How can I check, how many groups in "bill" has the same a.partner_id?
For example: 3 groups has partner_id = 1, 2 groups has partner_id = 2.
And later include to left join only groups, if more than 2 groups have the same partner_id.
If I understand correctly, you just want an aggregation on top of your query:
SELECT a.partner_id, count(*) as cnt, sum(total) as total
FROM account a LEFT JOIN
(SELECT SUM(b.amount) as total, b.accountId
FROM bill b
GROUP BY b.accountId
) b
ON a.id = b.accountId
GROUP BY a.partner_id;
You should be able to use the "HAVING" clause. Below is an example from the following link:
https://dev.mysql.com/doc/refman/5.0/en/group-by-handling.html
SELECT name, COUNT(name) AS c FROM orders
GROUP BY name
HAVING c = 1;

MySQL query and count from other table

I would like to get the data from one table, and count all results from other table, depending on the first table data, here is what I tried:
SELECT
cars.*, (
SELECT
COUNT(*)
FROM
uploads
WHERE
uploads.cid = cars.customer
) AS `count`,
FROM
`cars`
WHERE
customer = 11;
I dont really have an idea why its not working, as I'm not a regular MySQL user/coder...
Could anyone direct me in the right direction with this one?
SELECT
c.*, COUNT(u.cid) AS count
FROM
cars c
LEFT JOIN
uploads u
ON
u.cid=c.customer
WHERE
u.customer = 11;
GROUP BY c.cid
Try it by joining both tables using LEFT JOIN
SELECT a.customer, COUNT(b.cid) totalCount
FROM cars a
LEFT JOIN uploads b
ON a.customer = b.cid
WHERE a.customer = 11
GROUP BY a.customer
using COUNT(*) in LEFT JOIN will have records to have a minimum count of 1.
SELECT cars.*,COUNT(uploads.*) as uplloaded
from cars
left outer join uploads on uploads.cid = cars.customer
where cars.customer = 11
group by uploads.cid;
Try this :
SELECT customer, COUNT(cid) totalCount
FROM cars
INNER JOIN uploads
ON (customer = cid)
WHERE customer = 11
GROUP BY customer