Code for top 1 results giving 0 results???

  • 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"

  • 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?

  • 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