473,394 Members | 1,674 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,394 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 2584
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.