• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

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.
0
rakesh_rl
Asked:
rakesh_rl
  • 3
  • 3
  • 2
1 Solution
 
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
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
 
rakesh_rlAuthor Commented:
This works. Thank you.
0
 
Jinesh KamdarCommented:
Glad to be of help :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now