Our new Indie Games subforum is now open for business in G&T. Go and check it out, you might land a code for a free game. If you're developing an indie game and want to post about it,
follow these directions. If you don't, he'll break your legs! Hahaha! Seriously though.
Our rules have been updated and given
their own forum. Go and look at them! They are nice, and there may be new ones that you didn't know about! Hooray for rules! Hooray for The System! Hooray for Conforming!
SQL help transposing data
I'm trying to figure out a method for updating a column with data which would typically generate multiple rows onto a single row seperateed by a comma.
Example:
I have a table that shows the states a restuarant operates in. The data is stored thusly
McDonalds NY
McDonalds TX
McDonalds AK
KFC KY
KFC FL
I want to pull it in as "McDonalds" "NY, TX, AK"
"KFC","KY,FL"
Any thoughts? I'm stumped
#FreeThan
#FreeScheck
#FreeSKFM
0 •
Posts
An easy way would just be select all the data you want to begin with into a temp table, then loop through the temp table (grouped by restaurant) and populate a new table with the data you need.
That's not going to be the most efficient though, so if you need something that will be used regularly I'd have to think about it more. I'm a .NET guy not a DBA so someone else may have a better idea.
Something like
Select Left(a.name, 3) (for KFC), Sum(Right(b.name, 2)) From table1 a, table1 b where Left(a.name, 3) = Left(b.name, 3) Group by left(a.name, 3)
In reality, you should just dump it into excel (if 1 time) or build more columns (if regular basis)/views which string these things out for you.
#FreeScheck
#FreeSKFM
I'm not even sure how I can transpose that shit in excel.
(im not really dealing with restaurants or states)
#FreeScheck
#FreeSKFM
Are there going to be multiple "Mcdonalds" in "TX" and if so, will there be a need for the redundant entries, or do you only want unique values?
#FreeScheck
#FreeSKFM
So, essentially... you want to turn THIS:
McDonalds NY
McDonalds TX
McDonalds AK
McDonalds TX
McDonalds TX
McDonalds AK
Into this:
"McDonalds" "NY, TX, AK"
Right?
#FreeScheck
#FreeSKFM
#FreeScheck
#FreeSKFM
Final question - does this process have to be completely done in SQL as a stored procedure that will get referenced all the time or something along those lines, or can we break out some VB or another language to make this easier?
A quick and dirty app that can access the table will do this worlds better for you.
Or excel if it is a one time thing.
-I am absolute garbage at VB
-The fields I need to transpose and concatenate are not char(2) They're strings of variable length
#FreeScheck
#FreeSKFM
1) write a SQL query to only return unique values of that field (If there are 3 "McDonalds TX" entries, it will only return one. Return it sorted alphabetically.
2) create a new array (array1) where you left/right the two values into... well... two values per row.
3) create a new array (array2) from that first array that has only the unique names of the restaurants.
4) Create a for each loop in which for each row in array1 in which column 1 matches on both array 1 and array 2, place the value of the second column into the second column of array2, along with a comma divider.
sString = sString & array1(2) & ", ";
at the end of the loop, run another quick for each loop to slice off the last two characters from each value in array 2, column 2. This will kill the stray comma and space.
Export array 2 into a table of your choosing. you are done.
[=LEFT(A1,(FIND("@,SUBSTITUTE(A1, ","@,LEN(A1)-LEN(SUBSTITUTE(A1, ",""))))))]
[=MID(A1,FIND("@,SUBSTITUTE(A1, ","@,LEN(A1)-LEN(SUBSTITUTE(A1, ","")))),LEN(A1))]
Now you have your asset name and location broken out and no duplicates it should be easier to group by asset name and append the location.
Restaurant and State are separate columns.
I basically have this data
A B
1 McDonalds Texas
2 McDonalds New York
3 McDonalds Kentucky
I'm trying to create this output
A B
1 McDonalds Texas, New York, Kentucky
Sorry for the shit explanation
#FreeScheck
#FreeSKFM
Restaurant
,State1
,State2
,State3
,State4
UPDATE #output
SET State1 = state
FROM inputtable i
, #output o
WHERE
i.restaurant = o.restaurant
UPDATE #output
SET State2 = state
FROM inputtable i
, #output o
WHERE
i.restaurant = o.restaurant
AND o.State1 <> i.state
UPDATE #output
SET State3 = state
FROM inputtable i
, #output o
WHERE
i.restaurant = o.restaurant
AND o.State1 <> i.state
AND o.State2 <> i.state
etc...
Then in my print SELECT
Restaurant
,State1 + ", " + State2 + ", " State3
I am babby when it comes to SQL. There has to be a way to code this more awesomely
#FreeScheck
#FreeSKFM
select restaurant, group_concat(distinct state separator ',') from mytable group by restaurant
But that's a MySQL-specific extension.
Edit: I looked at your post history, and you're probably using Sybase...? Stack Overflow tells me Sybase has no equivalent to group_concat (and recommends writing a stored procedure). So, sorry, guess that wasn't very helpful.
Poking around online, here's an approach that looks promising:
http://www.sybaseteam.com/sql-simple-way-build-string-column-t-92.html