Solved

using Coalesce

Posted on 2010-11-30
3
316 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 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