Table transform

  • Thread starter Thread starter Nedlog
  • Start date Start date
N

Nedlog

Hi!

I have a table in the following format:

Code Name Phase Start_Date End_Date
--------------------------------------------------------------------------
TSP1952 IT Resource Implementation 28/06/2003 30/09/2005
TSP1952 IT Resource Feasibility 12/09/2002 31/10/2002
TSP1952 IT Resource Design 1/04/2003 12/08/2003

I need to create a query that will transform the above data into one
row with the following columns:
Code, Name, Feasibility_Start_Date, Feasibility_End_Date,
Design_Start_Date, Design_End_Date, Implementation_Start_Date,
Implementation_End_Date

I hope this is clear what is needed. What is the best wato do this?

Thanks
D
 
Nedlog said:
Hi!

I have a table in the following format:

Code Name Phase Start_Date End_Date
--------------------------------------------------------------------------
TSP1952 IT Resource Implementation 28/06/2003 30/09/2005
TSP1952 IT Resource Feasibility 12/09/2002 31/10/2002
TSP1952 IT Resource Design 1/04/2003 12/08/2003

I need to create a query that will transform the above data into one
row with the following columns:
Code, Name, Feasibility_Start_Date, Feasibility_End_Date,
Design_Start_Date, Design_End_Date, Implementation_Start_Date,
Implementation_End_Date
Hi D,

quick and dirty
(replace "yurtable" w/ real table name)

SELECT
Code,
[Name],
(SELECT q.Start_Date FROM yurtable As q
WHERE q.Phase='Feasability') As Feasibility_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE q.Phase='Feasability') As Feasibility_End_Date,
(SELECT q.Start_Date FROM yurtable As q
WHERE q.Phase='Design') As Design_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE q.Phase='Design') As Design_End_Date,
(SELECT q.Start_Date FROM yurtable As q
WHERE q.Phase= 'Implementation') As Implementation_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE q.Phase='Implementation') As Implementation_End_Date
FROM yurtable As t
GROUP BY
Code,
[Name];
 
Hi

When I try this access returns "At most one record can be returned by
this subquery"
This is because the table I am building the query on has many projects
in each phase (Implementation, Feasibility, Design etc)
How can I write the subquery so it joins on code from the main query?
This should work as each project code has only one record for each
phase.

Thanks,
Damien
 
Hi

When I try this access returns "At most one record can be returned by
this subquery"
This is because the table I am building the query on has many projects
in each phase (Implementation, Feasibility, Design etc)
How can I write the subquery so it joins on code from the main query?
This should work as each project code has only one record for each
phase.

Thanks,
Damien

Damian, you can see your database and you know your table structures.

We do not.

Could you please explain the subquery, what you're trying to join, and
what you want to accomplish?

John W. Vinson[MVP]
 
Nedlog said:
When I try this access returns "At most one record can be returned by
this subquery"
This is because the table I am building the query on has many projects
in each phase (Implementation, Feasibility, Design etc)
How can I write the subquery so it joins on code from the main query?
This should work as each project code has only one record for each
phase.
Sorry Damien, should have realised...

SELECT
Code,
[Name],
(SELECT q.Start_Date FROM yurtable As q
WHERE
q.Phase='Feasability'
AND
q.Code = t.Code) As Feasibility_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE
q.Phase='Feasability'
AND
q.Code = t.Code) As Feasibility_End_Date,
(SELECT q.Start_Date FROM yurtable As q
WHERE
q.Phase='Design'
AND
q.Code = t.Code) As Design_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE
q.Phase='Design'
AND
q.Code = t.Code) As Design_End_Date,
(SELECT q.Start_Date FROM yurtable As q
WHERE q.Phase= 'Implementation'
AND
q.Code = t.Code) As Implementation_Start_Date,
(SELECT q.End_Date FROM yurtable As q
WHERE q.Phase='Implementation'
AND
q.Code = t.Code) As Implementation_End_Date
FROM yurtable As t
GROUP BY
Code,
[Name];

=========================
A less "quick-and-dirty" solution:

Create a query (say "qryDistinctProject")
that gives you each "distinct project."

SELECT DISTINCT
Code,
[Name]
FROM
yurtable;

Start a new query.

Add "qryDistinctProject"
Add original table 3 times.

====
aliases
====

Right-mouse click on each
of the 3 table copies,
choose "Properties"
change Alias to

F
for the "feasibility" table

D
for the "design" table

I
for the "implementation" table

Right-mouse click on the query,
choose "Properties"
change Alias to

Q

====
criteria
====

Drop-and-drag F.Phase
down to a Field row in grid.
In Criteria row for this column, type in

Feasibility

Drop-and-drag D.Phase
down to a Field row in grid.
In Criteria row for this column, type in

Design

Drop-and-drag I.Phase
down to a Field row in grid.
In Criteria row for this column, type in

Implementation

None of these 3 columns need to be shown
in final query output, they just determine we
are going to get

"feasibility dates" from "F"
"design dates" from "D"
"implementation dates" from "I"

so uncheck "Show" checkbox
for these 3 columns in grid.


====
joins
====

Drag-and-drop Q.Code over
on F.Code which will produce
a join line between Q and F.

You say that

"each project code has only one record for each
phase"

so maybe this "INNER JOIN" is all you
need to connect Q with F.

But, just to be sure, I might

right-mouse click on the join line,
choose properties,
and choose option to include
"all of Q and only matching F"
which will "LEFT JOIN" Q to F.

You might also need to complete
further join lines between Q and F
(you know your data)

Repeat above for joining

Q to D

and

Q to I

=====
output fields
=====

Drag-and-drop fields from Q
down to a field row in there own
columns in the grid.

Drag-and-drop date fields from F
down to a field row in there own
columns in the grid.

The F.Start_Date column might look like

Field: Start_Date
Table: F
Sort:
Show: <checked>
Criteria:
Or:

give it an alias by entering text
at the start of the Field row,
then a colon, for example:

Field: Feasibility_Start_Date: Start_Date
Table: F
Sort:
Show: <checked>
Criteria:
Or:


Do similarly for

F.End_Date
D.Start_Date
D.End_Date
I.Start_Date
I.End_Date
 
Back
Top