How to select the record with two different postcode in ms access with sql query - sql

I'm new in sql script. I'm currently trying to write a sql script to select the record that with two different postcode from a table in ms access with sql query. For some reason this is just not working for me.
Example:
I have the following table resident_postcode :
Postcode Name
1001 Alan
1002 James
1003 Alan
1004 Merry
1001 Merry
I write a sql script to select the name that have 2 different postcode and looking for the output like following:
Name
Alan
Merry
I have tried to run the following script but for some reason this is just not working in MS Acess:
SELECT a.Name
FROM resident_postcode a
WHERE 1 < (SELECT count(b.Postcode) FROM resident_postcode b WHERE b.Name= a.NameGROUP BY b.Name)
Does anyone know what is going wrong with my script?

Try with:
SELECT [Name]
FROM resident_postcode
GROUP BY [Name]
HAVING Count(*) > 1

If there is no repetition of same postcode for any name then you can have names with two post codes as below
SELECT NAME FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING COUNT(POSTCODE)=2
But if any postcode can be specified more than once (for example 1001 is specified more than once for Alan) then you need to use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)=2
if you want all names having two or more than 2 postcode then use below query:
select name from (
SELECT distinct NAME, postcode FROM RESIDENT_POSTCODE)
group by name
having count(*)>=2

If you want 2 or more different post codes, then you can use:
SELECT NAME
FROM RESIDENT_POSTCODE
GROUP BY NAME
HAVING MIN(POSTCODE) <> MAX(POSTCODE);
If you want exactly two, then you can use:
SELECT NAME
FROM (SELECT DISTINCT NAME, POSTCODE
FROM RESIDENT_POSTCODE
) AS NP
GROUP BY NAME
HAVING COUNT(*) = 2;

Related

SQLite query to get table based on values of another table

I am not sure what title has to be here to correctly reflect my question, I can only describe what I want.
There is a table with fields:
id, name, city
There are next rows:
1 John London
2 Mary Paris
3 John Paris
4 Samy London
I want to get a such result:
London Paris
Total 2 2
John 1 1
Mary 0 1
Samy 1 0
So, I need to take all unique values of name and find an appropriate quantity for unique values of another field (city)
Also I want to get a total quantity of each city
Simple way to do it is:
1)Get a list of unique names
SELECT DISTINCT name FROM table
2)Get a list of unique cities
SELECT DISTINCT city FROM table
3)Create a query for every name and city
SELECT COUNT(city) FROM table WHERE name = some_name AND city = some_city
4)Get total:
SELECT COUNT(city) FROM table WHERE name = some_name
(I did't test these queries, so maybe there are some errors here but it's only to show the idea)
As there are 3 names and 2 cities -> 3 * 2 = 6 queries to DB
But for a table with 100 cities and 100 names -> 100 * 100 = 10 000 queries to DB
and it may take a lot of time to do.
Also, names and cities may be changed, so, I can't create a query with predefined names or cities as every day it's new ones, so, instead of London and Paris it may be Moscow, Turin and Berlin. The same thing with names.
How to get such table with one-two queries to original table using sqlite?
(sqlite: I do it for android)
You can get the per-name results with conditional aggregation. As for the total, unfortunately SQLite does not support the with rollup clause, that would generate it automatically.
One workaround is union all and an additional column for ordering:
select name, london, paris
from (
select name, sum(city = 'London') london, sum(city = 'Paris') paris, 1 prio
from mytable
group by name
union all
select 'Total', sum(city = 'London'), sum(city = 'Paris'), 0
from mytable
) t
order by prio, name
Actually the subquery might not be necessary:
select name, sum(city = 'London') london, sum(city = 'Paris') paris, 1 prio
from mytable
group by name
union all
select 'Total', sum(city = 'London'), sum(city = 'Paris'), 0
from mytable
order by prio, name
#GMB gave me the idea of using group by, but as I do it for SQLite on Android, so, the answer looks like:
SELECT name,
COUNT(CASE WHEN city = :london THEN 1 END) as countLondon,
COUNT(CASE WHEN city = :paris THEN 1 END) as countParis
FROM table2 GROUP BY name
where :london and :paris are passed params, and countLondon and countParis are fields of the response class

how to get combine from Two select statements

