Freelancers Network
 
skill list top cap
Homepage
Join the Freelancer's Network
Update your details
Find a freelancer
Post a project
Find a project
Projects Archive
Post a job
Find a job
Jobs Archive
See Dan's Pages
See Andy's Pages
Link to this site
Resources
Join/Leave Forum
Forum Messages
+Additions+ Adverts
Advertising
Contact Us
Subscribe to our newsletter - enter your email address and hit return
Freelancers.net is owned and operated by Andy Stowell and Dan Winchester
skill list end cap
guru web hostcom

Find me again on Freelancers.net

RE: FN-FORUM: Access database query to add new columns from related table

date posted 1st September 2006 13:25

If I understand you correctly, I think you want to collate multiple order
item rows into a single order row.

I have created a query (below) that does this (note - it is a cut down
version of your own), but please read my comments as I've made a few
assumptions to get this query.

regards,
Dudley

Dudley Trueman
http://www.freelancers.net/dudleytrueman

--------------------

SELECT
[Order Number],
Name,
Company,
Max(Order1ProdRef) AS MaxOfOrder1ProdRef,
Max(Order1ProdDesc) AS MaxOfOrder1ProdDesc,
Max(Order1Qty) AS MaxOfOrder1Qty,
Max(Order2ProdRef) AS MaxOfOrder2ProdRef,
Max(Order2ProdDesc) AS MaxOfOrder2ProdDesc,
Max(Order2Qty) AS MaxOfOrder2Qty
FROM
(SELECT
Order.[Order Number],
Person.Name,
Person.Company,
switch(OrderDetail.OrderSequenceNumber=1, OrderDetail.ProductReference) AS
Order1ProdRef,
switch(OrderDetail.OrderSequenceNumber=1, ProductLinked.[Short
description]) AS Order1ProdDesc,
switch(OrderDetail.OrderSequenceNumber=1, OrderDetail.QuantityOrdered) AS
Order1Qty,
switch(OrderDetail.OrderSequenceNumber=2, OrderDetail.ProductReference) AS
Order2ProdRef,
switch(OrderDetail.OrderSequenceNumber=2, ProductLinked.[Short
description]) AS Order2ProdDesc,
switch(OrderDetail.OrderSequenceNumber=2, OrderDetail.QuantityOrdered) AS
Order2Qty,
Order.[Total Cost]
FROM (([Order] LEFT JOIN OrderDetail ON Order.[Order Sequence Number] =
OrderDetail.OrderSequenceNumber)
LEFT JOIN Person ON Order.InvoiceContactID = Person.ContactID)
LEFT JOIN ProductLinked ON OrderDetail.ProductReference =
ProductLinked.[Product reference]
) AS vw_OrderItems
GROUP BY[Order Number], Name, Company;


The important points to note are:
* This is a cutdown version of your original query and does not contain all
10 columns you mention in your email. You will need to add to this query for
your purposes.
* I create a column for each order item field using a SWITCH statement - so
the first order item appears in the first order item column (the other order
item columns for this row are left blank - i.e. contain NULL)
* The order items must be numbered sequentially (1,2,3...) for each order -
I used [Order Sequence Number] in my example
* The outer query uses MAX() and GROUP BY to *squash* the multiple rows into
a single row

NOTE OF CAUTION
- the order items MUST be numbered sequentially for each order number for
this to work. I used [Order Sequence Number], making the assumption that
this holds these values. If this field doesn't contain sequential values
(starting at 1) for each order then you'll need to create this sequential
numbering yourself before running the query - i.e. use a temporary table,
vba code, additional queries, etc.
- MS-Access may not be able to handle the complexity and size of a single
query when it is extended to 10 columns, so you may need to split the
processing into multiple queries and use macros/vba to run each part of the
process in turn. Unfortunately, it's a case of try it and see.

I hope this helps.

Dudley.


--- Previous message ---

From: Simon Dingley [EMAIL REMOVED]
Reply-To: [EMAIL REMOVED]
To: "FN-FORUM / [EMAIL REMOVED] [EMAIL REMOVED]
Subject: FN-FORUM: Access database query to add new columns from related
table
Date: 1 Sep 2006 08:56:24 -0000


My client has come to me with an access database from an Actinic(which
is a piece of shite) E-Commerce site. They would like me to export
customer orders into a spreadsheet for them so that there is one line
per order which is simple until I get to the point of adding order
items. My query so far is below but obviously creates a new line per
order item rather than one line per order with each order item in its
own column. Can anyone advise on how to get the order items for the
order into their own columns so that I eventually end up with columns
like OrderItem1, OrderItem2, OrderItem3 etc. It is safe to say that
there are no orders with more than 10 items.




Messages by Day
September 30th 2006
September 29th 2006
September 28th 2006
September 27th 2006
September 26th 2006
September 25th 2006
September 24th 2006
September 23rd 2006
September 22nd 2006
September 21st 2006
September 20th 2006
September 19th 2006
September 18th 2006
September 17th 2006
September 16th 2006
September 15th 2006
September 14th 2006
September 13th 2006
September 12th 2006
September 11th 2006
September 10th 2006
September 9th 2006
September 8th 2006
September 7th 2006
September 6th 2006
September 5th 2006
September 4th 2006
September 3rd 2006
September 2nd 2006
September 1st 2006


Messages by Month
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006


Messages by Year
2008
2007
2006
2005
2004
2003
2002
2001
2000