dbTalk Databases Forums  

Subquery and display help

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Subquery and display help in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
archanapatelwhite@googlemail.com
 
Posts: n/a

Default Subquery and display help - 02-20-2007 , 06:27 PM






Hi
I need to produce an excel output from SQL that shows
email - company name - producttype1, producttype2 (where the - denotes
a change in column)

basically i have a table the first line is the fields and the second
and third are the entries
email - companyname - producttype
xx (AT) xx (DOT) com - xx - Product1
xx (AT) xx (DOT) com - xx - Product2

Quote:
From my query I want to show in excel
xx (AT) xx (DOT) com - xx - Product1, Product2

ie I only want 1 row for the email, but want to show the 2 products
they have listed for them.

This is a simplistic version of what I want as there are actually
other fields in the table which I want to display in exactly the same
way, only 1 line per contact.

Can anyone help?

Thanks
Archana



Reply With Quote
  #2  
Old   
KenJ
 
Posts: n/a

Default Re: Subquery and display help - 02-21-2007 , 10:00 PM






Hi Archana,

I guess the key would be concatenating the various products into a
single line. Here's a basic concatenation function that will get all
your products into a single string, based on email and company:

CREATE FUNCTION dbo.ufn_concatProducts(
@email varchar(255)
, @company varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @productList varchar(8000)

SELECT
@productList = coalesce(@productList + ',', '') + product
FROM
tbl
WHERE
email = @email AND
company = @company

RETURN(@productList)
END
GO


Now, you just have to query your string together:

SELECT
email + ' - ' +
company + ' - ' +
dbo.ufn_concatProducts(email, company) AS ProductList
FROM
tbl
GROUP BY
email
, company


Have fun!

Ken


On Feb 20, 6:27 pm, archanapatelwh... (AT) googlemail (DOT) com wrote:
Quote:
Hi
I need to produce an excel output from SQL that shows
email - company name - producttype1, producttype2 (where the - denotes
a change in column)

basically i have a table the first line is the fields and the second
and third are the entries
email - companyname - producttype
x... (AT) xx (DOT) com - xx - Product1
x... (AT) xx (DOT) com - xx - Product2

From my query I want to show in excel

x... (AT) xx (DOT) com - xx - Product1, Product2

ie I only want 1 row for the email, but want to show the 2 products
they have listed for them.

This is a simplistic version of what I want as there are actually
other fields in the table which I want to display in exactly the same
way, only 1 line per contact.

Can anyone help?

Thanks
Archana



Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Subquery and display help - 02-22-2007 , 04:13 PM



KenJ (kenjohnson (AT) hotmail (DOT) com) writes:
Quote:
CREATE FUNCTION dbo.ufn_concatProducts(
@email varchar(255)
, @company varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @productList varchar(8000)

SELECT
@productList = coalesce(@productList + ',', '') + product
FROM
tbl
WHERE
email = @email AND
company = @company

RETURN(@productList)
END
Not that this function relies on undefined behaviour. You may get what
you execpt, but everyonce in ahile. you don't.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.