Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have written the query like this::
SQL
select
B.PaymentId
,B.PayDate
--,D.FirstName+' '+D.LastName as PayFrom
,case when (B.PayFrom) is Null
 Then
  (select G.E_FName+ ' '+G.E_LName from LakesideLabSQL.dbo.tblGuardian G join LakesideLabSQL.dbo.tblUserPayment
   H on G.GuardianId=H.GuardianId where H.PayDate>'2013-04-02' and H.PayDate<DATEADD(day,1,'2013-04-2'))
    when (B.GuardianId) IS Null

    Then
    (select I.FirstName+' '+I.LastName from  LakesideLabSQL.dbo.tblUserPayment K join  LakesideLabSQL.dbo.tblUser I
      on K.PayFrom=I.UserId  where K.PayDate>'2013-04-02' and K.PayDate<DATEADD(day,1,'2013-04-2'))
    End
,B.MonerisMsg
,B.GuardianId
,B.RegId
,B.TransId
,B.PayAmount as Amount
,B.Status as PayStatus
,Null as CardAuthorizationCode

,B.CnfCode as Event
from LakesideLabSQL.dbo.tblUserPayment B
join LakesideLabSQL.dbo.tblUser D on D.UserId=B.PayFrom
 where B.PayDate>'2013-04-02' and B.PayDate<DATEADD(day,1,'2013-04-2')


But its throwing the error for me in Case part.The error is "subquery is returned more than 1 value.
VB
This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


Can you please tell me the solution how to make this work?
Posted
Comments
ZurdoDev 2-Apr-13 14:20pm    
The error tells you what is happening. Do you understand the error? It's hard to tell you how to fix this because I don't understand, in English, what you are trying to do.
priya9826 2-Apr-13 14:29pm    
I am trying to get the name from the tbluser and tblGaurdian based on the userid and gaurdianId values from tbl userpayment.In tbluserpayment if userid value is present then gaurdianId value shows as null and if gaurdian id value is present then userid values is null.I need to get userid name from tbluser if gaurdianId value is null as vice versa

The easy solution is to change all of the SELECT statements within the CASE statement to SELECT TOP 1. That, however, may not be the correct solution. It could be that the CASE statement is the wrong thing to use for your business requirement. You should study the data and determine if retrieving only one row for each clause of the CASE statement is indeed the correct solution.
 
Share this answer
 
You can't use joins like this:
SQL
from LakesideLabSQL.dbo.tblUserPayment B JOIN LakesideLabSQL.dbo.tblUser D ON D.UserId=B.PayFrom

because UserID probably returns numeric values and PayFrom returns datetime values.

In JOIN statement you need to define fields with the same data types.
SQL
from LakesideLabSQL.dbo.tblUserPayment B JOIN LakesideLabSQL.dbo.tblUser D ON D.UserId=B.UserID


More:
http://www.w3schools.com/sql/sql_join.asp[^]
http://msdn.microsoft.com/en-us/library/aa196318%28v=sql.80%29.aspx[^]
http://msdn.microsoft.com/en-us/library/ms173815.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900