my table is,
ID First_Name Last_name manager_ID Unique_ID
12 Jon Doe 25 CN=Jon Doe, DC=test,DC=COM
25 Steve Smith 39 CN=steve smith, DC=test,dc=com
I want to write a sql that will give me manager's unique ID,
select manager_id from test where ID = '12'
this will give me users manager_ID
select unique_id from test where ID = '25'
can i combine above sql in one statement that will give me user's manager's unique_id as output?
You are looking for a self-join:
select m.unique_id
from test t join
test m
on t.manager_id = m.id
where t.ID = 12;
Note that I remove the single quotes around 12. Presumably, id is an integer. You should not be comparing an integer to a string.
Instead of joining it to the same table, you can also make a nested subquery statement like this.
SELECT unique_id FROM test WHERE ID =(SELECT manager_id FROM test WHERE ID = 12);
The inner query outputs the manager_id where id of person equals 12 and the outer query gives the unique_id of the related manager.

SQL - Removing Duplicate without 'hard' coding?

Heres my scenario.
I have a table with 3 rows I want to return within a stored procedure, rows are email, name and id. id must = 3 or 4 and email must only be per user as some have multiple entries.
I have a Select statement as follows
SELECT
DISTINCT email,
name,
id
from table
where
id = 3
or id = 4
Ok fairly simple but there are some users whose have entries that are both 3 and 4 so they appear twice, if they appear twice I want only those with ids of 4 remaining. I'll give another example below as its hard to explain.
Table -
Email Name Id
jimmy#domain.com jimmy 4
brian#domain.com brian 4
kevin#domain.com kevin 3
jimmy#domain.com jimmy 3
So in the above scenario I would want to ignore the jimmy with the id of 3, any way of doing this without hard coding?
Thanks
SELECT
email,
name,
max(id)
from table
where
id in( 3, 4 )
group by email, name
Is this what you want to achieve?
SELECT Email, Name, MAX(Id) FROM Table WHERE Id IN (3, 4) GROUP BY Email;
Sometimes using Having Count(*) > 1 may be useful to find duplicated records.
select * from table group by Email having count(*) > 1
or
select * from table group by Email having count(*) > 1 and id > 3.
The solution provided before with the select MAX(ID) from table sounds good for this case.
This maybe an alternative solution.
What RDMS are you using? This will return only one "Jimmy", using RANK():
SELECT A.email, A.name,A.id
FROM SO_Table A
INNER JOIN(
SELECT
email, name,id,RANK() OVER (Partition BY name ORDER BY ID DESC) AS COUNTER
FROM SO_Table B
) X ON X.ID = A.ID AND X.NAME = A.NAME
WHERE X.COUNTER = 1
Returns:
email name id
------------------------------
jimmy#domain.com jimmy 4
brian#domain.com brian 4
kevin#domain.com kevin 3

Stuck in writing sql query

i have a table where termid,termversion are two primary key.
column present in table - termid,termversion,name
I want to select all term whose name ilike '%AB%' and the result should contain each matching term with maximum version number.
Example:
id name ver
1 ABBBB 1
1 ABBBB 2
1 ABBBB 3
2 ABC 1
2 ABC 2
output should come
1 ABBBB 3
2 ABC 2
I want to write this query in hibernate using
Criteria...if any one can suggest me in hibernate then its really good else at least help me in writing the sql query.
PS I am using postgresql
I think that is what you are looking for:
SELECT id, name, MAX(ver)
FROM table
WHERE name ILIKE '%AB%'
GROUP BY name, id -- I assume: id == id' <==> name == name'
is it?
select id, name, max(ver) from mytable where name like '%AB%' group by id, name
can you try this sql command.
select id,name,max(ver) from table_name group by id, name having name like '%AB%'

how to find people with same family name?

You have a table with 4 columns:
Primary key / name / surname / middle name
How to write Sql query to find people who has same family name?
1 / Ivan / Ivanov / Ivanovich
2 / Petr / Levinsky / Aleksandrovich
3 / Alex / Ivanov / albertovich
Should return Ivan and Alex
Thanks
In standard SQL you can simply join the table with itself:
select a.name, b.name
from t as a, t as b
where a.surname = b.surname and a.id < b.id
where t is your table and id is the primary key column.
This returns all distinct pairs of first names for every surname that has multiple entries.
You might want to add surname to the list of selected columns.
If you want to find exactly names then you should firstly find all surnames that appear more than once and the find all names:
select name
from t
where surname in (select surname from t group by surname having count(surname) > 1);
As for me easiest way is to group records by surname and then select those with count more than 1.
You want to GROUP BY the surname and then use a HAVING clause to find any groups that have > 1.
Untested:
SELECT
name
FROM
theTable
WHERE Surname IN (
SELECT
Surname
FROM
theTable
GROUP BY
Surname
HAVING
COUNT(Surname) > 1)
select surname,group_concat(firstname)
from people
group by surname
having count(firstname)> 1;