| 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 100101 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 300Error:Server: Msg 207, Level 16, State 3, Line 2Invalid column name 'PRODUCT_CODE'.Server: Msg 207, Level 16, State 1, Line 2Invalid column name 'product_code'.Server: Msg 207, Level 16, State 1, Line 2Invalid column name 'product_code'.SELECT p.ID, p.PRODUCT_CODE as Chapt, p.product_code as Dues, p.product_code as Volfrom ( 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 pGROUP 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 AtributesFROM (select distinct ID from @table) t1cross 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 CHi102 100 jke,200 wcw,300 ewce |
 |
|
|
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 = IDDues = Product_typeMem = Product_Code100 = BalanceINSERT 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 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-20 : 10:58:28
|
| What is the output format you want? |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2008-02-20 : 11:19:39
|
| From the subscription table Where prod type = DuesID, product_code, product_type, balanceReturns: 101, REG_DUES, DUES, 100.00From the subscription table Where prod type = VolID, product_code, product_type, balanceReturns: 101,WaltFund, VOL, 50.00From the subscription table Where prod type = ChaptID, product_code, product_type, balanceReturns: 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 |
 |
|
|
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 valueFROM (select distinct ID from @table) t1cross apply (select product_code + ','+ product_type+ ','+ CAST(balance as varchar(4)) + ',' as [text()] from @table where ID = t1.ID for xml path(''))t2(list) |
 |
|
|
miky001@yahoo.com
Starting Member
5 Posts |
Posted - 2008-02-22 : 13:11:53
|
HiI need help with a query for the following:I have 3 tables:TABLE 1name No_of_creditsBOB 16TOM 14KEVIN 18TABLE 2name No_of_vehiclesBOB 0JAMES 2TABLE 3name No_of_cell_phonesPETER 2BOB 0KEITH 1PATRICK 1KEVIN 1JAMES 1HOW DO I GET RESULTS LIKE THIS:NAME CREDITS VEHICLES PHONESBOB 16 0 0TOM 14 NO DATA NO DATAKEVIN 18 NO DATA 1JAMES NO DATA 2 1PETER NO DATA NO DATA 2KEITH NO DATA NO DATA 1PATRICK NO DATA NO DATA 1Thanks in advance! MikyMiky001 |
 |
|
|
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] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-23 : 02:59:22
|
quote: Originally posted by miky001@yahoo.com HiI need help with a query for the following:I have 3 tables:TABLE 1name No_of_creditsBOB 16TOM 14KEVIN 18TABLE 2name No_of_vehiclesBOB 0JAMES 2TABLE 3name No_of_cell_phonesPETER 2BOB 0KEITH 1PATRICK 1KEVIN 1JAMES 1HOW DO I GET RESULTS LIKE THIS:NAME CREDITS VEHICLES PHONESBOB 16 0 0TOM 14 NO DATA NO DATAKEVIN 18 NO DATA 1JAMES NO DATA 2 1PETER NO DATA NO DATA 2KEITH NO DATA NO DATA 1PATRICK NO DATA NO DATA 1Thanks in advance! MikyMiky001
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 PHONESFROM TABLE1 t1FULL OUTER JOIN TABLE2 t2ON t2.name=t1.nameFULL OUTER JOIN TABLE3 t3ON t3.name=t1.name Make it a point to start a new post to ask a question in future |
 |
|
|
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.MikyMiky001 |
 |
|
|
|
|
|