473,382 Members | 1,238 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Get multiple column values as one item?

Hi, I'm attempting to do a query that will return all values for a specific person under one alias. e.g I have a users table with names in such as Fred, Bob, Jim. Then I have a devices table of items they all use, say Fred uses a Phone, PC, Printer and Fax and they were linked on userid would I be able to run a query that instead of returning:

Name-----Device

Fred ------ Phone
Fred ------ PC
Fred ------ Printer
Fred ------ Fax


I would get:

Name ----- Device
Fred ------ Phone, PC, Printer, Fax

Sorry if it's not clear but without being able to draw up tables it's hard to explain.
Feb 4 '08 #1
6 2583
In effect I want to return more than 1 value from a subquery
Feb 4 '08 #2
Delerna
1,134 Expert 1GB
Here is a similar Question with an answer.
Feb 4 '08 #3
Jim Doherty
897 Expert 512MB
Hi, I'm attempting to do a query that will return all values for a specific person under one alias. e.g I have a users table with names in such as Fred, Bob, Jim. Then I have a devices table of items they all use, say Fred uses a Phone, PC, Printer and Fax and they were linked on userid would I be able to run a query that instead of returning:

Name-----Device

Fred ------ Phone
Fred ------ PC
Fred ------ Printer
Fred ------ Fax


I would get:

Name ----- Device
Fred ------ Phone, PC, Printer, Fax

Sorry if it's not clear but without being able to draw up tables it's hard to explain.

Hi Spoogledrummer,

This should work for you if it fits your table names and field names if not then obviously amend

Assumptions
You have a table Called tblUsers with Fields
UserID (datatype - int identity)
UserName (datatype - text)

You have a table called tblDevice with Fields
DeviceID (datatype int identity)
UserID (datatype int)
Device (datatype text)

Requirement
To retrieve all devices from the Devices table associated with a specific UserID from the Users table and in so doing present the device names contained in the Devices table concatenated as a continous single field line separating the respective values with a comma and then a space.

Functional Resolution
Create a user defined function having the following syntax

Expand|Select|Wrap|Line Numbers
  1.  CREATE FUNCTION dbo.UDF_RetrieveDevice 
  2. ( @UserID int ) 
  3. RETURNS varchar(500) 
  4. AS 
  5. BEGIN 
  6. DECLARE @DeviceString varchar(255), @delimiter char 
  7. SET @delimiter = ',' 
  8. SELECT @DeviceString = COALESCE(@DeviceString + @delimiter, '') +SPACE(1)+ Device
  9. FROM (SELECT DISTINCT Device FROM dbo.tblDevice where UserID=@UserID) derived_devices
  10. RETURN ( SELECT LTRIM(@DeviceString) AS [Device]) 
  11. END
  12.  
Implementation method
Call the function from a view/query by typically using this SQL syntax

SELECT UserID, UserName, UDF_RetrieveDevices(UserID) as Device FROM dbo.tblUsers

Comments & considerations
Note the defined varchar lengths within the body of the function to amend to reflect your needs
The resultant SQL statement when run as a view/query outlined above should present you with what you require from your posting

Regards

Jim :)
Feb 5 '08 #4
Delerna
1,134 Expert 1GB
well thanks very much Jim
I learnt something today
Feb 5 '08 #5
Excellent, thanks very much.
Feb 5 '08 #6
Jim Doherty
897 Expert 512MB
Hi guys,

You're very welcome

Jim :)
Feb 5 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: NYprmr | last post by:
Hi All, I'm trying to get value from Listbox where Multi Select property is set to Simple, meaning multiple values can be selected. Doing regular loop like following, retuns NULL value for each...
1
by: carlor | last post by:
Hi there, I have a datagrid that is bound to a datasource. The grid has a hyperlink column and I want to generate the URL using the URL Field and URL Format String fields in the Property...
1
by: sri_san | last post by:
Hello, I have a datagrid in which the header needs to span over 2 columns. I have tried creating a tableCells and tableRow at runtime and set the columnspan property of a cell to 2. But, the...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
2
by: Ned Balzer | last post by:
Hi, Can anyone tell me what is the best approach for passing multiple keys from a listbox to a SQL update? I prefer to use stored procedures for updates. I am using asp.net 2.0 and SQL 2000,...
1
by: sheenaa | last post by:
Hello friends, I m using ASP.NET with C# 2005 and SQL SERVER 2005 for the ASP.Net Website. I m using sqldatasource to connect and retrieve the data from database and then it displays the data...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
3
by: sejal17 | last post by:
hello Can any one tell me how to read multiple worksheets from a single excel file.I have stored that excel in xml file.so i want to read that xml that has multiple worksheet.And i want to store...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.