Mission Impossible, Query similar to pivot table

B

Braka

I need query that can add skills from Table1 into Table2.
It looks impossible to me but maybe somebody has a way.
Table1
ID,Code,Skill
1,3267,Machine Operator
2,3267,Warehouse Worker
3,3268,Janitor
4,3269,Assembly
5,3269,Construction Eng.
6,3269,General Labor
7,7269,Janitor
8,3269,Forklift: Gas
9,3269,Machine Operator
10,3269,Production
11,3269,Warehouse Worker

Table2
Code,Skill1,Skill2,Skill3,Skill4,Skill5,Skill6,Skill7,Skill
8,Skill9
3267,Machine Operator,Warehouse Worker
3268,Janitor
3269,Assembly,Construction Eng.,General Labor
7269,Janitor
3269,Forklift: Gas,Machine Operator,Production,Warehouse
Worker
 
M

[MVP] S.Clark

Table 2 is not structured in any such a way that is useful to Access, thus
your goal is impossible.

What is the purpose of Table 2 in regards to the crosstab?
What is the desired layout of your crosstab?

--
HTH,

Steve Clark, Access MVP
FMS, Inc.
Professional Solutions Group
http://www.FMSInc.com
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Is your Access database too slow?
Are you ready to upgrade to SQL Server?
Contact us for optimization and/or upsizing!
http://www.FMSInc.com/consulting
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
G

Guest

Table2 is goint into Legacy system. This is a conversion
from Access (table1) going to Leagacy system.
 
G

Gary Walter

Hi Braka,
There may be a clever SQL,
but I'd just make a "quick-and-dirty"
subroutine.

9 string vars (strSkill1...strSkill9)

open a recordset to Table2

open a recordset to distinct Code's in Table1
(select Distinct
Code:
 From Table1)

In outer loop, get each code from this recordset.

Clear string vars
Open recordset to Table1 for current code
Loop through recordset assigning string vars
After inner loop done, add string vars and code
to Table2 recordset.
Close recordset to Table1

Get next distinct code and repeat outer loop

close all open recordsets

set all recordsets to Nothing

quick-and-dirty Gary Walter
 
R

Rufus T. Firefly

Season Greetings,

I need query that can add skills from Table1 into Table2.
It looks impossible to me...

Lots of stuff looks impossible until you have the
tools to do it easily.You can actually write an Access sql
solution to create table2, which can be viewed as
a simple crosstab.But the sql is messy and complicated
and do you really need that?:).
The RAC (Relational Application Companion) utility was
built, among other things, to solve many types of problems
easily and with a minimum of sql coding.You can think of
it as a higher level language than sql.RAC is native to
Sql Server 2000 so all you need is any server version
including the MSDE 2000 version.Simply pipe your Access
data to the server or setup a linked server to Access
or use an Access ADP.
You will find RAC in many ways similar to the Access
crosstab but much more powerful with many more features
and options.RAC can be used in many ways from crosstabs
to solving hierachy problems.

Here's a simple RAC solution done entirely on the server.
Table2 could be obtained in 1 RAC execution but I'll
present the solution piecemeal so you can more readily see
what's being done.

create table #MI ([ID] int primary key,Code int,Skill varchar(25))
go
insert #MI values(1,3267,'Machine Operator')
insert #MI values(2,3267,'Warehouse Worker')
insert #MI values(3,3268,'Janitor')
insert #MI values(4,3269,'Assembly')
insert #MI values(5,3269,'Construction Eng.')
insert #MI values(6,3269,'General Labor')
insert #MI values(7,7269,'Janitor')
insert #MI values(8,3269,'Forklift: Gas')
insert #MI values(9,3269,'Machine Operator')
insert #MI values(10,3269,'Production')
insert #MI values(11,3269,'Warehouse Worker')

In the first RAC run the key is to create the
column 'CodeGrp'.It's a sort of rank/counter
that binds together same values of 'Code' based
on the sort order by ID.It will be used as a
GROUP BY column in the 2nd run of RAC.The result
of the 1st RAC run is saved in table ##MI2
in the @select parameter.

Exec Rac
-- @transform is like the Access crosstab Transform.
-- It can used for many things in RAC including
-- creating multiple aggregates for a crosstab.
-- Here were not creating a crosstab just a table
-- so we use the RAC keyword '_dummy_'.
@transform='_dummy_',
-- The @rows parameter is somewhat similar to the
-- Access crosstab GROUP BY statement.
@rows='Code & Skill & [ID]',
@rowsort='[ID]',-- Sort the columns in @rows by ID.
-- If we were creating a crosstab @pvtcol would have
-- the column to pivot just like the Access crosstab.
@pvtcol='Access Reports',
@from='#MI',
@defaults1='y',
-- @rowbreak can eliminate duplicate @rows columns
-- like Access Reports.Here we want all duplicate 'Codes'.
@rowbreak='n', -- 'n' is short for 'no'.
-- @rowindicators creates the CodeGrp counter of type integer.
-- (RAC can create many different types of counters with no
-- coding required).
@rowindicators='Code{CodeGrp}',@counterdatatype='int',
@select='Select _rows_,CodeGrp
into ##MI2
from rac
order by rd'

select * from ##MI2 order by 1*[ID]

