Solved

using Coalesce

Posted on 2010-11-30
3
312 Views
Last Modified: 2012-05-10
the problem i am having with this procedure is the item number (i.item) for each user store shows up for all userid. BAsically it takes the first userid item number and use it for all userid.
It  not suppose to be the same for each row. It works correctly with a regular join.



DECLARE @userid VARCHAR(MAX)
DECLARE @orderid int
select orderid, ( select  COALESCE(i.Item + '', '') + CASE when i.Quantity < 10 THEN '0' ELSE '' END + cast(i.Quantity AS varchar(3)) + 'A'
 from OrderDetail i
where   t.DateCreated > t.Submitted OR t.Submitted is NULL  for xml path ('') )Item
 , t.userid
  from Orders t
order by t.orderid desc


example.
orderid         item       userid
1                1234567    tony
2                1234567    james
3                1234567    jessy

what i need is.

orderid         item       userid
1                1234567    tony
2                6545654    james
3                5643345    jessy
0
Comment
Question by:sevensnake77
3 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34241169
DECLARE @userid VARCHAR(MAX)
DECLARE @orderid int
select orderid, ( select  COALESCE(i.Item + '', '') + CASE when i.Quantity < 10 THEN '0' ELSE '' END + cast(i.Quantity AS varchar(3)) + 'A'
 from OrderDetail i
where   t.orderid=i.orderid  -- missing this
and t.DateCreated > t.Submitted OR t.Submitted is NULL  for xml path ('') )Item
 , t.userid
  from Orders t
order by t.orderid desc
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34241174
you have to correlate the subquery:
DECLARE @userid VARCHAR(MAX) 
DECLARE @orderid int
select orderid, ( select  COALESCE(i.Item + '', '') + CASE when i.Quantity < 10 THEN '0' ELSE '' END + cast(i.Quantity AS varchar(3)) + 'A'
 from OrderDetail i
where  i.OrderID = t.OrderID
  AND ( t.DateCreated > t.Submitted OR t.Submitted is NULL )
  for xml path ('')    
)Item
 , t.userid 
  from Orders t
order by t.orderid desc

Open in new window

0
 
LVL 9

Author Closing Comment

by:sevensnake77
ID: 34241232
thanks again my friend
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now