473,467 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Transform Query Help required

6 New Member
Hi I have a database storing trip details. There's a parent table which stores the basic trip information and a child table that stores the countries to be visited information.
The query I have at the moment is:

TRANSFORM [Test Child].[Arrival Country] AS country
SELECT [Test Tickets].Locator
FROM [Test Tickets] INNER JOIN [Test Child] ON [Test Tickets].Locator = [Test Child].Locator
GROUP BY [Test Tickets].Locator
PIVOT [Test Child].[Arrival Country];

The problem is that all the countries in the child table are shown as headings and what I want is to have 10 columns Country1 to Country10 displayed as column headings with the country name in the data.

I'm sure there's a simple solution but I can't think of it. Any help will be much appreciated.
-Kevin.
Aug 30 '07 #1
11 1735
Rabbit
12,516 Recognized Expert Moderator MVP
Are you talking about something that looks like:

Expand|Select|Wrap|Line Numbers
  1. TripID Country1 Country2 Country3
  2. Trip19 Germany  France
  3. Trip84 Germany  Russia   China
  4.  
Aug 30 '07 #2
KevinM
6 New Member
Are you talking about something that looks like:

Expand|Select|Wrap|Line Numbers
  1. TripID Country1 Country2 Country3
  2. Trip19 Germany  France
  3. Trip84 Germany  Russia   China
  4.  
Yes, that's exactly what I'm trying to achieve. Any ideas?
Aug 31 '07 #3
Rabbit
12,516 Recognized Expert Moderator MVP
Well, you'll need two queries. One to assign a "rank" to each country for a trip. And then the crosstab. If it doesn't matter what order they're in, which I doubt, then you can "rank" by country name. Otherwise you'll probably be ranking on the date of the visit.
Aug 31 '07 #4
KevinM
6 New Member
Well, you'll need two queries. One to assign a "rank" to each country for a trip. And then the crosstab. If it doesn't matter what order they're in, which I doubt, then you can "rank" by country name. Otherwise you'll probably be ranking on the date of the visit.
Thanks for the response. I'm not quite sure what you mean by "rank". Do I need to insert a new field in a table or crerate a new query and use the results of that query as the source data for the crosstab query? The order is not important in this instance as I use the results to mailmerge into Word.
Sep 3 '07 #5
Rabbit
12,516 Recognized Expert Moderator MVP
Well, you basically need to assign each country in a trip a number before you can crosstab; Starting with 1 and ending with the number of countries in that trip. To assign the numbers, you need to be able to rank them on something. What you rank on can be anything as long as it's unique for each trip. If order doesn't matter then you can just use the name of the country.
Sep 3 '07 #6
KevinM
6 New Member
I've assigned a number for each country for the trips and then used the Rank field to pivot on and it almost works. The only problem is the results produce a row for each country, rather than just 1 row per trip. Any ideas on how to produce 1 row per trip.

Output now looks like this: (.....=spaces)
Trip Country 1 Country 2 Country 3
001 USA
002 FRANCE
003 USA
003...................MEXICO
003.........................................BELIZE
004 GERMANY

Query is as follows:
TRANSFORM Kev.[Country] AS country
SELECT [Test Tickets].Locator
FROM [Test Tickets] INNER JOIN Kev ON [Test Tickets].Locator = Kev.Locator
GROUP BY [Test Tickets].Locator,Kev.Country
PIVOT Kev.[Rank];
Sep 6 '07 #7
Rabbit
12,516 Recognized Expert Moderator MVP
Query1
Expand|Select|Wrap|Line Numbers
  1. SELECT x.Trip, x.Country, (SELECT Count(Country) As Expr1 FROM TableName WHERE Trip = x.Trip AND Country <= x.Country;) As Rank
  2. FROM TableName As x;
  3.  
Query2
Expand|Select|Wrap|Line Numbers
  1. TRANSFORM First(Country) As CountryName
  2. SELECT Trip
  3. FROM TableName
  4. GROUP BY Trip
  5. PIVOT "Country " & Rank;
  6.  
Sep 6 '07 #8
KevinM
6 New Member
Thanks very much for your help and patience, that's all working now. It will make a big difference to my reporting.
Thanks again.
Kevin.
Sep 7 '07 #9
Rabbit
12,516 Recognized Expert Moderator MVP
That second query should be pulling its data from the first query, not the table. But if it's working then it sounds like you figured that out.
Sep 7 '07 #10
KevinM
6 New Member
That second query should be pulling its data from the first query, not the table. But if it's working then it sounds like you figured that out.
Almost, but not quite. I can run the first query and it works fine, so I saved it.
When I refer to that query in the crosstab query I get an error saying that the database jet engine does not recognise 'x.Locator' as a valid field name or expression. I got around this by updating a new field in the child table with the Rank value. This took a bit of messing around and isn't a sensible way of working. The only references I could find for this error was to specify the parameters in the first query, but when I tried this the query was expecting some input values.
The queries used are below. The first query is saved as query5 referred to in the 2nd query.

SELECT x.Locator, x.[Arrival Country], (SELECT Count([Arrival Country]) AS Expr1
FROM [Test Child1] WHERE Locator = x.Locator AND [Arrival Country] <= x.[Arrival Country];) AS Rank
FROM [Test Child1] AS x;

TRANSFORM First(Query5.[Arrival Country]) AS CountryName
SELECT Query5.Locator
FROM Query5
GROUP BY Query5.Locator
PIVOT "Country " & Rank;

I think it's something to do with the alias in the first query but it could be something completely different for all I know. Hopefully it's easy and you know the answer.
Thanks.
Sep 11 '07 #11
Rabbit
12,516 Recognized Expert Moderator MVP
As long as your data table is called Test Child1 and there's a field in there named Locator, the query should work fine.
Sep 11 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Luther Baker | last post by:
My team is using the FO library to generate PDFs. We are also required to use https. The XSL transform page fed into javax.xml.transform.Transformer starts with <?xml version="1.0"?>...
1
by: lily82 | last post by:
can sm 1 help me transform this code to C# code?? tq so much :wink: Goto : <% Dim counter Dim page Dim pages counter= 10 pages = 20 page = 1
3
by: edgekaos | last post by:
Is method 2 valid? Method 1: wstring input = L"STRING"; wstring output = input; transform(output.begin(), output.end(), output.begin(), towupper); Method 2: wstring input = L"STRING";...
6
by: Stephen Cook | last post by:
Having worked through the problems around enabling the document function using an XmlUrlResolver I started work on building a useful class to hide the intricacies. Trying to generalise the process...
3
by: Peter Row | last post by:
Hi, I have 2 XML files and 1 XSLT file. The second XML file has the following declarative 1st line: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> ....the 1st one (the one to be...
2
by: Alex | last post by:
I don't know why this works: class C { public: C(int x, int y) : a(x), b(y) {} int a,b; }; float avg(const C* c) {
2
by: Venk | last post by:
hi all, I saw one reply to arun on the subject "Dynamic Query in Ms-Access" by one Mr Rick I found it very useful. Now to extend this solution forward I have the following situation. I...
7
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.