Sql get id where column1=value1 and column1=value2 - sql

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;

Related

How to do a sub-query in SQL

My table looks something like this:
I want to retrieve all the PractitionerIdFK if they have SpecialityIdFK = 1 AND SpecialityIdFK= 2. I tried the following but it doesn't seem to work.
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK IN (
SELECT PractitionerSpecialities.SpecialityIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK = 1
AND PractitionerSpecialities.SpecialityIdFK = 2
)
You can use GROUP BY and HAVING:
SELECT ps.PractitionerIdFK
FROM PractitionerSpecialities ps
WHERE ps.SpecialityIdFK IN (1, 2)
GROUP BY ps.PractitionerIdFK
HAVING COUNT(*) = 2; -- the size of the comparison list
This assumes that there are no duplicates in PractitionerSpecialities. If that is a possibility, then use HAVING COUNT(DISTINCT ps.SpecialityIdFK) = 2.
It can be achieved by using IN and BETWEEN operator in SQL .
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK in (1,2)
-- You can BETWEEN Clause as well ..
SELECT PractitionerSpecialities.PractitionerIdFK
FROM PractitionerSpecialities
WHERE PractitionerSpecialities.SpecialityIdFK BETWEEN 1 AND 2
In Sub query use OR operator instead of AND .

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)

SQL Select Entries with same Column Values

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

how to limit a sql integer query result to <=1

how to limit an integer query result to 1. a return of 2 to be 1, a return 1 to be 1, and a return of 0.5 to be 0.5 because it is <= 1. i don't want to modify the tables, i just want to modify the results.
This is my exact query.
select ((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT') ;
I'm using postgres.
To limit, you'd do something like this:
select
case
when yourNumber >= 1 then 1
else yourNumber
end
...
Then you just apply this concept to your query.
As noted by Wiseguy, you could also do:
select LEAST(yourNumber, 1)
, since this is postgresql.
The first solution will work with any ANSI SQL compatible database.
Update
Applied to your query, I think (if I understood what you want correctly) it would be like this:
select LEAST(1,
((select "V01" from sports where "UID" = '1') * 1.0 ) /
(select "V01" from master where "BALL" = 'REQUIREMENT')
);
use the LEAST function , docs: http://www.postgresql.org/docs/8.3/static/functions-conditional.html. Also, check out GREATEST too
SELECT LEAST(1, <your value>)
EDIT replaced GREATEST with LEAST
try this:
select CASE
WHEN ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT') >= 1
THEN 1
ELSE ((select V01 from sports where UID = '1') * 1.0 ) /
(select V01 from master where BALL = 'REQUIREMENT')
END;

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 ;