I have an SQL database with several tables of patient data. Every table has one column in common, an ID number representing each patient. There is significant overlap between the tables, i.e. the same patient ID number often appears on multiple tables. What I would like to do is SELECT all distinct patient ID numbers that do not appear on one specific table.
You can use UNION and NOT IN like this:
select id
from (
select id from table1
union
select id from table2
union
select id from table3
...
) t where id not in (
select id from sometable
);
Related
The following query:
SELECT * FROM table WHERE id IN (1,2,3);
will return three records.
SELECT * FROM table WHERE id IN (1,2,1);
will return just two records (for Ids 1 and 2)
Is there a way for the result set to contain two records for Id 1 (and three in total)?
You could try creating a table for the ids you want to filter by. This would get you your desired results. I'm not sure if mysql supports CTE, but hopefully this is enough for you to get the idea.
WITH IDS
AS
(
SELECT 1 AS id
UNION ALL
SELECT 2 AS id
UNION ALL
SELECT 1 AS id
)
SELECT T.*
FROM T
JOIN IDS
ON T.id = IDS.id
I am trying to get the table name within the sql query that I am writing to get data from the same table, for example
Table_one
TABLE_TWO
So while doing this query Select * from table_one UNION Select * from table_two
the result that i get is the data of two tables in one table like this:
But what I need that one more column to come in the table to see the data is coming from which table as following
Use union all:
select id, name, age, 'table_one' as tabel_name
from table_one
union all
select id, name, age, 'table_two'
from table_two
I have two tables, training_taken_november and qtr1_copy. Each of them have a unique field. I want to join these two tables and count the unique values from both tables where their category = 'livestock', illustration below:
Tbl1 values might be: 1,2,3,4,5
Tbl2 values might be: 6,7,8,1,2
The count of distinct values should return: 8, that is, when we join the two tables they have 8 unique values in mentioned fields (name of fields that have unique values also differ).
Select count(*) from
(
Select id from ... Where category...
Union
Select id from ... Where cat ...
) x
Try this:
Select count(*) from
(select tazkira from Training_taken_november where category='Livestock'
union
select bene from qtr1_copy where cat='Livestock')x
I have four tables with same fields. Now I want to join these tables in such a way that I retrieve records only if there is a match between any two tables on a field(like name).
Thanks in advance.
This would return all the name values that appear in more than one table:
select
name
from
(select distinct
name
from table1
union all
select distinct
name
from table2
union all
select distinct
name
from table3
union all
select distinct
name
from table4) temp
group by name
having count(*) > 1;
Check out the interactive example.
I have two tables, both having column a device_id column that I want to count. For the purposes of demonstration, the schema looks like:
Table 1: 'id', 'save_val', 'device_id_major'
Table 2: 'id', 'save_val', 'location', 'device_id_team'
Table 1 could have many of the same 'device_id_major'.
I basically want to get the unique device_id's from both tables, then from that result set, get the count of unique device_id's (the same device_id can appear in both tables).
Is this possible in one query?
select distinct aa.device_id, count(*)
from(select distinct device_id from table1
union all
select distinct device_id from table2) as aa
group by device_id
order by device_id
Or something like... As I don't have the schema to hand, I can't fully validate it.
SELECT count(DISTINCT aa.id)
FROM (SELECT DISTINCT major_id AS id FROM `major`
UNION ALL
SELECT DISTINCT team_id AS id FROM `team`)
AS aa
This seems to do the trick.
You could use a query that takes the UNION of both tables, then SELECT the unique values.