SQL Select Entries with same Column Values - sql

Imagine that I have a column like this:
Var: 1, 1, , 3, 2
-
Name: Ben, John, Josh, Bill
How can I select Entries with the same VAR column Value? Like, if I want entries with value 1 in the VAR column, it will give me Ben and Josh.

This will give you records having multiple records of the same VAR.
SELECT a.*
FROM TableName a
WHERE EXISTS
(
SELECT 1
FROM TableName b
WHERE a.Var = b.Var
GROUP BY Var
HAVING COUNT(*) > 1
)
SQLFiddle Demo
Another way to solve this is by using JOIN,
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT Var
FROM TableName b
GROUP BY Var
HAVING COUNT(*) > 1
) b ON a.Var = b.Var
SQLFiddle Demo
But adds a little confusion when you add this line: "..if I want entries with value 1 in the VAR column, it will give me: Ben and Josh" -- Do you want to specify VAR or not? Like this demo <<

Try
SELECT name
FROM table1
WHERE var IN (SELECT MIN(var)
FROM table1
GROUP BY var
HAVING COUNT(*) > 1)
Here is SQLFiddle demo.

this question is confusing, does a select not work?
select name from theTable
where var = 1

Related

SQL Server - How to check if a value does not exist in other rows of the same table for same column values?

Following are the two tables in SQL Server: TABLE_A and TABLE_B
I need to get the output as follows:
Get IDs from TABLE_A where Exist = 0
We would get 100, 101 & 102
Now, among 100, 101 & 102, no other rows (in the same table) with the same ID value should have Exist = 1
Hence, 100 can't be selected as it has Exist = 1 in the 2nd row.
So, only 101 & 102 remain
With the remaining ID values (101 & 102), check against the ID column in TABLE_B where 'Exist' column value should not be equal to '1' in any of the rows
In TABLE_B, 4th row has Exist = 1 for 102. So, that can't be selected
We have only 101 now. This is required output and that should be selected.
Could you let me know how to write the simplest query to achieve this please? Let me know if the question needs to be improved.
You can use exists & not exists :
with t as (
select t1.*
from t1
where exists (select 1 from t1 t11 where t11.id = t1.id and t11.exists = 0) and
not exists (select 1 from t1 t11 where t11.id = t1.id and t11.exists = 1)
)
select t.*
from t
where not exists (select 1 from t2 where t.id = t2.id and t2.exists = 1);
Try:
SELECT
ID,
SUM(CAST(Exist AS int)) AS [Exists]
FROM
TABLE_A
GROUP BY ID
HAVING SUM(CAST(Exist AS bit)) = 0
will give you the answer to the first part. You can then JOIN this to a similar query for TABLE_B. That is a "simple" way to show how this works. You can write more complex queries as that from #Yogest Sharma
Like #Peter Smith mentioned, you can use the aggregate function SUM. Note that you would need a cast since you cannot use the aggregate function on a field that has a BIT datatype
;WITH CTE AS
(
SELECT ID, SUM(CAST(Exist AS INT)) AS AggExist FROM TABLE_A GROUP BY ID
UNION
SELECT ID, SUM(CAST(Exist AS INT)) As AggExist FROM TABLE_B GROUP BY ID
)
SELECT ID, SUM(AggExist) FROM CTE GROUP BY ID
HAVING SUM(AggExist) = 0
Here is the demo

Sql get id where column1=value1 and column1=value2

I need to get the tour_id where the tour_types_id is 7 AND 1
So in this case (image) i need:
213,215,223,225
I feel is a simple question but i can't figure out :(
Thanks.
[EDIT] Sorry where the tour_types_id is 7 (not 5) AND 1. Pot corrected
EDIT:
Since you want all tour_ids that have tour_types_id 1 AND 7, you can use this:
SELECT DISTINCT tour_id FROM tablename WHERE tour_types_id = 1 OR tour_types_id = 7 GROUP BY tour_id HAVING count(DISTINCT tour_types_id) = 2
PREVIOUS:
Maybe you're thinking about OR? If you want to get all tour_id that have tour_types_id = 5 or tour_types_id = 1, you need an OR. In english, the statement means: Select all the tour_ids from the table tablename where tour_types_id is equal to 1 or equal to 5.
SELECT tour_id FROM tablename WHERE tour_types_id = 1 OR tour_types_id = 5;
Use the query SELECT tour_id FROM tablename WHERE tour_types_id = 5 OR tour_types_id = 1
with tablename the actual name of the table you're using.
Your question is malformed though. You do not provide enough information (tablename?)
From the results you want, it seems you want an OR not an AND.
You can use in clause too after the where like
SELECT tour_id FROM [Table_Name] WHERE tour_type_id IN (1,5)
this can be more flexible for you if you want to add or remove some ids
I found this solution:
SELECT DISTINCT tour_id FROM tablename t1
WHERE EXISTS (SELECT * FROM tablename WHERE tour_types_id = '1' AND tour_id = t1.tour_id)
AND EXISTS (SELECT * FROM tablename WHERE tour_types_id = '7' AND tour_id = t1.tour_id)
Sorry if i wasted your time, i was searching for hours.
Thanks for the help
Use an aggregation query with having:
select tour_id
from t
where tour_types_id in (1, 7)
group by tour_types_id
having count(distinct tour_types_id) = 2;
Try this query:
select tour_id
from xxxxtable
where tour_types_id = 1
OR tour_types_id = 5;