Code Skill ID CodeGrp
------ ----------------- ---- -----------
3267 Machine Operator 1 1
3267 Warehouse Worker 2 1
3268 Janitor 3 2
3269 Assembly 4 3
3269 Construction Eng. 5 3
3269 General Labor 6 3
7269 Janitor 7 4
3269 Forklift: Gas 8 5
3269 Machine Operator 9 5
3269 Production 10 5
3269 Warehouse Worker 11 5

Look at the values of 'CodeGrp' and how they
relate to 'Code'.To create table2 we can simply
group by 'Codegrp' and bring along the corresponding
'Code'.We pivot 'Skill' as a rank based on Skills
sort order.In other words the 1st pivot column is
the 1st skill for a CodeGrp/Code combination (row).
The 2nd pivot column is the 2nd skill for the row etc.

Exec Rac
@transform='(Skill) as Skill',
@rows='CodeGrp & Code',
@pvtcol='Skill',-- Pivot the 'Skill' column.
@from='##MI2',
@defaults1='y',
-- The @rank option tells RAC to create the pivot columns
-- as a ranking of the pivot column 'Skill'.@rank='some characters'
-- prefixes the character string to the pivot column.So the
-- 1st pivot column is named 'Skill1'.The 2nd pivot column 'Skill2' etc.
-- The @ranklimit options sets up a maximum of 9 pivot columns,ie.
-- 'Skill1'-'Skill9 regardless if there's any data in a particular
-- pivot column.
@rank='Skill',@ranklimit='9',
-- Here the RAC Select statement is used to create ##table2.
-- The rd column is an internal RAC counter based on the
-- sort order of the rows.
@select='Select cast(rd as tinyint) as rd,Code,_pvtcols_
into ##table2
from rac
order by rd'

Here is ##table2 exactly as you posted it (I think:)
You can do much more.For example you can easily concatenate
all the pivot column to a single delimited string.There are many
possibilities.

select * from ##table2 order by rd
(Hope the wrapping doesn't create a mess:)

rd Code Skill1 Skill2 Skill3 Skill4
Skill5 Skill6 Skill7 Skill8
Skill9
---- ---- ----------------- ----------------- ----------------- ------------
----- ----------------- ----------------- ----------------- ----------------
- -----------------
1 3267 Machine Operator Warehouse Worker
2 3268 Janitor
3 3269 Assembly Construction Eng. General Labor
4 7269 Janitor
5 3269 Forklift: Gas Machine Operator Production Warehouse
Worker

And yes,RAC does come with a point and click GUI as an
alternative to batch execution.

For more info on RAC for S2k including documentation and
an evaluation version visit:
www.rac4sql.net

We also offer a free tool, QALite, for adminstrating
and querying MSDE.

Happy Holidays from RacTeam
 
R

Rufus T. Firefly

[MVP] S.Clark said:
Table 2 is not structured in any such a way that is useful to Access, thus
your goal is impossible.

What is the purpose of Table 2 in regards to the crosstab?
What is the desired layout of your crosstab?

Hi MVP Steve,

Remember that expression that goes something like:
'Ours is not to reason why, but just *do* or...... ' :~)

Happy Holidays,
RTF
www.rac4sql.net
 
D

Duane Hookom

Check out the "Generic Function to..." from
http://www.rogersaccesslibrary.com/OtherLibraries.asp#Hookom,Duane. There is
a function and demo on how to use it. It doesn't involve any extra code
writing, just sql. You Concatenate() would be something like:
First create a totals query that selects distinct Code values.
SELECT DISTINCT Code
FROM Table1;
Save as qselCodes
Then create a query using the function:
SELECT Code, Concatenate("SELECT Skill FROM Table1 WHERE Code=" &
Code:
) As
Skills
FROM qselCodes;

This assumes Code is a numeric field. If not, you would need to add
delimiters.
 
R

Rufus T. Firefly

Hey MVP Duane,

Your making assumptions here,IMHO, that wouldn't duplicate
what the OP says is table2.
Seems he wanted a *table* of skills not a concatenated list
of them.
Also seems he wanted groups of different skills for the
same CODE.The tricky part.
I say give them what they want and with RAC if possible :~)
From 'no pain,no gain' to just 'no pain' :~)

I hope you will consider participating in one of our future group
RAC chats.

Happy Holidays,
RTF
www.rac4sql.net
 
D

Duane Hookom

Hi RTF,
I guess I was assuming that Braka (an others) would understand that the
query I suggested could be used as the source for a make-table query.

Is the schedule for your chats on your web site?
 
G

Gary Walter

I have to admit that like Mr. Firefly (aargh)
I didn't understand your response
either until this message. :cool:
 
O

Otis B. Driftwood

"Duane Hookom" wrote: in message
Hi RTF,
I guess I was assuming that Braka (an others) would understand that the
query I suggested could be used as the source for a make-table query.
Must be an Access thing:)
Is the schedule for your chats on your web site?
No, but it's a good idea.We'll see about it.In the meantime
watch for a post.

Happy Holidays
www.rac4sql.net
 
O

Otis B. Driftwood

Gary Walter" said:
I have to admit that like Mr. Firefly (aargh)
I didn't understand your response
either until this message. :cool:

Well I guess you'll be happy to get rid of R.T.Firefly:( :)

Happy Holidays,
Mr. Driftwood
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top