Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 sub select returning more than 1 row

Author  Topic 

Harry C
Posting Yak Master

148 Posts

Posted - 2010-03-27 : 01:57:29
I know this is a long shot without seeing something, but I figured, what the heck. I am merging data from an old database to a new database. The structure is a little different.

So, anything with an _ prefix is from the old DB and anything from the new DB has no prefix. So, the subselect is returning more than one row. I for the life of me cannot figure out why. A registration typically has only one account and race per line. So, I can't figure out why the sub is returning more than 1 row. Any thoughts?

SELECT 
(
SELECT Registration.[Guid] FROM Registration
WHERE RaceGuid = r.Guid
AND AccountGuid = a.Guid
AND Registration.isPaid = 1
)
,r.Guid as RaceGuid
,evt.Guid as EventGuid
,ath.Guid as AthleteGuid
,c2.CodeName
,c3.CodeName
,ur.IsPaid
,ur.IsPaid
,CASE
WHEN u.USATNUMBER LIKE '%[^-+ 0-9]%' THEN 0
ELSE NULL
END
,ur.IsPaid
FROM _UserRaceFee urf
INNER JOIN _Events e ON e.RaceID = urf.RaceID
INNER JOIN _Users u ON u.UserID = urf.UserID
INNER JOIN Account a ON a.UserID = u.UserID
INNER JOIN Race r ON r.RaceID = urf.RaceID
INNER JOIN _Codes c ON c.CodeID = urf.FeeID
INNER JOIN [Event] evt ON evt.EventID = urf.FeeID AND evt.RaceGuid = r.Guid
INNER JOIN _Profiles p ON p.ProfileID = urf.ProfileID
INNER JOIN Athlete ath ON ath.ProfileID = p.ProfileID
INNER JOIN _UserRace ur ON ur.ProfileID = urf.ProfileID AND ur.UserID = urf.UserID AND urf.RaceID = ur.RaceID
INNER JOIN _Codes c2 ON c2.CodeID = ur.TShirtSize
INNER JOIN _Codes c3 ON c3.CodeID = ur.Division
WHERE urf.RaceID = 146

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-27 : 02:46:58
what are you trying to retrive inside subquery? will there be more than one record with isPaid = 1 in each RaceGuid AND AccountGuid group?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Harry C
Posting Yak Master

148 Posts

Posted - 2010-03-27 : 09:39:10
There should not be, but apparently there is. But I can't figure out what query to run to see what the offending records are. Any thoughts?

HC

quote:
Originally posted by visakh16

what are you trying to retrive inside subquery? will there be more than one record with isPaid = 1 in each RaceGuid AND AccountGuid group?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-27 : 11:06:11
yup. do like

SELECT RaceGuid, AccountGuid
FROM Registration
WHERE Registration.isPaid = 1
GROUP BY RaceGuid, AccountGuid
HAVING COUNT(*) > 1


to find out groups with duplicates

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Harry C
Posting Yak Master

148 Posts

Posted - 2010-03-27 : 17:38:11
Thanks, that helped me diagnose the issue. I appreciate it.

HC
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-28 : 02:38:06
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -