Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 union join to return multiple rows into columns.

Author  Topic 

dzabor
Posting Yak Master

138 Posts

Posted - 2008-02-19 : 21:25:37
I have a subscriptions table that has many line items for each record. Each line item has a different type, dues, vol, Chapt.

101 dues Mem 100
101 Vol charity 200
101 chapt CHi 300

I want my end result to have one line item per record id, but I keep coming up with an error. I am pretty sure I am close, but need assistance before I can proceed.

101 mem 100 charity 200 chi 300

Error:
Server: Msg 207, Level 16, State 3, Line 2
Invalid column name 'PRODUCT_CODE'.
Server: Msg 207, Level 16, State 1, Line 2
Invalid column name 'product_code'.
Server: Msg 207, Level 16, State 1, Line 2
Invalid column name 'product_code'.



SELECT p.ID,
p.PRODUCT_CODE as Chapt,
p.product_code as Dues,
p.product_code as Vol
from (
SELECT ID,
product_code as Chapt,
Null as dues,
Null as Vol
from subscriptions
where prod_type = 'chapt'
AND BALANCE > 0

union all

SELECT ID,
Null as chapt,
product_code as Dues,
Null as vol
from subscriptions
where prod_type = 'dues'
AND BALANCE > 0

union all

SELECT ID,
Null as chapt,
Null as dues,
product_code as Vol
from subscriptions
where prod_type = 'vol'
AND BALANCE > 0

) AS p
GROUP BY p.id

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-19 : 23:40:58
You can get it like this:-

declare @table table
(
ID int,
PrdtType varchar(10),
PrdtCode varchar(10),
CatNo int
)


INSERT INTO @Table values(101, 'dues' ,'Mem', 100)
INSERT INTO @Table values(101, 'Vol', 'charity', 200)
INSERT INTO @Table values(101, 'chapt', 'CHi', 300 )

INSERT INTO @Table values(102, 'dues' ,'jke', 100)
INSERT INTO @Table values(102, 'Vol', 'wcw', 200)
INSERT INTO @Table values(102, 'chapt', 'ewce', 300 )

select t1.ID,left(t2.list,len(t2.list)-1) as Atributes
FROM (select distinct ID from @table) t1
cross apply (select CAST(CatNo as varchar(4)) + ' ' + PrdtCode + ',' as [text()]
from @table
where ID = t1.ID
for xml path(''))t2(list)
output
---------------------
ID Atributes
----------- ------------------------------
101 100 Mem,200 charity,300 CHi
102 100 jke,200 wcw,300 ewce
Go to Top of Page

dzabor
Posting Yak Master

138 Posts

Posted - 2008-02-20 : 08:31:39
Here are the fields i need. I am not sure how to place it in the statement you are suggesting. Each ID can have up to three different line item distinct by the product code and type. Should I palce the field names where you have the values? if so how do I distinguish between the dues type and the vol type?

101 = ID
Dues = Product_type
Mem = Product_Code
100 = Balance

INSERT INTO @Table values(101, 'dues' ,'Mem', 100)
INSERT INTO @Table values(101, 'Vol', 'charity', 200)
INSERT INTO @Table values(101, 'chapt', 'CHi', 300 )