select specific records using IN

I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)

Get data from table using group by

I have a Emptbl in which I have EmpType Column.
In EmpType I have following data for example :
E0123
M0123
E1245
E4578
M1245
E0478
M4789
E4762
Now I want to get only those emp data which have same EmpType for example below data:
E0123
M0123
E1245
M1245
And want to show this data as group by as 0123 and 1245
So how to get above data? I use UNION but it does not get valida data.
Thanks
Try this:
select substring(emptype, 2, len(emptype))
from emptbl
group by substring(emptype, 2, len(emptype))
having count(*) > 1
The hard-coded 2 is based on your sample data. If instead you had an arbitrary number of letters before the numeric part, e.g. 'ABCDEFG0123', you could use patindex to get the starting index for your substring like so;
select substring(emptype, patindex('%[0-9]%',emptype), len(emptype)
Select A.*
From EmpTbl A
Inner Join
EmpTbl B
On SubString(A.EmpType, 2, 4) = SubString(B.EmpType, 2, 4) And
SubString(A.EmpType, 1, 1) <> SubString(B.EmpType , 1, 1)
;with CTE as (Select Name,SUBSTRING(Name,2,5) as Number, ROW_NUMBER()
OVER (PARTITION By SUBSTRING(Name,2,5) ORDER BY Name) AS Row
from #Temp)
Select Temp.Name
From CTE C
Cross Apply (Select Name FRom CTE T Where T.Number=C.Number) as Temp
Where C.Row>1
Here is the fiddle sample
select top 4 id from
(
select id,rn=row_number()over(partition by right(id,3) order by right(id,3)) from #t
)x
SEE IT LIVE
This is the smallest query that works:
select substr(a.emptype, 2) num
join emptbl a
join emptbl b on substr(a.emptype, 2) = substr(b.emptype, 2)
and a.emptype != b.emptype

Fetch unique combinations of two field values

Probably it has been asked before but I cannot find an answer.
Table Data has two columns:
Source Dest
1 2
1 2
2 1
3 1
I trying to come up with a MS Access 2003 SQL query that will return:
1 2
3 1
But all to no avail. Please help!
UPDATE: exactly, I'm trying to exclude 2,1 because 1,2 already included. I need only unique combinations where sequence doesn't matter.
For Ms Access you can try
SELECT DISTINCT
*
FROM Table1 tM
WHERE NOT EXISTS(SELECT 1 FROM Table1 t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
EDIT:
Example with table Data, which is the same...
SELECT DISTINCT
*
FROM Data tM
WHERE NOT EXISTS(SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
or (Nice and Access Formatted...)
SELECT DISTINCT *
FROM Data AS tM
WHERE (((Exists (SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source))=False));
your question is asked incorrectly. "unique combinations" are all of your records. but i think you mean one line per each Source. so it is:
SELECT *
FROM tab t1
WHERE t1.Dest IN
(
SELECT TOP 1 DISTINCT t2.Dest
FROM tab t2
WHERE t1.Source = t2.Source
)
SELECT t1.* FROM
(SELECT
LEAST(Source, Dest) AS min_val,
GREATEST(Source, Dest) AS max_val
FROM table_name) AS t1
GROUP BY t1.min_val, t1.max_val
Will return
1, 2
1, 3
in MySQL.
To eliminate duplicates, "select distinct" is easier than "group by":
select distinct source,dest from data;
EDIT: I see now that you're trying to get unique combinations (don't include both 1,2 and 2,1). You can do that like:
select distinct source,dest from data
minus
select dest,source from data where source < dest
The "minus" flips the order around and eliminates cases where you already have a match; the "where source < dest" keeps you from removing both (1,2) and (2,1)
Use this query :
SELECT distinct * from tabval ;