Query sorting

A

alex

I have a query with one to many relationship so I get this when merged
together:

vin option
123 a
123 b
456 a
789 b
789 c
876 a
876 b


I need to summarize this to a table so I can see the most popular group
of options:

a b 2 vins
a 1 vin
b c 1 vin

also note that the options cans be in any order and I could have 10-15
for a single vin sometimes. How do we tackle this problem?
 
J

Jerry Whittle

This is about as close as you'll get in Access without jumping through some
major hoops. You'll need to add another field named ID to the query as a
crosstab requires a minimum of three fields to work. You could even get away
with a second OPTION.

TRANSFORM Count(qryVINS.ID) AS CountOfID
SELECT qryVINS.VIN, Count(qryVINS.ID) AS TotalVins
FROM qryVINS
GROUP BY qryVINS.VIN
PIVOT qryVINS.OPTION;

Change all the "qryVINS" with the name of the query and the proper field
names.
 
A

alex

Thank you, but this is only part of the solution. This report will be
in a dap so I cannot keep in a table format since I would need to lock
the option name in colomn name. The option name changes all the times
and there is a few dozens of them.

you see now, with a table I get this:

Vin: options:
A B C D
123 1 1
456 1
789 1 1
876 1 1

I would need to get this:

Vin OptionGroup

123 AB
456 a
789 BC
876 AB

I dont have to worry about the order of the options anymore since the
table (part one of the solution) will do it. so i can merge them into
on field. This way, my column title in my dap wil always be fine.
From this point all I have to do is a sum query and connect my dap to
it.

So, I need to merge none blank field into a field (in a query). How
could we do this?

Help please.....
 
A

alex

Got another part of the solution but still short, I replaced the Count
fuction by First of my option in my table, now I get this:

Vin: options:
A B C D
123 A B
456 A
789 B C
876 A B

