select unique values (1 Viewer)

cpampas

Registered User.
Local time
Today, 10:29
Joined
Jul 23, 2012
Messages
218
Hello,
I have a table "myTable", with the folowwing data :
dataMaValuebValuecValue
31-dez-2319,7315,8652,56
31-dez-2319,7352,5615,86
31-dez-2352,5615,8619,73
31-dez-2352,5619,7315,86
31-dez-232631,7180
31-dez-23268031,71
31-dez-2331,712680
31-dez-2331,713274
31-dez-2331,715056
31-dez-2331,715650
31-dez-2331,717432
31-dez-2331,718026
31-dez-233231,7174
31-dez-23327431,71
31-dez-235031,7156
31-dez-23505631,71
31-dez-235631,7150
31-dez-23565031,71
31-dez-237431,7132
31-dez-23743231,71
31-dez-23802631,71
31-dez-238031,7126

i want a query that returns only those records that have a unique set of field values . in this case i would like to return:
dataMaValuebValuecValue
31-dez-2319,7315,8652,56
01-jan-242631,7180
02-jan-2431,713274
03-jan-2431,715056

is this done with a query or dao.recordset ?
 

Mike Krailo

Well-known member
Local time
Today, 13:29
Joined
Mar 28, 2020
Messages
1,044
So what is the primary key in your table? I'm not sure how you define unique set of field values. You are showing records 1,5,8,9 as being a unique set, but how are they unique compared to any of the other records in the table? Please clarify.
 

June7

AWF VIP
Local time
Today, 09:29
Joined
Mar 9, 2014
Messages
5,473
Why do you show jan dates in the output? Was that an error from building data in Excel?

Assuming there is a unique identifier field in table, like an autonumber named ID, consider:

Query1:
SELECT ID, dataM, aValue AS Qty FROM MyTable
UNION ALL SELECT ID, dataM, bValue FROM MyTable
UNION ALL SELECT ID, dataM, cValue FROM MYTable
ORDER BY ID;

Query2:
TRANSFORM First(Query1.Qty) AS FirstOfQty
SELECT Query1.ID, Query1.dataM
FROM Query1
GROUP BY Query1.ID, Query1.dataM
PIVOT DCount("*","Query1","ID=" & [ID] & " AND Qty<" & [Qty]) + 1;

Query3:
SELECT DISTINCT Query2.dataM, Query2.[1], Query2.[2], Query2.[3] FROM Query2;

dataM123
31-Dec-2315.8619.7352.56
31-Dec-232631.7180
31-Dec-2331.713274
31-Dec-2331.715056
 
Last edited:

cpampas

Registered User.
Local time
Today, 10:29
Joined
Jul 23, 2012
Messages
218
Mike, you are right i forgot the primary key, wich is a unique number "ID"
June, i used your queries that work great, altough a litle slow, but i can live with that
Many thanks
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 03:29
Joined
Jan 20, 2009
Messages
12,852
June, i used your queries that work great, altough a litle slow,
I strongly suspect the whole problem is due to inappropriate table structure and your source data should be stored in a table something like the output of June's first query.
 

ebs17

Well-known member
Local time
Today, 19:29
Joined
Feb 7, 2020
Messages
1,946
i want a query that returns only those records that have a unique set of field values . in this case i would like to return:
How do you produce other dates from 31-Dec-23?

The only rule that can be derived from the result shown is that the first record was selected for the first date.

Every query and every program procedure must follow rules. So you would have to formulate them sufficiently and reliably in advance.
Unique: With the four fields as a combination, each record is already unique in MyTable. So that's not enough.

If @June7's suggestion appeals to you, then your goal was different from what you originally formulated.

This also implies the question of whether you really know what you want to achieve.
 

June7

AWF VIP
Local time
Today, 09:29
Joined
Mar 9, 2014
Messages
5,473
The rule is: Output unique trios of values for each date, regardless of order in fields.

Normalization would make first query unnecessary and if table stored a sequence number for each trio group, the DCount() would not be needed and performance would be much better.

UNION ALL as shown might be faster than a just UNION version:

SELECT ID, dataM, aValue AS Qty, "a" AS Src FROM MyTable
UNION SELECT ID, dataM, bValue, "b" FROM MyTable
UNION SELECT ID, dataM, cValue, "c" FROM MYTable
ORDER BY ID;
 
Last edited:

ebs17

Well-known member
Local time
Today, 19:29
Joined
Feb 7, 2020
Messages
1,946
It would certainly make more sense to access the original existing tables instead of calculating data back and forth here.
 

Users who are viewing this thread

Top Bottom