How to join two tables with grouping in MySQL? - mysql

I have two tables,Table 1 and Table 2,Accid is the key to join two tables,
i want to sum revenueact and revenuutilz based on year and account, so out will look like this
in reality more data is there,when i join two tables and group by year only first account is coming,can anyone please help me on this?

You could try this:
SELECT
Accname,
YEAR,
SUM(revenueact) AS Revac,
SUM(revenuutilz) AS Revut
FROM table1 a
INNER JOIN Table2 b
ON a.Accid = b.Accid
GROUP BY Accname,Year

You cound use a join adn a group by
select t2.accname, sum(t1.revenueact), sum(t1.revenuutiliz), t1.year
from table1 t1
inner join table2 t2 on t1.accid = t2.accid
group by t2.accname, t1.year

Related

Display Data from Table where Data is not In other Table with group by

I have a table that looks like this.
and a table like this
My question is this. How can I display data from table2 that is not on table1? on a date basis for example.
and also how can I include the date?
what I mean sir is how can I display the data from table2 that is not in 1 table1? for example BBB,CCC,DDD,EEE are not in table1 only AAA on date 1/1/2018
SELECT a.*,b.*,
FROM Table1 a
LEFT JOIN Table2 b
Where a.Name <>b.Name
Try the above query. If this does not work, please provide a sqlfiddle and we wil improve it.
Generate all the possible rows using cross join and then weed out the ones that don't exist:
select n.name, d.date
from table2 n cross join
(select distinct date from table1) d left join
table1 t1
on n.name = t1.name and d.date = t1.date
where t1.name is null;

SQL SELECT from multiple tables or JOIN

I have two tables from which I need to get data in the same SELECT output. The thing is that I need to limit the amount of results.
Say I have an ID column that is unique in table1, but in table2 it has many rows with that ID.
Now I just want to list how many different IDs I have in table1 and some other information stored in table2.
How can I get the desired output I show in the end?
To make my idea clear I used a "messenger" database for an example.
Tables
T1
Id_thread Date
1 13Dic
2 12Dic
T2
Id_thread Message Name
1 Hi Someone
1 Hi to you Someone
2 Help me? Someother
2 Yes! Someother
Desired output
T1.Id_thread T2.Name T1.Date
1 Someone 13Dic
2 Someother 12Dic
I'd join and use distinct:
SELECT DISTINCT t1.id_thread, t2.name, t1.date
FROM t1
JOIN t2 ON t1.id_thred = t2.id_thread
Use a JOIN and GROUP BY:
SELECT t1.Id_thread, t2.Name, t1.Date
FROM t1
JOIN t2 ON t1.Id_thread = t2.Id_thread
GROUP BY t1.Id_thread
Note that if Name is the same for all rows in t2 that have the same Id_thread, that column probably should be in t1. If you fix that, you don't need the JOIN.
Try this:
SELECT DISTINCT T1.Id_thread, T2.Name, T1.Date
FROM T1
LEFT OUTER JOIN T2 ON T1.Id_thread = T2.Id_thread
select T1.Id_thread,T2.Name,T1.Date from T1
inner join T2 on T1.Id_thread = T2.Id_thread
group by T1.Id_thread
order by T1.Id_thread
You haven't specified how you want the limit the results from Table 2. Considering you just want one row, you can use a CROSS APPLY:
Select T1.Id_thread,T2Table.Name,T1.Date From T1
Cross Apply (Select Top 1 T2.Name From T2 Where T2.Id_thread=T1.Id_thread) T2Table
You can specify other conditions in the inner Select statement if you wish.

Select all which is not in table

I have a table in MySQL where all employees are listed. I have another where all employees are listed which have to work on a specific day. And now I want to select all employees which have free (or at least are NOT listed in the work-table).
In this fiddle you can see my schema.
A code like SELECT * FROM pf_mitarbeiter WHERE NOT LISTED AS employeeID IN pf_tagesplan_zuteilungen would be super awesome. But I take other versions as well too.
Thank you guys!
Use a LEFT JOIN to join pf_tagesplan_zuteilungen on employeeID with a condition that there's no rows matching pf_mitarbeiter:
SELECT t1.*
FROM pf_mitarbeiter t1
LEFT JOIN pf_tagesplan_zuteilungen t2 ON t2.employeeID = t1.ID AND t2.date = CURDATE()
WHERE t2.ID IS NULL
select *
from A
where not exists
(
select 1
from B
where a.key = b.key
)
Use a LEFT OUTER JOIN to join the two tables. By doing so, you can select all the rows from the pf_mitarbeiter table even though there is no related row in the pf_tagesplan_zuteilungen table.
SELECT
S.*
FROM
pf_mitarbeiter S
LEFT OUTER JOIN pf_tagesplan_zuteilungen T ON (S.ID = T.employeeID)
WHERE
T.ID IS NULL
;
The IS NULL condition restricts the join to only return pf_mitarbeiter rows where there is no pf_tagesplan_zuteilungen row with a matching employeeId.

Left Join with condition from join

Suppose I have 2 tables:
Table_1 has |Product_ID|Max_Date
Table_2 has |Invoice_ID|Product_ID|Sale_Date|Amount_Sold
Table_2 stores all the Sales for the products.
I want to join the tables in such a way that I get a SUM(Amount_Sold) from Table_2 grouped by Product_ID but only considering the sales that occured before the Max_Date for each product from Table_1.
I tried
SELECT * FROM
(SELECT * FROM Table_1)a
LEFT JOIN
(SELECT Product_ID,Sale_Date,SUM(Amount_Sold) FROM Table_2 GROUP BY Product_ID)b
ON a.Product_ID=b.Product_ID AND b.Sale_Date<a.Max_Date
but it didn't return the correct sums.
I think the answer might be along these lines, but I have not been able to figure it out...
mysql: How to INNER JOIN a table but limit join to 1 result with the highest vote or count?
Any ideas?
Thanks in advance.
You can include the date criteria in your join conditional, which also makes the joins simpler to read, IMHO.
SELECT t2.product_id, sum(amount_sold)
FROM Table_1 t1 INNER JOIN Table_2 t2
ON t1.product_id = t2.product_id AND t2.sale_date <= t1.max_date
GROUP BY t2.product_id

Mysql Inner Join

Can anyone help me please? Inner join query working fine. but query displaying duplicate data. I don't to display duplicate data.
here is my query.
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
Here is output
"COLOR";"456";"456";"Nude"
"COLOR";"456";"456";"Ivory"
"COLOR";"456";"456";"Black"
"COLOR";"456";"456";"Coral"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Coral"
"COLOR";"459";"459";"Nude"
"COLOR";"459";"459";"Ivory"
"SIZE";"460";"460";"Large"
"SIZE";"460";"460";"Medium"
"SIZE";"460";"460";"Small"
"SIZE";"470";"470";"Large"
"SIZE";"470";"470";"Small"
"SIZE";"470";"470";"Medium"
"COLOR";"476";"476";"White"
"COLOR";"476";"476";"Black"
"SIZE";"477";"477";"Small"
But i don't to display duplicate data. for example which is displaying here.
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"460";"60";"Black"
is there any way?? thanks
Maybe you just want to group it by names? You seem to be calling a duplicated data what seems to have different ids...
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
GROUP BY t1.class,t2.option_name