I have two tables:
Table A
ID | DATE | VALUE | KEY|
1 30.8.14 100 11
2 25.8.14 500 11
2 20.8.14 250 11
Table B
ID | DATE | VALUE | KEY|
1 30.8.14 AB 11
2 25.8.14 CD 11
3 10.8.14 EF 11
These two tables should be merged, key is used to define which entries should be merged WHERE KEY = '11'
IF there is a date in TABLE A that is also in TABLE B, it becomes on entry with both values
IF there is no date in TABLE A that is also in TABLE B, the value for B becomes (null)
And in the End, there should be only 1 date field.
Columns should also be be a unique name..
I created this example table, how my output should look like
joinedDate | aValue | bValue
30.8.14 100 AB
25.8.14 500 CD
20.8.14 250 (null)
10.8.14 (null) EF
Im using MySQL version 5.5 on Maria DB
Could someone help me here?
You seem to want full outer join, which MySQL doesn't offer. Here is one method:
select d.date, a.value as avalue, b.value as bvalue
from ((select date from a union
select date from b
)
) d left join
a
on a.date = d.date left join
b
on b.date = d.date;
select a.date, a.value as avalue, b.value as bvalue
from tablea a
left join tableb b
on a.date = b.date
union all
select b.date, null, b.value
from tableb b
left join tablea a
on a.date = b.date
where a.date is null
Fiddle:
http://sqlfiddle.com/#!2/09ab9e8/4/0
Related
Im creating a query that select two tables and create a total variable by count a field in one table.
Example:
Table A:
ID | email
1 | test#test
2 | test2#test
3 | test3#test
Table B
ID | email_id | username_id
1 | 1 | 11
2 | 1 | 22
3 | 2 | 33
My query:
select a.id, a.email, count(c.id) as total
from tableA a
left join tableC c on c.email_id = a.id AND total <= 5
group by a.email LIMIT 1
Output:
Unknown column 'total' in 'on clause
I need to select the first "a.id" that has total <= 5. How can I do it?
Logically Select is processed after the Where clause so you cannot use Alias name in same Where clause.
Use HAVING clause
select a.id, a.email, count(c.id) as total
from tableA a
left join tableC c on c.email_id = a.id
group by a.email
Having count(c.id) <= 5
LIMIT 1
I think Mysql allows you do this as well
Having total <= 5
Try HAVING Count(c.id) <= 5
Just to make this a bit clearer, since the correct answer has already been provided - You don't have to use the HAVING clause, and the HAVING clause is not always the solution for this problem.
The HAVING clause is usually used to place filters on aggregated columns (sum,count,max,min etc..) , but when you have a calculated column (colA + colB as calc_column for example) , then another approach , which should work here as well is to wrap the query with another select, and then the new column will be available on the WHERE :
SELECT *
FROM (The query here ) s
WHERE s.total <= 5
I have two tables:
Table A
id name
--- -----
1 Foo
2 Bar
3 Fred
4 Joe
Table B
id tablea_id result date
--- ---- ---- ---
1 1 A 02/04/2015
2 2 A 02/04/2015
3 1 B 03/04/2015
4 1 C 04/04/2015
How would I be able to achieve a result set that looks like this? What joins would I need in order to 'cycle' what is in table A?
date tablea_id result
--- --- ----
02/04/2015 1 A
02/04/2015 2 A
02/04/2015 3 NULL
02/04/2015 4 NULL
03/04/2015 1 B
03/04/2015 2 NULL
03/04/2015 3 NULL
03/04/2015 4 NULL
04/04/2015 1 C
04/04/2015 2 NULL
04/04/2015 3 NULL
04/04/2015 4 NULL
It looks like you want a cross join to generate all the rows and a left join to bring in existing values:
select d.date, a.tablea_id, b.result
from a cross join
(select distinct date from b) d left join
b
on b.tablea_id = a.tablea_id and b.date = d.date
order by d.date, a.tablea_id;
What joins would I need
An OUTER JOIN and most probably (per your posted expected result) a LEFT OUTER JOIN
select ab.id,ab.date,b.result
from
(select a.id,b.date
from(select distinct id
from table_a)a,
(select distinct date
from table_b)b)ab
left outer join
table_b b
on ab.id=b.id
and ab.date=b.date
Check This Simple Query using Full Outer Join. Live Demo
select distinct a.date,a.id as tablea_id ,result from B b
full outer join
(
select a.id,b.date from B b,A a
)A on a.id=b.tablea_id and a.date=b.date
Given 2 tables, I want to generate top 3 highest amount from [Purchase] table.
Additional criteria is [Crocs] must be included in top 3 of the records.
I have following SQL, but it cannot generates the result as I wanted (Result A), please guide me on how to pull out the result in Result B. Thank you.
Table (Purchase):
Purchase_ID | StoreID | Amount
------------|---------|--------
1 | 21 | 22
2 | 23 | 13
3 | 25 | 6
4 | 26 | 23
5 | 28 | 18
Table (Store):
Store_ID | StoreName
---------|----------
21 | Adidas
22 | Nike
23 | Puma
24 | New Balance
25 | Crocs
26 | Converse
SQL:
SELECT IF(SUM(amount) IS NULL, 0, SUM(amount)) as totalAmount
FROM (
SELECT a.amount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
GROUP BY a.amount
HAVING b.StoreName = 'Crocs'
ORDER BY a.amount DESC
LIMIT 3
) t
Result A: $6
Explanation A: Amount of Crocs is $6
Result B: $51
Explanation B: Total Amount of top 3 = $22 (Adidas) + 23 (Puma) + $6 (Crocs)
The answer from scaisEdge is almost right, but the first query could also return a row with crocs and the sorting is wrong (order by max(a.amount) limit 2 means that the lowest 2 results will be shown). Additionally you could wrap the query in another select query to sort the results
SELECT * FROM (
SELECT b.storename, max(a.amount) as maxAmount
FROM purchase a
INNER JOIN store b ON a.store_id = b.storeid
WHERE b.storename != 'crocks'
GROUP BY a.storename
ORDER BY max(a.amount) DESC
LIMIT 2
UNION
SELECT b.storename, a.amount as maxAmount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
WHERE b.storename='crocks'
ORDER BY a.amount DESC
LIMIT 1
) ORDER BY maxAmount DESC
You could use an union
SELECT b.storename, max(a.amount)
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
GROUP BY a.storename
order by max(a.amount) limit 2
union
SELECT b.storename, a.amount
FROM purchase a
INNER JOIN store b
ON a.store_id = b.storeid
where b.storename='crocks'
try this one:
SELECT sum(amount)as sum_amount,a.store_id,storename,category from
(select amount,store_id from tbl_purchase) as a
inner JOIN
(select store_id,storename,category from tbl_store)as b on a.store_id = b.store_id where b.category = 'supermarket' GROUP BY category
I have got 2 tables.
A :
ida title
1 aaa
2 bbb
3 ccc
and
B :
idb ida date count
1 1 2014-09-15 14:22:37 15
2 1 2014-09-15 15:52:07 34
3 1 2014-09-15 14:25:38 16
I would like to get all the A table rows, the most recent date and the count corresponding in B even if none of them exist in B table.
I expect this result on this example.
A.ida A.title B.date B.count
1 aaa 2014-09-15 15:52:07 34
2 bbb null null
3 ccc null null
Any help would be apreciated :)
Edit : my initial and false request was :
SELECT e.ida, MAX(n.date_add) as date_notification, n.count FROM ida e LEFT JOIN idb n ON e.ida = n.ida group by e.ida
Don't joining it to this initial Stack was akward.
Here is the query you're looking for:
SELECT A.ida
,A.title
,B.date
,B.count
FROM tableA A
LEFT OUTER JOIN (SELECT B.ida
,MAX(B.date) AS [max_date]
FROM tableB B
GROUP BY B.ida) T ON T.ida = A.ida
LEFT OUTER JOIN tableB B ON B.ida = T.ida
AND B.date = T.max_date
ORDER BY A.ida
The first jointure is used to get only the last date for each ida and the second jointure is used to get the corresponding information from tableB.
Hope this will help you.
I'm having trouble fixing a MYSQL query that will find "matches" between 2 relational tables. For example, if the TABLEA ID being passed in was 2, then I want to return all IDs from TABLEB where both TABLEA VALUES (b and c) exist for a single TABLEB ID. Hope that made sense! In this instance it wouldn't return anything. However, when passing in ID 3 it would return 13 as d and e exist within ID 13 rows in TABLEB. Any help massively appreciated!
TABLEA
ID | VALUE
1 | a
2 | b
2 | c
3 | d
3 | e
TABLEB
ID | VALUE
10 | a
12 | b
12 | z
13 | d
13 | e
13 | f
You can try this query.
UPDATE:
In MySQL :
SELECT tab_b.ID
FROM TABLEB tab_b
WHERE tab_b.VALUE IN (SELECT TABLEA.VALUE
FROM TABLEA
WHERE TABLEA.ID = 3)
GROUP BY tab_b.ID
HAVING COUNT(DISTINCT tab_b.VALUE) = (SELECT COUNT(TABLEA.VALUE)
FROM TABLEA
WHERE TABLEA.ID = 3)
Check this Fiddle in MySQL: ->http://sqlfiddle.com/#!2/b1aba/10
Fiddle in SQL Server: -> http://sqlfiddle.com/#!3/b0d63/3
(You'll have to change the values from 2 to 3 to see the desired result)
Hope this helps!!!
select b.id2
from tableA a
left join tableB b
on a.value = b.value2
where a.id = 2
and id2 is not null
hope this answers it for you
here is the fiddle
fiddle
http://sqlfiddle.com/#!3/7e4b1/5