Isolate records that have multiple values (derived table) - ms-access

Over simplified I have a table that looks like this:
ID - NAME - EMAIL
1 - JIM - Jim#hotmail.com
2 - JIM - Jim#gmail.com
3 - BARRY - Barry#hotmail.com
4 - JACK - Jack#hotmail.com
I would like to query this table and return only the fields for which NAME has multiple EMAIL values, so requied result is:
NAME - EMAIL
JIM - Jim#hotmail.com
JIM - Jim#gmail.com
The other two (Barry and Jack) would not need to be returned as they have only one value for email.
I am struggeling a bit with a derived table set-up in Access, the following will not work because it gives me an error in the FROM clause.. Is there another syntax?
select DISTINCT x.ID, x.NAME, x.EMAIL
from t as x
join (
select ID
from t
group by ID
having count(distinct EMAIL) > 1
) as y
on x.ID = y.ID

Try this
Select z.Name, t.Email from (
SELECT t.Name
FROM t
GROUP BY t.Name
HAVING Count(t.[Name])>1
) as z INNER JOIN t ON z.name=t.name

Try this:
select x.ID, x.NAME, x.EMAIL
from t as x
where x.NAME in (
select t.NAME
from t
group by t.NAME
having count(*) > 1)

Related

Filter out records based on condition SQL

I am trying to filter out some records based on condition but couldn't get the proper results.
Data:
GID OID SID Z
1 1 1 A
1 2 2 B
1 3 3 C
1 2 4 B
Expected Result:
GID OID SID Z
1 1 1 A
1 3 3 C
Here GID, OID can be repeated but not SID.
Need to filter out all records where Z contains 'A' & 'C'
What I have tried:
select distinct GID, OID, SID, Z
from table
where Z ilike ('A') or Z ilike ('C')
but this query will include all record of sample GID records.
Moreover I have also thought of self join but could not frame the query around that.
I think this is the query you need
WITH cte AS (
SELECT
gid, oid, sid, z,
ROW_NUMBER() OVER () AS rn
FROM data
WHERE LOWER(z) LIKE '%a%'
OR LOWER(Z) LIKE '%c%'
)
SELECT gid, oid, sid, z FROM cte c
WHERE NOT EXISTS (
SELECT sid FROM cte t
WHERE t.z = c.z
AND t.sid = c.sid
AND t.rn < c.rn
)
I use ROW_NUMBER to be able to check if a sid value repeats.
Demo

how to get the non duplicates?

I need to make a request in SQL.
I have a field that contains IDs.
These IDs are written in 2 ways and are prefixed either by'C0' or by'E0' for example: "C0121213" or "E0121213".
I would like to make a query allowing me to find the number of IDs starting with C0 but not duplicating starting with E0.
That is, I would like to find IDs that do not have C0 or E0 pairs.
Thank you in advance
I started with a request :
SELECT *
FROM SBYN
WHERE ID IN (
SELECT LID
FROM SBYN
WHERE LEFT(ID,2) = 'C0'
OR LEFT(ID, 2) = 'E0'
GROUP BY LID HAVING COUNT(*) > 1
)
ORDER BY ID
NOT EXISTS comes to mind:
SELECT COUNT(*)
FROM SBYN s
WHERE s.ID LIKE 'C0%' AND
NOT EXISTS (SELECT 1
FROM SBYN s2
WHERE s2.ID LIKE 'E0%' AND
SUBSTRING(s2.ID, 2) = SUBSTRING(s.ID, 2)
);
If you want the IDs, then use SELECT ID rather than SELECT COUNT(*).
Using EXISTS:
SELECT
ID
FROM SBYN s1
WHERE
ID LIKE 'C0%' AND
NOT EXISTS (SELECT 1 FROM SBYN s2
WHERE s2.ID LIKE 'E0' AND
SUBSTRING(s1.ID, 3) = SUBSTRING(s2.ID, 3));
With NOT EXISTS:
select * from sbyn s
where not exists (
select 1 from sbyn
where left(id, 2) <> left(s.id, 2) and
right(id, 3, length(id)) = right(s.id, 3, length(s.id))
)
This will return all the non duplicates.
If you care only about those starting with C0 add to the where clause:
and left(s.id) = 'C0'

