-3

I want to get unique value form table. But all values should be unique.

So suggest how to get.

SELECT DISTINCT ProCode
           , id,SubCat
           ,SmlImgPath
           ,RupPrice
           ,ActualPrice
           ,ProName
 FROM product
 WHERE ProCode='FZ10003-EBA';
Mzf
  • 5,210
  • 2
  • 24
  • 37

3 Answers3

0

DISTINCT refers to all selected columns, so the answer is that your SELECT already does that.

EDIT:

It seems your problem isn't related to DISTINCT. What you want is to get a single row when your search returns multiple rows.

If you don't care which row you get then you can use:

MS SQL Server syntax:

SELECT TOP 1  ProCode
           , id,SubCat
           ,SmlImgPath
           ,RupPrice
           ,ActualPrice
           ,ProName
 FROM product
 WHERE ProCode='FZ10003-EBA';

MYSQL syntax:

SELECT ProCode
           , id,SubCat
           ,SmlImgPath
           ,RupPrice
           ,ActualPrice
           ,ProName
 FROM product
 WHERE ProCode='FZ10003-EBA'
 LIMIT 1;

Oracle syntax:

SELECT ProCode
           , id,SubCat
           ,SmlImgPath
           ,RupPrice
           ,ActualPrice
           ,ProName
 FROM product
 WHERE ProCode='FZ10003-EBA'
   AND rownum <= 1;
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • Please provide some rows from your table – Ullas Sep 02 '14 at 11:08
  • FZ10003-EBA ~/Products/CELEBRITY/KANGANA RANAUT/FZ10003-EBA-sm.jpg FZ10003-EBA ~/Products/SALWAR KAMEEZ/DESIGNER WEAR/FZ10003-EBA-sm.jpg FZ10003-EBA ~/Products/SALWAR KAMEEZ/PARTY WEAR/FZ10003-EBA-sm.jpg – Sandeep Pal Sep 02 '14 at 11:08
  • 1
    Add this in your question – Ullas Sep 02 '14 at 11:09
  • If you want a single value, then DISTINCT is not what you want. You need to search for a unique value, that is you need to have a where clause that includes all columns in a unique index. – Klas Lindbäck Sep 02 '14 at 11:36
0

(one day I may be able to post comments!)

SQLFiddle to show normal, distinct and returning a single row

SELECT DISTINCT works fine but it doesn't work the way you want it to work. From the data you posted in the comment under Klas' answer, it's clear you're expecting a single result when there are data differences somewhere in the columns. For example

    /Products/CELEBRITY/KANGANA

is completely DISTINCT from

    /Products/SALWAR

What you appear to be looking for cannot work with DISTINCT nor can it work with GROUP BY. Basically the only way two (or three, or ten, or 100) rows will become ONE row is if the data in ALL SEVEN COLUMNS in your SELECT are IDENTICAL.

Take a step back and think about what it is, exactly, you're trying to achieve here.

Dave Brown
  • 490
  • 4
  • 11
0

Are you saying that you want one record only? This is called aggregation. In case there are more records then one (three in your example), you would have to decide for each column, which value to show.

Which SubCat, which SmlImgPath, etc. do you want to see in your result line? The maximum value? The minimum? Or the string 'various'? An example:

SELECT 
    ProCode
  , CASE WHEN MIN(id) <> MAX(id) THEN 'various' ELSE MIN(id) END
  , MIN(SubCat)
  , MAX(SmlImgPath)
  , AVG(RupPrice)
  , AVG(ActualPrice)
  , MAX(ProName)
FROM product
WHERE ProCode='FZ10003-EBA'
GROUP BY ProCode;
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73