Solved

using Coalesce

Posted on 2010-11-30
3
309 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks again my friend
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

743 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

18 Experts available now in Live!

Get 1:1 Help Now