Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using Coalesce

Posted on 2010-11-30
3
Medium Priority
?
324 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:Seven price
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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:Seven price
ID: 34241232
thanks again my friend
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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 extract information from SQL Server on Database, Connection and Server properties

715 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