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
 General SQL Server Forums
 New to SQL Server Programming
 Subselect question

Author  Topic 

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-07-17 : 19:16:12
I have a subselect that should be working but doesn't. Been at it too long today. Can anyone guide me?


DECLARE @Calendar1 AS DateTime
SET @Calendar1 = '{{{ Please choose a start date. }}}'

SELECT
('0' + CONVERT (varchar (10), W.WorkerID)) AS VendorID,
(W.FirstName + ' ' + W.LastName) AS VendorName,
(W.FirstName + ' ' + W.LastName) AS Contact,
W.WorkerID AS AccountNumber,
W.Address,
W.Address2,
W.City,
W.State,
(W.Zip) AS ZipCode,

"ExpenseAccount" =
CASE
WHEN D.FleetID = 1 THEN '51001'
WHEN D.FleetID = 4 THEN '51004'
WHEN D.FleetID = 6 THEN '51006'
WHEN D.FleetID = 7 THEN '51007'
WHEN D.FleetID = 8 THEN '51008'
ELSE '51001'
END

FROM tblWorker AS W
INNER JOIN tblDrivers AS D ON D.DriverID = W.WorkerID


WHERE W.Status <> 2
AND W.WorkerID > 100
AND @Calendar1 >= (SELECT SH.BeginDate FROM tblSettlementHistory AS SH)

ORDER BY W.WorkerID



Where did I go wrong?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-07-17 : 20:17:22
There is more than 1 row in tblSettlementHistory, right? Do you want MIN(BeginDate) or some other aggregate function? Or are you wanting to join to related data instead?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dwdwone
Yak Posting Veteran

71 Posts

Posted - 2013-07-18 : 00:29:23
Yes, there are multiple rows. I don't need any aggregates, but I believe the reason I'm having problems, now that I look at it with fresher eyes, is that there are no linking columns! No wonder I couldn't get it to work.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-18 : 00:43:45
quote:
Originally posted by dwdwone

Yes, there are multiple rows. I don't need any aggregates, but I believe the reason I'm having problems, now that I look at it with fresher eyes, is that there are no linking columns! No wonder I couldn't get it to work.


What you need is somekind of join relationship with tblSettlementHistory table so that it pulls only related rows and then you can use BeginDate value to compare against @Calendar

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -