Solved

Flatten a query to get a list values for a column

Posted on 2015-01-06
1
102 Views
Last Modified: 2015-01-10
I TSQL I can do the following:
declare @t table(id int, name varchar(90),somecolumn varchar(90))
insert into @t
    select 1,'ABC','X' union all
    select 1,'ABC','Y' union all
    select 1,'ABC','Z' union all
    select 2,'MNO','R' union all
    select 2,'MNO','S'

select ID,Name,
    stuff((select ',' + CAST(t2.SomeColumn as varchar(1000))
     from @t t2 where t1.id = t2.id and t1.name = t2.name
     for xml path('')),1,1,'') SomeColumn
from @t t1
group by id,Name

Open in new window

with the following results:
ID      Name      SomeColumn
1      ABC      X,Y,Z
2      MNO      R,S

How would I do this with a VFP Select?
0
Comment
Question by:Peter H.
1 Comment
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 40534476
http://msdn.microsoft.com/en-us/library/66dhb85f(v=vs.80).aspx#fogrfconsiderationsforsqlselectstatementsanchor2

That's all aggregation functions. So you will not be able to concatenate strings within a select query. The only way to compose a string would be within a function, but I see no way of it being called three or two times respectively and returning one value each.

So you'd do that outside of the query.

If min and max values would suffice, you could do:
Create Cursor curTest (id i,name v(90),somecolumn v(90))
Insert into curTest Values (1,'ABC','X')
Insert into curTest Values (1,'ABC','Y')
Insert into curTest Values (1,'ABC','Z')
Insert into curTest Values (2,'MNO','R')
Insert into curTest Values (2,'MNO','S')

Select ID, Name, MIN(somecolumn)+'-'+MAX(somecolumn) as SomeColumn ;
from curTest ;
group by id, name

Open in new window

Bye, Olaf.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
foxpro 9 formset issue 6 678
Report to PDF and TIF 33 1,432
@ ... say c_Symbol in Visual Foxpro 9.0 SP2 in Windows 8 5 361
FoxPro .frx file - Embed an image 3 116
Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now