How to frame a date check query

Please let me know the syntax for retrieving the list of devices for a given date (@input_date_by_user)
where the EARLIEST device_start_ date of the device is less than the @input_date_by_user. (from device table). Is it something to with HAVING clause, I have other where conditions too.
rakesh_rlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacNuttinCommented:
"list of devices for a given date (@input_date_by_user)
where the EARLIEST device_start_ date of the device is less than the @input_date_by_user. (from device table). "

select devices from [device_table] where device_start_ date < @input_date_by_user
0
rakesh_rlAuthor Commented:
I have multiple start dates for the same device (based on location) so I have to take the earliest.
0
MacNuttinCommented:
Try this?

select devices,min(device_start_date) from [device_table] where device_start_ date < @input_date_by_user group by devices
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

rakesh_rlAuthor Commented:
This will not work as I need this check to be done in the where clause.

In the snippet, I want something like
min(device.date_of_effect )       <= '01/01/2008'
instead of
device.date_of_effect        <= '01/01/2008'

SELECT *
FROM device,   
 SRO  
WHERE SRO.location_name 		  = 'ABC' and
	device.date_of_effect 	 <= '01/01/2008' and
	device.SRO_id        = SRO.SRO_id AND  
	 device.device_type        = 'Electronic' 

Open in new window

0
MacNuttinCommented:
What is this? FROM device,  
 SRO ---you must find the join for these tables otherwise it's union all (many combinations)

and select * is bad practice call your variables implicitly please.

It does work on simpler mock up tables I've tested it for you! Your where clauses will have no impact on "group by"

for each variable you need add it to group by list (try code below)
SELECT sro.location_name,min(dv.date_of_effect),dv.devicetype
FROM device dv left join SRO on dv.SRO_id = SRO.SRO_id
WHERE SRO.location_name = 'ABC' and
	dv.date_of_effect 	 <= '01/01/2008' and
	 dv.device_type = 'Electronic' 
GROUP BY dv.devicetype,sro.location_name

Open in new window

0
Jinesh KamdarCommented:
Try this.
SELECT device.id
FROM device, SRO
WHERE device.SRO_id = SRO.SRO_id
AND SRO.location_name = 'ABC'
AND device.device_type = 'Electronic' 
GROUP BY device.id
HAVING MIN(device.date_of_effect) <= '01/01/2008'

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rakesh_rlAuthor Commented:
This works. Thank you.
0
Jinesh KamdarCommented:
Glad to be of help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.