Solved

Flatten a query to get a list values for a column

Posted on 2015-01-06
1
96 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

15 Experts available now in Live!

Get 1:1 Help Now