Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i prevent duplicate rows being selected in a select query?

I have been given the task of selecting key data from an Oracle database, but I am noticing that my select is returning duplicate rows. I don't need them for my report yet I don't want them to delete them. Could someone help to get only the data that I need. I have tried the following code but this doesn't help.

SELECT distinct bbp.SUBCAR "Treadwell",
       bbp.BATCH_ID "Batch ID",
       bcs.SILICON "Si",
       bcs.SULPHUR "S",
       bcs.MANGANESE "Mn",
       bcs.PHOSPHORUS "P",
       to_char(bcs.SAMPLE_TIME, 'dd-MON-yy hh24:MI') "Sample Time",
       to_char(bbp.START_POUR, 'dd-MON-yy hh24:MI') "Start Pour Time",
       to_char(bbp.END_POUR, 'dd-MON-yy hh24:MI') "End pour Time",
       bofcs.temperature "Temperature"
FROM  bof_chem_sample bcs, bof_batch_pour bbp, bof_celox_sample bofcs
WHERE bcs.SAMPLE_CODE= to_char('D1')
AND bbp.BATCH_ID=bcs.BATCH_ID
AND bcs.SAMPLE_TIME>=to_date('01-jan-10')
like image 498
LaDante Riley Avatar asked May 19 '11 20:05

LaDante Riley


People also ask

How do I avoid duplicates in select query?

The SQL DISTINCT keyword, which we have already discussed is used in conjunction with the SELECT statement to eliminate all the duplicate records and by fetching only the unique records.

How can you eliminate duplicate rows from a query result?

SQL Delete Duplicate Rows using Group By and Having Clause According to Delete Duplicate Rows in SQL, for finding duplicate rows, you need to use the SQL GROUP BY clause. The COUNT function can be used to verify the occurrence of a row using the Group by clause, which groups data according to the given columns.

Which query is used to avoid duplicate records?

A simple SQL query allows you to retrieve all duplicates present in a data table.


1 Answers

If your SELECT statement has a DISTINCT in it, then all of the returned records have a unique combination of values across the columns you are selecting. You need to identify which columns return different values across the records you deem to be duplicated.

like image 129
Datajam Avatar answered Sep 30 '22 14:09

Datajam