Link to home
Start Free TrialLog in
Avatar of rakesh_rl
rakesh_rl

asked on

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.
Avatar of MacNuttin
MacNuttin
Flag of United States of America image

"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
Avatar of rakesh_rl
rakesh_rl

ASKER

I have multiple start dates for the same device (based on location) so I have to take the earliest.
Try this?

select devices,min(device_start_date) from [device_table] where device_start_ date < @input_date_by_user group by devices
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

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

ASKER CERTIFIED SOLUTION
Avatar of Jinesh Kamdar
Jinesh Kamdar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works. Thank you.
Glad to be of help :)