Something like DISTINCT or DISTINCTROW, but what ? 
Author Message
 Something like DISTINCT or DISTINCTROW, but what ?

Hi Zeljko,

if you select a unique field in your query you cannot distinct duplicate
values in any field, because every record of the table will be in the
resulting recordset. If you want to distinct duplicate values you can group
the records with the GROUP BY - command. But you have to include all fields
from your select-statement, that aren't used with aggregate-functions (SUM,
COUNT etc.). ie.

SELECT F1,F2,F3,F4, COUNT(F5) FROM table GROUP BY F1,F2,F3,F4;

With this query you will get the F1-F2-F3-F4 - combinations only once - plus
the number of records with this combinations.

I hope this will help you.
--
CU  Ronald
-> Life would be easier if we had the source code.

--


Quote:
> Hi. I know almost nothing about the SQL. so don't laugh at
> this question:

> I need to collect, let's say, 5 fields from the table, but to return
> only unique values from 4 of them. I'll try to be more specific: I
> have fields F1, F2, F3, F4, F5, and F5 is always different (primary
> key). I need to return that field also as a result of a query, but to
> make the rest of the fields unique. Something like:

> SELECT DISTINCT F1,F2,F3,F4,F5 FROM table,

> just that I want F5 field to be excluded from duplicate checking.

> How can I do that ? I'm sure there is an easy
> answer , but it's so far from me at this moment.
> Best regards,
>        Zeljko

> We are born {*filter*}, wet, and hungry.
> Then things got worse.

> Remove "die.spammer" before replying



Mon, 11 Feb 2002 03:00:00 GMT  
 Something like DISTINCT or DISTINCTROW, but what ?
This can be done as follows:

SELECT F1, F2, F3, F4, MIN(F5)
FROM TABLE
GROUP BY F1, F2, F3, F4

You will get one row for each F1-F2-F3-F4 combination, with the lowest F5
value present for each combination. If any F5 values are NULL, they will not
appear in the result.


Quote:
> Hi, Ronald.

> Thanks for the reply. Unfortunately, this doesn't suit my needs,
> because I need to return primary key also, as a result of the query.
> Maybe my post was somewhat vague. I'll try to clear it up a bit. If I
> have this situation in the table:

> F1 F2 F3 F4 F5

> A B C D 1
> A B C D 2

> I need query to return the folowing:

> F1 F2 F3 F4 F5

> A B C D 1 <= "1" is the first occurence

> of combination "ABCD".

> Now, when I think of it a bit more, I'm not sure this is possible,
> but, "almost nothing" very well describes my SQL knowledge, so...



> >Hi Zeljko,

> >if you select a unique field in your query you cannot distinct duplicate
> >values in any field, because every record of the table will be in the
> >resulting recordset. If you want to distinct duplicate values you can
group
> >the records with the GROUP BY - command. But you have to include all
fields
> >from your select-statement, that aren't used with aggregate-functions
(SUM,
> >COUNT etc.). ie.

> >SELECT F1,F2,F3,F4, COUNT(F5) FROM table GROUP BY F1,F2,F3,F4;

> >With this query you will get the F1-F2-F3-F4 - combinations only once -
plus
> >the number of records with this combinations.

> >I hope this will help you.
> >--
> >CU  Ronald
> >-> Life would be easier if we had the source code.

> Best regards,
>        Zeljko

> We are born {*filter*}, wet, and hungry.
> Then things got worse.

> Remove "die.spammer" before replying



Fri, 15 Feb 2002 03:00:00 GMT  
 Something like DISTINCT or DISTINCTROW, but what ?
        Thank you for replying, I'll try it. I had some virus problems
in a last couple of days, so I'm late with reply.

        Zeljko

On Mon, 30 Aug 1999 20:43:51 -0700, "Kevin P. Fleming"

Quote:

>This can be done as follows:

>SELECT F1, F2, F3, F4, MIN(F5)
>FROM TABLE
>GROUP BY F1, F2, F3, F4

>You will get one row for each F1-F2-F3-F4 combination, with the lowest F5
>value present for each combination. If any F5 values are NULL, they will not
>appear in the result.



>> Hi, Ronald.

>> Thanks for the reply. Unfortunately, this doesn't suit my needs,
>> because I need to return primary key also, as a result of the query.
>> Maybe my post was somewhat vague. I'll try to clear it up a bit. If I
>> have this situation in the table:

>> F1 F2 F3 F4 F5

>> A B C D 1
>> A B C D 2

>> I need query to return the folowing:

>> F1 F2 F3 F4 F5

>> A B C D 1 <= "1" is the first occurence

>> of combination "ABCD".

>> Now, when I think of it a bit more, I'm not sure this is possible,
>> but, "almost nothing" very well describes my SQL knowledge, so...



>> >Hi Zeljko,

>> >if you select a unique field in your query you cannot distinct duplicate
>> >values in any field, because every record of the table will be in the
>> >resulting recordset. If you want to distinct duplicate values you can
>group
>> >the records with the GROUP BY - command. But you have to include all
>fields
>> >from your select-statement, that aren't used with aggregate-functions
>(SUM,
>> >COUNT etc.). ie.

>> >SELECT F1,F2,F3,F4, COUNT(F5) FROM table GROUP BY F1,F2,F3,F4;

>> >With this query you will get the F1-F2-F3-F4 - combinations only once -
>plus
>> >the number of records with this combinations.

>> >I hope this will help you.
>> >--
>> >CU  Ronald
>> >-> Life would be easier if we had the source code.

>> Best regards,
>>        Zeljko

>> We are born {*filter*}, wet, and hungry.
>> Then things got worse.

>> Remove "die.spammer" before replying



Mon, 18 Feb 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Something like DISTINCT or DISTINCTROW, but what ?

2. SELECT DISTINCT vs DISTINCTROW with dynaset

3. difference between DISTINCT and DISTINCTROW (in a SQL query)

4. DISTINCT and DISTINCTROW in a SQL query?

5. Distinct or DistinctRow

6. Sheridans Datagrid, show something, write something else?

7. SELECT DISTINCTROW?

8. SELECT DISTINCTROW year(y) FROM x will not work properly

9. Can I use DISTINCTROW in SQL Server 7?

10. Copy read-only liked tables

11. Visual Liking in .Net using TREE VIEW control

12. DDE and the likes

 

 
Powered by phpBB® Forum Software