Something like DISTINCT or DISTINCTROW, but what ?
Author |
Message |
Ronal #1 / 3
|
 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 |
|
 |
Kevin P. Flemin #2 / 3
|
 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 |
|
 |
Zlj #3 / 3
|
 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 |
|
|
|