June 13, 2014 at 2:19 pm
I am trying to get the top 1 trial date between the dates of 1/1/14 and 5/31/14 but when I run the query it doesn't give me an error yet gives me no results. Can anyone see what I'm doing wrong?
SELECT *
FROM appt_exch where appointment_sk in
(select top (1) appointment_sk FROM appointments where appoint_type_sk=
(SELECT ITEM_SK FROM GROUP_ITEMS WHERE GROUP_CODE = 'APPTTY' AND DESCRIPTION = 'TRIAL')
and begin_time between '01/01/2014' and '05/31/2014'
ORDER BY begin_time desc)
Thanks so much!
"Cakes"
June 13, 2014 at 2:30 pm
SELECT *
FROM appt_exch where appointment_sk in
(select top (1) appointment_sk FROM appointments where appoint_type_sk=
(SELECT ITEM_SK FROM GROUP_ITEMS WHERE GROUP_CODE = 'APPTTY' AND DESCRIPTION = 'TRIAL')
and begin_time between '01/01/2014' and '05/31/2014'
ORDER BY begin_time desc)
This should return results. Is it possible that appoint_type_sk is not equal to Item_Sk? Or that your subselect in the where returns no rows?
June 13, 2014 at 2:45 pm
There are a number of things that could be the issue here.
How about this subselect.
SELECT ITEM_SK FROM GROUP_ITEMS WHERE GROUP_CODE = 'APPTTY' AND DESCRIPTION = 'TRIAL'
If you run that all by itself how many rows are returned? If there are more than 1 you have an issue. You would need a way to know which row you want. I guess it wouldn't have more than 1 since you did say the query didn't raise and exception.
What about this query?
select top (1) appointment_sk FROM appointments where appoint_type_sk=
(SELECT ITEM_SK FROM GROUP_ITEMS WHERE GROUP_CODE = 'APPTTY' AND DESCRIPTION = 'TRIAL')
and begin_time between '01/01/2014' and '05/31/2014'
ORDER BY begin_time desc
Do you get a row here?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply