简体   繁体   中英

SQL select with conditions and without duplicates

I'm trying to create SQL query to get specific entities with some conditions. The thing is I have some duplicate entities I want to avoid. My data table (the table represent drivers) is: 在此处输入图片说明

I want to get some drivers with condition of specific facility ID/parkinglot ID. For example, in case I want all the drivers at facility with ID '2', I want to get:

11112 Michael Smith

and not:

11112 Michael Smith
11112 Michael Smith

I want the same thing will happen with the parkinglot ID and with the facility Id together.

I tried:

SELECT * FROM "DRIVERS" 
where facilityid = '2'
group by driverid

And I got an error:

Could not execute 'SELECT * FROM "DRIVERS" where facilityid = '2' group by driverid'
invalid column name: The column 'DRIVERS.FIRSTNAME' is invalid in the select list because the GROUP BY clause or an aggregation function does not contain it: line 1 col 8 (at pos 7)

Any ideas? Thank you!

If you just want to get the distinct values from the table then do:

SELECT DISTINCT *
FROM "DRIVERS"
WHERE facilityid = '2';

GROUP BY should be used when you are aggregating data

Edit: To get the results you asked for you could use:

SELECT DISTINCT DRIVERID, FIRSTNAME, LASTNAME
FROM "DRIVERS"
WHERE facilityid = '2';

YOU WILL GET THE RESULT WITH THIS QUERY. select distinct(DRIVERID),FIRSTNAME,LASTNAME FROM "DRIVERS" where facilityid = '2'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM