This will do what you want for up to two states. It puts them in columns by alpha order.
Code:
SELECT C1, C2, IIF(Temp <> C2,Temp,Null) AS C3
FROM
(SELECT Table1.C1, Min(Table1_1.C2) AS C2, Max(Table1_1.C2) AS Temp
FROM Table1 INNER JOIN Table1 AS Table1_1 ON Table1.C1 = Table1_1.C1
GROUP BY Table1.C1)
;
if i show you this spreadhseet with 220 columns you'd cry
trust me, i'd never design a database so poorly organized
but in this job i got into something new, using access, vba, excel macros and vbs to completely automate pulling data from oracle, manipulate it, export to excel, format it and save or email to whoever it goes
pretty cool, but unless i specify what i'm doing when i'm asking for help i always get raised eyebrows about my format)))))))))
I agree with LagBolt, the point is your new layout does not contain any useful “meaning”.
Take for example your item listed as “1” it has two values, “MD” and “MA” which in the example you have shown translates to:
C1: 1, C2: “MD”, C3: “MA”
But also this would be correct:
C1: 1, C2: “MA”, C3: “MD”
So really the position of the states, “which column they appear in” depends on the order they appear in your list, so either your design is wrong, or there is some more information you have not given us which will be necessary to help us formulate the correct answer for you.
i didn't realize this had to be specified, you're right
the columns hav to be created in the order the entries appear
the states is an example, i'm working with doc degrees, specialties and stuff
so they have to go in the order they appear (they have a rank)
i thought once i figure out how to do the columns that will be easy to do, no?
i say this all the time, thats why i love access so much despite all the "big shots" that diss it
everything else - i already explained why it has to be done this way
in the future i will explain it right away when asking a question
i do understand why you'd question this design, i would too
but ChrisO is right, i don't have control of the output, it has to look this wasy
so basically what i'm doing is i'm taking perfectly normalized data out of Orcale and making it into a mess with 220 columns))))))))))
TRANSFORM Max(C2) AS ColID
SELECT C1
FROM Table1
GROUP BY C1
PIVOT "C" & (DCount("[C2]","Table1","[C1]=" & [C1] & " AND [C2] <='" & [C2] & "'")+1)
Produces this:
Code:
C1 C2 C3
1 MA MD
2 MD
3 KS MS
It will produce as many columns as required. It works by populating a column number field (ColID) and then cross-tabbing. The DCount function is used to provide the sequence. The +1 bit is simpy to ensure the columns start at 2.
Unfortunately the result in each row will be in alphabetical order since there is no other order defined in your list. If there is a primary key then the order can be corrected to exactly match what you requested.
But I agree with the others. What are you planning to do with the data in this structure? If you are simply putting it in a report then consider John's post or maybe use the multiple column feature in the print options.
Edit: Never mind. Didn't see there were two more pages of replies after the first page.
Lagbolt is right. However, it can be done. The key is to recognize that you're asking the database for information that you haven't expliclty provided to it.
To whit: look at ytour output. There's a column called C3. Look at your input. No such information exists. Your human brain can figure out what you mean, but computers require a more literal explanation.
Here's the SQL for a Query based on 'Table1' with your columns, and an autoid primary key field.
Code:
SELECT Table1.C1, Table1.C2, DCount("*","Table1","[C1]=" & [C1] & " AND [autoid]<=" & [autoid]) AS Expr1
FROM Table1;
This query counts how many times the value in C1 has been used. This then can be used as the basis for a column header field in a crosstab query. And the SQL for that would be:
Code:
TRANSFORM First(Query1.C2) AS FirstOfC2
SELECT Query1.C1
FROM Query1
GROUP BY Query1.C1
PIVOT "C" & [Expr1]+1;
And I tweaked it to make the column name follow the convention in your example.
this looks similar to stopher's, no?
i'm trying it out right now
also, i didn't understand what you meant by C3 not existing (columns names and data are fake, the column names they want are long and with spaces so i used C1, C2))))))))
anyway, yes, this column doesn't exist in the table and that's what i'm trying to do - create it
as a matter of fact, i'm trying to create as many columns as there are rows for each set
so i didn't understand what you meant that it had to exist in the original data
anyhow, i'm trying this out and thank you so much for your help
Yep...much the same as Stopher's. I came up with it independently but hadn't noticed there were other replies beyond the first page, so my post was redundant.
As for the point I was making, your output required a piece of information that was not inherent in the data you were using. The first query provides that extra bit of information (the field to be used for the column heading) and then the crosstab uses the output of the first query to generate your desired output. Regardless, Stopher's solution should work for you.
Edit: Never mind. Didn't see there were two more pages of replies after the first page.
Lagbolt is right. However, it can be done. The key is to recognize that you're asking the database for information that you haven't expliclty provided to it.
To whit: look at ytour output. There's a column called C3. Look at your input. No such information exists. Your human brain can figure out what you mean, but computers require a more literal explanation.
Here's the SQL for a Query based on 'Table1' with your columns, and an autoid primary key field.
Code:
SELECT Table1.C1, Table1.C2, DCount("*","Table1","[C1]=" & [C1] & " AND [autoid]<=" & [autoid]) AS Expr1
FROM Table1;
This query counts how many times the value in C1 has been used. This then can be used as the basis for a column header field in a crosstab query. And the SQL for that would be:
Code:
TRANSFORM First(Query1.C2) AS FirstOfC2
SELECT Query1.C1
FROM Query1
GROUP BY Query1.C1
PIVOT "C" & [Expr1]+1;
And I tweaked it to make the column name follow the convention in your example.
>>or i have to create the query and use the function to see the results?<<
No, but you could use the download available in post #18 where the code builds the table tblDestination and then run a query off that table for your report.