INSERT INTO @Table values(102, 'dues' ,'jke', 100)
INSERT INTO @Table values(102, 'Vol', 'wcw', 200)
INSERT INTO @Table values(102, 'chapt', 'ewce', 300

Thanks,
DZ
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-20 : 10:58:28
What is the output format you want?
Go to Top of Page

dzabor
Posting Yak Master

138 Posts

Posted - 2008-02-20 : 11:19:39
From the subscription table Where prod type = Dues
ID, product_code, product_type, balance

Returns: 101, REG_DUES, DUES, 100.00

From the subscription table Where prod type = Vol
ID, product_code, product_type, balance

Returns: 101,WaltFund, VOL, 50.00

From the subscription table Where prod type = Chapt
ID, product_code, product_type, balance

Returns: 101,Chapt_Chi, CHAPT, 75.00

*******************
All of the data needed is in the same table so i am assuming I will have to self join or use the temp table proposed.

Anticipated Output:
ID, product_code(Where prod type = Dues), product_type(Where prod type = Dues), balance(Where prod type = Dues), product_code(Where prod type = VOL), product_type(Where prod type = VOL), balance(Where prod type = VOL),product_code(Where prod type = CHAPT), product_type(Where prod type = CHAPT), balance(Where prod type = CHAPT)

Anticipated Output with actual values:
101, REG_DUES, DUES, 100.00, WaltFund, VOL, 50.00,Chapt_Chi, CHAPT, 75.00
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-20 : 11:33:39
Try like this:-

select CAST(t1.ID AS varchar(4)) + ',' + left(t2.list,len(t2.list)-1) as value
FROM (select distinct ID from @table) t1
cross apply (select product_code + ','+ product_type+ ','+ CAST(balance as varchar(4)) + ',' as [text()]
from @table
where ID = t1.ID
for xml path(''))t2(list)
Go to Top of Page

miky001@yahoo.com
Starting Member

5 Posts

Posted - 2008-02-22 : 13:11:53
Hi
I need help with a query for the following:

I have 3 tables:

TABLE 1
name No_of_credits
BOB 16
TOM 14
KEVIN 18

TABLE 2
name No_of_vehicles
BOB 0
JAMES 2

TABLE 3
name No_of_cell_phones
PETER 2
BOB 0
KEITH 1
PATRICK 1
KEVIN 1
JAMES 1




HOW DO I GET RESULTS LIKE THIS:

NAME CREDITS VEHICLES PHONES
BOB 16 0 0
TOM 14 NO DATA NO DATA
KEVIN 18 NO DATA 1
JAMES NO DATA 2 1
PETER NO DATA NO DATA 2
KEITH NO DATA NO DATA 1
PATRICK NO DATA NO DATA 1


Thanks in advance!

Miky

Miky001
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2008-02-22 : 20:01:36
Miky,

please start a new thread for your question


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-23 : 02:59:22
quote:
Originally posted by miky001@yahoo.com

Hi
I need help with a query for the following:

I have 3 tables:

TABLE 1
name No_of_credits
BOB 16
TOM 14
KEVIN 18

TABLE 2
name No_of_vehicles
BOB 0
JAMES 2

TABLE 3
name No_of_cell_phones
PETER 2
BOB 0
KEITH 1
PATRICK 1
KEVIN 1
JAMES 1




HOW DO I GET RESULTS LIKE THIS:

NAME CREDITS VEHICLES PHONES
BOB 16 0 0
TOM 14 NO DATA NO DATA
KEVIN 18 NO DATA 1
JAMES NO DATA 2 1
PETER NO DATA NO DATA 2
KEITH NO DATA NO DATA 1
PATRICK NO DATA NO DATA 1


Thanks in advance!

Miky

Miky001



SELECT COALESCE(t1.name,t2.name,t3.name) AS NAME,
COALESCE(CAST(No_of_credits AS varchar(4)),'NO DATA') AS CREDITS,
COALESCE(CAST(No_of_vehicles AS varchar(4)),'NO DATA') AS VEHICLES,
COALESCE(CAST(No_of_cell_phones AS varchar(4)),'NO DATA') AS PHONES
FROM TABLE1 t1
FULL OUTER JOIN TABLE2 t2
ON t2.name=t1.name
FULL OUTER JOIN TABLE3 t3
ON t3.name=t1.name


Make it a point to start a new post to ask a question in future
Go to Top of Page

miky001@yahoo.com
Starting Member

5 Posts

Posted - 2008-03-03 : 16:51:08
Thanks for the reply and code!
I apologize for ignorance on not starting a new topic.

Miky

Miky001
Go to Top of Page
   

- Advertisement -