Mysql Query select one row from the same values (select duplicate only)

I have duplicate data on my table as described bellow.
no name adrress
1 Joe No.3
2 Joe No.2
3 Joe No.1
4 Anna No.4
5 Anna No.5
6 Ali No.6
I want to show only the first item from duplicate data like bellow.
no name address
1 Joe No.3
2 Anna No.4
If you mean to find the duplicate only, this one similar to your question here
for your case will be like this
SELECT x.*
FROM new_table x
JOIN
( SELECT name
, MIN(id) as min_id , COUNT(id) as count_id
FROM new_table
GROUP
BY name
) y
ON y.name = x.name
AND y.min_id = x.id
AND y.count_id > 1
ORDER
BY id;
hope that answer your question.
There could be lot of answers to this question see which one fits your requirements:
Select * from tablename where no=1;
Select * from tablename where adrress LIKE "%3%";
Select * from tablename ORDER BY no LIMIT 1;
You can use this query for selecting particular row in the table
select * from yourtablename WHERE primarykeyfield=" ";
Try this:
mysql_query("select * from $table where id='1';");

select max() from result of count for distinct ids in mysql

I have a table with the following structure:
id name
1 X
1 X
1 Y
2 A
2 A
2 B
Basically what I am trying to do is to write a query that returns X for 1 because X has repeated more than Y (2 times) and returns A for 2. So if a value occurs more than the other one my query should return that. Sorry if the title is confusing but I could not find a better explanation. This is what I have tried so far:
SELECT MAX(counted) FROM(
SELECT COUNT(B) AS counted
FROM table
GROUP BY A
) AS counts;
The problem is that my query should return the actual value other than the count of it.
Thanks
This should work:
SELECT count(B) as occurrence, A, B
FROM table
GROUP BY B
ORDER BY occurrence DESC
LIMIT 1;
Please check: http://sqlfiddle.com/#!9/dfa09/3
You can try like this using a GROUP BY clause. See a Demo Here
select *, max(occurence) as Maximum_Occurence from
(
select B, count(B) as occurence
from table1
group by B
) xxx
This is how I finally handled my problem. Not the most efficient way but get the job done:
select A,B from
(select A,B, max(cnt) from
(select A ,B ,count(B) as cnt
from myTable
group by A,B
order by cnt desc
) as x group by A
) as xx

Select inside math function

This is most likely a beginner's question in SQL. Is it possible to use a select within a math expression?
For example, I have two tables:
- table A with a column named id (primary key) and another column named val_A
- table B with a column named id (primary key) and another column named val_B
I want to do something like:
select ((select val_A from A where id = 1) +
(select val_B from B where id = 1)) as final_sum;
I'm using MySQL and it is throwing errors. I'm assuming that this is because the result of a select is a set and I want the numeric value of val_A and val_B to be make the sum.
Is there any way of doing this?
Thanks!
The query that you have:
select ((select val_A from A where id = 1) +
(select val_B from B where id = 1)
) as final_sum
is correctly formed SQL in MySQL (assuming that the table and columns exist).
However, it assumes that each subquery only returns one row. If not, you can force it using limit or a function like min() or max():
select ((select val_A from A where id = 1 limit 1) +
(select max(val_B) from B where id = 1)
) as final_sum
Or, possibly, you are trying to get the sum of all the rows with id = 1 in both tables:
select ((select sum(val_A) from A where id = 1) +
(select sum(val_B) from B where id = 1)
) as final_sum
Yes you can do that, but a more proper query format would be:
SELECT (a.val_a + b.val_b) as final_sum
FROM a INNER JOIN b ON a.id = b.id
WHERE a.id = 1
I'm not sure why it's not working, but you could try something like:
select (val_A + val_B) as final_sum from A,B where A.id=1 and B.id=1;
Break down and test your query
select 1+1
so your statement is just without the select. This would run -
select ((select sum(val_A) from A where id = 1) +
(select sum(val_B) from B where id = 1)) as final_sum;