Question : SQL Query Question

Hey,  I got this query yesterday and it has been working fine.  But it seems like after 12:00 it has stopped working.  The code is for a muster report to determine the last time someone entered or left the building.  It seems to work perfectly, then it just stopped working.  The error I get is:

Message == Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

Is there something in the code that is wrong or should be redone?  The tables look fine to me, there is nothing weird in them.

This is in a MSDE database.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
Select C.FirstName,C.LastName
      , h.T as Time
      , coalesce((select Name from HWIndependentDevices as D
          Inner Join History as X
            on D.deviceid=X.link1
           and c.recordid=x.link3
           and H.t=x.gentime
         ),'Unknown') as Door    
  from CardHolder as C
 Inner Join (select link3,max(gentime) as t 
               from History 
              Where Gentime >= '20100715'
              group by link3
           )  as H 
    ON C.RecordID = H.Link3     
 Order by LastName,FirstName,h.t Desc

Answer : SQL Query Question

The cause of your problem is related to the following subquery:

...
(select Name from HWIndependentDevices as D
          Inner Join History as X
            on D.deviceid=X.link1
           and c.recordid=x.link3
           and H.t=x.gentime
         )...

The subquery is invoked for every row of the main query.  It works when it 0 or 1 values for EVERY row of the main query.  It fails with the error you observered when it returns more than one value for ANY row of the main query.  

So, then ... it's sensitive to the data in one of the correlated tables: probably History.  You correlate to history like this:  "and H.t=x.gentime",  which means if you get multiple link3's with the same max(gentime), the problem occurs.

The problem is a little complex, so I cannot advise further without detailed knowledge of your data.  However, I do suspect that the subquery in question probably needs to correlate to the history table based on it's primary key rather than gentime.  

I also propose you try the attached query to better isolate the data causing the issue.  Note that DoorCount > 1 identifies problem data.

 



1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
Select C.FirstName,C.LastName
       , (select count(*) from HWIndependentDevices as D
          Inner Join History as X
            on D.deviceid=X.link1
           and c.recordid=x.link3
           and H.t=x.gentime
        ) as DoorCount  
      , h.T as Time
      , coalesce((select max(Name) from HWIndependentDevices as D
          Inner Join History as X
            on D.deviceid=X.link1
           and c.recordid=x.link3
           and H.t=x.gentime
         ),'Unknown') as DoorMax
      , coalesce((select min(Name) from HWIndependentDevices as D
          Inner Join History as X
            on D.deviceid=X.link1
           and c.recordid=x.link3
           and H.t=x.gentime
         ),'Unknown') as DoorMin
 
  from CardHolder as C
 Inner Join 
(select link3,max(gentime) as t 
               from History 
              Where Gentime >= '20100715'
              group by link3
           )  as H 
    ON C.RecordID = H.Link3     
 Order by 3 Desc
Random Solutions  
 
programming4us programming4us