The only this in the way of doing what i need is to merge the fields
into one without using the column header. Again, the column header are
subject to change. now it`s A B C D but tomorrow it could be D F Z Y
and so one. I need kind of generic column header or a formula that
merges everyting and ignore blanks.
 
K

KARL DEWEY

TRANSFORM First([alex-1].option) AS FirstOfoption
SELECT [alex-1].vin
FROM [alex-1] INNER JOIN [QryAlex-1] ON [alex-1].vin = [QryAlex-1].vin
GROUP BY [alex-1].vin
PIVOT [alex-1].option;
 
K

KARL DEWEY

These two queries might do it for you.

The first query is named [QryAlex-1]

SELECT [alex-1].vin, Count([alex-1].option) AS CountOfoption
FROM [alex-1]
GROUP BY [alex-1].vin
HAVING (((Count([alex-1].option))>1));

TRANSFORM First([option]) & Last([option]) AS Opt
SELECT [alex-1].vin
FROM [alex-1] INNER JOIN [QryAlex-1] ON [alex-1].vin = [QryAlex-1].vin
GROUP BY [alex-1].vin
PIVOT "Options";
 
A

alex

Interesting, I try it and modify the >1 to "" because I want to get
the vins with empty ones also. it works fine..... there is only on
problem.

I only and always get the first and last option. So:

a) when there in no option = it works, my field is empty

b) when there is 1 option, = the option repeat itselft twice since it's
the first and last
c) when ther is 2 options= it works fine
d) when there is more thant 2 options = it does not work since it only
list 2 options.

so we get:
vin option
a) 123
b) 234 a a
c) 345 a b
d) 456 a b

insted of:
vin option
a) 123
b) 234 a
c) 345 a b
d) 456 a b c d e

Hell of a problem no?





KARL said:
These two queries might do it for you.

The first query is named [QryAlex-1]

SELECT [alex-1].vin, Count([alex-1].option) AS CountOfoption
FROM [alex-1]
GROUP BY [alex-1].vin
HAVING (((Count([alex-1].option))>1));

TRANSFORM First([option]) & Last([option]) AS Opt
SELECT [alex-1].vin
FROM [alex-1] INNER JOIN [QryAlex-1] ON [alex-1].vin = [QryAlex-1].vin
GROUP BY [alex-1].vin
PIVOT "Options";


alex said:
Thank you, but this is only part of the solution. This report will be
in a dap so I cannot keep in a table format since I would need to lock
the option name in colomn name. The option name changes all the times
and there is a few dozens of them.

you see now, with a table I get this:

Vin: options:
A B C D
123 1 1
456 1
789 1 1
876 1 1

I would need to get this:

Vin OptionGroup

123 AB
456 a
789 BC
876 AB

I dont have to worry about the order of the options anymore since the
table (part one of the solution) will do it. so i can merge them into
on field. This way, my column title in my dap wil always be fine.

it.

So, I need to merge none blank field into a field (in a query). How
could we do this?

Help please.....
 
K

KARL DEWEY

I used three queries, one of which is a make table.

Alex_55 ---
SELECT T.vin, T.option, (SELECT COUNT(*)
FROM [Alex-2] T1
WHERE T1.Vin = T.Vin
AND T1.Option <= T.Option) AS Rank INTO [Alex-rank]
FROM [Alex-2] AS T
ORDER BY T.vin, T.option;

Alex-rank_Crosstab --
TRANSFORM Max([Alex-rank].option) AS MaxOfoption
SELECT [Alex-rank].vin
FROM [Alex-rank]
GROUP BY [Alex-rank].vin
PIVOT [Alex-rank].Rank IN (1,2,3,4,5,6,7,8,9,10);

Alex-finish --
SELECT [Alex-rank_Crosstab].vin, [1] & [2] & [3] & [4] AS Options
FROM [Alex-rank_Crosstab];

Below is the raw data and the results.
vin option
123 a
123 b
456 a
789 b
789 c
876 a
876 b
123 c
456 a
789 e
789 d
876 a
876 b

vin Options
123 abc
456 a
789 bcde
876 ab


alex said:
Interesting, I try it and modify the >1 to "" because I want to get
the vins with empty ones also. it works fine..... there is only on
problem.

I only and always get the first and last option. So:

a) when there in no option = it works, my field is empty

b) when there is 1 option, = the option repeat itselft twice since it's
the first and last
c) when ther is 2 options= it works fine
d) when there is more thant 2 options = it does not work since it only
list 2 options.

so we get:
vin option
a) 123
b) 234 a a
c) 345 a b
d) 456 a b

insted of:
vin option
a) 123
b) 234 a
c) 345 a b
d) 456 a b c d e

Hell of a problem no?





KARL said:
These two queries might do it for you.

The first query is named [QryAlex-1]

SELECT [alex-1].vin, Count([alex-1].option) AS CountOfoption
FROM [alex-1]
GROUP BY [alex-1].vin
HAVING (((Count([alex-1].option))>1));

TRANSFORM First([option]) & Last([option]) AS Opt
SELECT [alex-1].vin
FROM [alex-1] INNER JOIN [QryAlex-1] ON [alex-1].vin = [QryAlex-1].vin
GROUP BY [alex-1].vin
PIVOT "Options";


alex said:
Thank you, but this is only part of the solution. This report will be
in a dap so I cannot keep in a table format since I would need to lock
the option name in colomn name. The option name changes all the times
and there is a few dozens of them.

you see now, with a table I get this:

Vin: options:
A B C D
123 1 1
456 1
789 1 1
876 1 1

I would need to get this:

Vin OptionGroup

123 AB
456 a
789 BC
876 AB

I dont have to worry about the order of the options anymore since the
table (part one of the solution) will do it. so i can merge them into
on field. This way, my column title in my dap wil always be fine.

From this point all I have to do is a sum query and connect my dap to
it.

So, I need to merge none blank field into a field (in a query). How
could we do this?

Help please.....


Jerry Whittle wrote:
This is about as close as you'll get in Access without jumping through some
major hoops. You'll need to add another field named ID to the query as a
crosstab requires a minimum of three fields to work. You could even get away
with a second OPTION.

TRANSFORM Count(qryVINS.ID) AS CountOfID
SELECT qryVINS.VIN, Count(qryVINS.ID) AS TotalVins
FROM qryVINS
GROUP BY qryVINS.VIN
PIVOT qryVINS.OPTION;

Change all the "qryVINS" with the name of the query and the proper field
names.
--
Jerry Whittle
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


:

I have a query with one to many relationship so I get this when merged
together:

vin option
123 a
123 b
456 a
789 b
789 c
876 a
876 b


I need to summarize this to a table so I can see the most popular group
of options:

a b 2 vins
a 1 vin
b c 1 vin

also note that the options cans be in any order and I could have 10-15
for a single vin sometimes. How do we tackle this problem?
 
A

alex

Wow! you are a genius!

I tought there was no solution with access to my problem!

Since I am not at work i try you exact model at home. I have yet to
try it at work but it works!

Since my application is only accessible through DAP (explorer) I
cannot use Make Table query but I twiked your stuff and it seems to
work.

I have a table [t]
and 3 Select Query
This is what I have now:

Alex-Rank --
SELECT t.vin, t.option, (SELECT COUNT(*)
FROM [t] T1
WHERE T1.Vin = T.Vin
AND T1.Option <= T.Option) AS Rank
FROM t
ORDER BY t.vin, t.option;

Alex-Rank_CrossTab --
TRANSFORM Max([Alex-rank].option) AS MaxOfoption
SELECT [Alex-rank].vin
FROM [Alex-rank]
GROUP BY [Alex-rank].vin
PIVOT [Alex-rank].Rank In
(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);

Alex-Finish --
SELECT [Alex-rank_Crosstab].vin, [1] & [2] & [3] & [4] & [5] AS Options
FROM [Alex-rank_Crosstab];


So I will test it full scale tomorrow and keep you posted!

Thank you so much!




KARL said:
I used three queries, one of which is a make table.

Alex_55 ---
SELECT T.vin, T.option, (SELECT COUNT(*)
FROM [Alex-2] T1
WHERE T1.Vin = T.Vin
AND T1.Option <= T.Option) AS Rank INTO [Alex-rank]
FROM [Alex-2] AS T
ORDER BY T.vin, T.option;

Alex-rank_Crosstab --
TRANSFORM Max([Alex-rank].option) AS MaxOfoption
SELECT [Alex-rank].vin
FROM [Alex-rank]
GROUP BY [Alex-rank].vin
PIVOT [Alex-rank].Rank IN (1,2,3,4,5,6,7,8,9,10);

Alex-finish --
SELECT [Alex-rank_Crosstab].vin, [1] & [2] & [3] & [4] AS Options
FROM [Alex-rank_Crosstab];

Below is the raw data and the results.
vin option
123 a
123 b
456 a
789 b
789 c
876 a
876 b
123 c
456 a
789 e
789 d
876 a
876 b

vin Options
123 abc
456 a
789 bcde
876 ab


alex said:
Interesting, I try it and modify the >1 to "" because I want to get
the vins with empty ones also. it works fine..... there is only on
problem.

I only and always get the first and last option. So:

a) when there in no option = it works, my field is empty

b) when there is 1 option, = the option repeat itselft twice since it's
the first and last
c) when ther is 2 options= it works fine
d) when there is more thant 2 options = it does not work since it only
list 2 options.

so we get:
vin option
a) 123
b) 234 a a
c) 345 a b
d) 456 a b

insted of:
vin option
a) 123
b) 234 a
c) 345 a b
d) 456 a b c d e

Hell of a problem no?





KARL said:
These two queries might do it for you.

The first query is named [QryAlex-1]

SELECT [alex-1].vin, Count([alex-1].option) AS CountOfoption
FROM [alex-1]
GROUP BY [alex-1].vin
HAVING (((Count([alex-1].option))>1));

TRANSFORM First([option]) & Last([option]) AS Opt
SELECT [alex-1].vin
FROM [alex-1] INNER JOIN [QryAlex-1] ON [alex-1].vin = [QryAlex-1].vin
GROUP BY [alex-1].vin
PIVOT "Options";


:

Thank you, but this is only part of the solution. This report will be
in a dap so I cannot keep in a table format since I would need to lock
the option name in colomn name. The option name changes all the times
and there is a few dozens of them.

you see now, with a table I get this:

Vin: options:
A B C D
123 1 1
456 1
789 1 1
876 1 1

I would need to get this:

Vin OptionGroup

123 AB
456 a
789 BC
876 AB

I dont have to worry about the order of the options anymore since the
table (part one of the solution) will do it. so i can merge them into
on field. This way, my column title in my dap wil always be fine.

From this point all I have to do is a sum query and connect my dap to
it.

So, I need to merge none blank field into a field (in a query). How
could we do this?

Help please.....


Jerry Whittle wrote:
This is about as close as you'll get in Access without jumping through some
major hoops. You'll need to add another field named ID to the query as a
crosstab requires a minimum of three fields to work. You could even get away
with a second OPTION.

TRANSFORM Count(qryVINS.ID) AS CountOfID
SELECT qryVINS.VIN, Count(qryVINS.ID) AS TotalVins
FROM qryVINS
GROUP BY qryVINS.VIN
PIVOT qryVINS.OPTION;

Change all the "qryVINS" with the name of the query and the proper field
names.
--
Jerry Whittle
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.


:

I have a query with one to many relationship so I get this when merged
together:

vin option
123 a
123 b
456 a
789 b
789 c
876 a
876 b


I need to summarize this to a table so I can see the most popular group
of options:

a b 2 vins
a 1 vin
b c 1 vin

also note that the options cans be in any order and I could have 10-15
for a single vin sometimes. How do we tackle this problem?
 
Top