I have Table A, Column 1.
This table has values such as:
1
2
3
3
4
4
4
5
I have Table B, Column 2.
Which lists certain values, like:
1
3
4
I need a query to Count each unique value in Table A, but ONLY if that value is present in Table B.
So with the above, the end result would be:
1 has a quantity of 1,
3 has a quantity of 2,
and 4 has a quantity of 3.
My only problem is that I do not have this ability. Any help out there?
Based on your question, something like the following should solve your problem.
select b.column1,
count(a.column2)
from tableb as b
inner join tablea as a on b.column1 = a.column2
group by b.column1
Since you wanted only records which are in both tables, I am using an inner join. Then I am just grouping by the ID found in tableb, and getting the count of rows in tablea.
Let me know if you have any problems.
For more information regarding inner join, see : http://www.w3schools.com/sql/sql_join_inner.asp, and for group by, see : http://www.w3schools.com/sql/sql_groupby.asp
I would use an INNER JOIN query with GROUP BY aggregate function
SELECT a.column1,
count(a.column1) as total
FROM tablea a
INNER JOIN tableb b
ON a.column1 = b.column2
GROUP BY a.column1
SELECT column1,COUNT(column1)
FROM table1
WHERE column1 IN
(SELECT DISTINCT column2 FROM table2)
GROUP BY column1
Try this
MsSql
Select Distinct Column1,Count(Column1) Over (Partition by Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
Fiddle Demo
MySQl
Select Column1,Count(Column1)
From Table1
Where Column1 IN (Select Column2 From Table2)
group by column1
Fiddle Demo
Related
I got two tables
Table 1:
id|value
1|Tom
1|Lucy
2|Tom
2|Lucy
3|Tom
3|Lucy
3|Bard
Table 2:
id|value
1|Tom
1|Lucy
2|Tom
2|wrong
3|Tom
3|Lucy
Results should be id where all the values match in both tables:
1
Tried this:
select distinct a.id
from table1 a
join table2 b on a.id=b.id and a.value=b.value
results are
1
2
3
INTERSECT comes to mind. Or a FULL OUTER JOIN maybe. MySQL supports neither.
The easiest way I can think of in MySQL:
select id
from table1
group by id
having (id, group_concat(value order by value)) in
(
select id, group_concat(value order by value)
from table2
group by id
);
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=768cc8fb2d01c2b5219a4d56d127d117
I have a database with 2 tables. table1 and table2.
Table1 contains a list of tests ( column 'name' ). And I want to count how many rows are missing in table2 ( this table has also a column 'name' ) according to the list of tests of table1.
So I just want to count the mismatch between table1.name and table2.name.
I tried several querys, but all didnt really work.
I tried to use the 'NOT IN' statement but it takes too much time. Like several minutes.
For example, the output should be :
COUNT(*) = 20
It means that 20 tests are missing ( or not done yet ) in table2.
I'm using MySQL, so I can't use EXCEPT or MINUS statement.
Thank you by advance.
Nordine
You can use not exists :
select count(*)
from table1 t1
where not exists (select 1 from table2 t2 where t2.name = t1.name);
If you have a duplicate name in table1 then you need count(distinct t1.name) instead.
Try the below query:
select count(case when bname is null then 1 end)
from
(
select a.name as aname, b.name as bname from
table1 a left join table2 b
on a.name=b.name)x
MINUS can be used in MySQL.
Ref:http://www.mysqltutorial.org/mysql-minus/
Try this:
SELECT name
FROM table1
MINUS
SELECT name
FROM table2
I have two table :- table a, table b.
table a
---ID---
1
2
3
4
5
7
table b
---ID----
2
3
4
5
6
I have to get Output Like this without UNION Command:-
----ID-----
1
2
3
4
5
6
7
Note: I have one solution with union:-
**select * from a
UNION
select * from b;**
I need alternative to this. please experts suggest.
We need another table with (at least) 2 rows for this:
CREATE TABLE d
( id INT NOT NULL
);
INSERT INTO d
(id)
VALUES
(0), (1) ;
Then, if we want to have only one query, we can use (this is for fun, DO NOT USE in production, that's why we have UNION):
SELECT DISTINCT
COALESCE(aa.id, bb.id) AS id
FROM
d
LEFT JOIN a AS aa ON d.id = 0
LEFT JOIN b AS bb ON d.id = 1
WHERE
COALESCE(aa.id, bb.id) IS NOT NULL
ORDER BY
id ;
Tested at SQLfiddle.com, and for other table combinations:
1 row - 1 row
2 rows - 2 rows
0 rows - 1 row
0 rows - 2 rows
0 rows - 0 rows
try this:
I think it works well in MS-SQL, change it to MySQL if you need, but MYSql doesnot support full outer join! Good luck
SELECT (
CASE
WHEN b.ID IS NULL
THEN a.ID
WHEN b.ID=a.ID
THEN b.ID
ELSE b.ID
END)
FROM
(SELECT ID FROM table2
)b
FULL OUTER JOIN
(SELECT ID FROM table1
) a
ON a.ID=b.ID
and play around with the query
Fiddle: http://sqlfiddle.com/#!3/c657d/13
And here is the MYSQL version:
SELECT DISTINCT COALESCE(t1.id, t2.id) id
FROM
(
SELECT TABLE_NAME <> 'table_a' n
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = SCHEMA()
AND TABLE_NAME IN('table_a', 'table_b')
) t LEFT JOIN table_a t1
ON t.n = 0 LEFT JOIN table_b t2
ON t.n = 1
Working fiddle: http://sqlfiddle.com/#!2/c657d8/34
I don't know why you are avoiding UNION but you can do like following
CREATE TABLE temp_ids(ID INT);
INSERT INTO temp_ids SELECT ID FROM a;
INSERT INTO temp_ids SELECT ID FROM b;
SELECT DISTINCT ID FROM temp_ids;
Try a full outer join and filter the NULL values.
As an abstract exercise (if this is an interview question we expect a kickback!) one ugly, innefficient solution would be to create a cartesian product and filter the unique values:
SELECT DISTINCT IF(a<>b, b.id, a.id)
FROM a, b
ORDER BY 1
;
Use FULL OUTER JOIN, like this:
SELECT CASE
WHEN t1.id IS NULL THEN t2.id
ELSE t1.id
END AS id
FROM t1
FULL OUTER JOIN t2
ON t1.id = t2.id
ORDER BY id
Note: Mysql does not support full outer joins.
Working demo: http://sqlfiddle.com/#!6/b7684/10
Let's say I have a Table that looks like this:
id fk value
------------
1 1 'lorem'
2 1 'ipsum'
3 1 'dolor'
4 2 'sit'
5 2 'amet'
6 3 'consetetur'
7 3 'sadipscing'
Each fk can appear multiple times, and for each fk I want to select the last row (or more precise the row with the respectively highest id) – like this:
id fk value
------------
3 1 'dolor'
5 2 'amet'
7 3 'sadipscing'
I thought I could use the keyword DISTINCT here like this:
SELECT DISTINCT id, fk, value
FROM table
but I am not sure on which row DISTINCT will return and it must be the last one.
Is there anything like (pseudo)
SELECT id, fk, value
FROM table
WHERE MAX(id)
FOREACH DISTINCT(fk)
I hope I am making any sense here :)
thank you for your time
SELECT *
FROM table
WHERE id IN (SELECT MAX(id) FROM table GROUP BY fk)
Try this:
SELECT a.id, a.fk, a.value
FROM tableA a
INNER JOIN (SELECT MAX(a.id) id, a.fk FROM tableA a GROUP BY a.fk
) AS b ON a.fk = b.fk AND a.id = b.id;
OR
SELECT a.id, a.fk, a.value
FROM (SELECT a.id, a.fk, a.value FROM tableA a ORDER BY a.fk, a.id DESC) AS a
GROUP BY a.fk;
Try this:
SELECT t.* FROM
table1 t
JOIN (
SELECT MAX(id) as id
FROM table1
GROUP BY fk
) t1
ON t.id = t1.id
Inner query will give you highest id for each fk using MAX(). Then we join this inner table with your main table.
You could also do
SELECT id, fk, value FROM table GROUP BY fk HAVING id = MAX(id)
I don't have mysql here, but it works in Sybase ASE
I've 2 table.
Table1
Has count about 2700 rows
Colums: ID, NO, NAME
Table2:
Has count about 300 rows
Colums: ID, NAME
where:
Table1.NO = Table2.ID
I want to list Table1(2700 rows) but if Table1 doesn't contain some of Table2's rows I want to write "NA"
How can i do that with SQL?
I assume you want to output the Name from table2, if it's present, in which case:
SELECT
a.id,
isnull(b.name, 'NA'),
a.name
FROM
table1 a
LEFT JOIN
table2 b
ON
a.no = b.id
will do it for you (I've also output the id and name from table1).
EDIT:
Apologies, I didn't see the MySQL tag until I posted. You will need to use the coalesce function instead if isnull, like so:
SELECT
a.id,
coalesce(b.name, 'NA'),
a.name
FROM
table1 a
LEFT JOIN
table2 b
ON
a.no = b.id
Try LEFT JOIN and ISNULL function like this below
SELECT ISNULL(Table1.Name,'NA')
FROM Table1
LEFT JOIN Table2
ON Table1.NO = Table2.ID