My 2c is that that result violates 1NF and is therefore a headache. Data in that shape is both hard to produce and hard to consume. I'd review your objectives and find a different way to get there.
What don't you like about this?
How is the other thing better?Code:ID States 1 MD, MA 2 MD 3 MS, KS
Cheers,
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) ;
Let me know if you need more columns.
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.
Anything is possible in Access with enough ingenuity and the curiosity to achieve it.
This query (assuming C1 is a number):
Code: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.
hth
Chris
Yep, we don’t need a reason, it’s just recreational programming.
Columns C1 to C13 with lateral inversion of data. Demo attached.
Might be able to chart this but for the moment, Cyprus calls.![]()
Now that is what I call assistance. If and when Lala comes back from vacation she/he should be pleased. Thanks everybody. Great turnout.
The easiest though somewhat limited way to do this is to use queries...
Do you have some UNIQUE way of identifying the records?
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.
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 and stopher's example (they're the same) is amazing!!!!!!!!!!!!
please explain how you did this, this is so simple that i'm in awe
Ahh...but you are getting wiser by the minute.i'm sorry, disregard the last post, i'm an idiot
Ahh...but you are getting wiser by the minute.![]()
No rubbing on these Forums. There is a policy against it.![]()