Joins on three tables

  • I have three tables namely,

    TBL_SRTAPPGROUPPOWER ,

    TBL_SRTTICKETMASTER and

    TBL_MARVEL_STATUS which has status id 1to25

    I wrote the following query to fetch the count of tickets with status id ( MSTAT_N_SID ) but i'm unable to get the status ids with count 0 (zero)

    How can i get it ? Please help

    select MSTAT_N_SID, count(TSTM.TICKET_N_TICKETNO) from TBL_MARVEL_STATUS TSM left join TBL_SRTTICKETMASTER TSTM on TSM.MSTAT_N_SID=TSTM.TICKET_N_STATUSID WHERE TICKET_N_APPID IN (SELECT AG_N_APPID FROM TBL_SRTAPPGROUPPOWER WHERE AG_V_APPGROUPID='1') GROUP BY MSTAT_N_SID;

    The Output:

    MSTAT_N_SID COUNT(TSTM.TICKET_N_TICKETNO)

    ---------------------- -----------------------------

    1 11

    4 1

    7 2

    9 2

    11 80

    15 2

  • Hi an welcome to the forums. It is impossible to answer your question with the limited amount of details you posted. Please take a few minutes to read the article in my signature about best practices when posting questions.

    _______________________________________________________________

    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/

  • I agree with Sean. Please see the link he has referenced. We'll be able to give you better help with more complete information.

    In the meantime, take a look at the WHERE clause in the query. It appears that you may have put WHERE conditions on the TBL_SRTTICKETMASTER table. If that's the case, it would prevent the LEFT JOIN from allowing the null conditions to be returned. Those would be the rows that you'll need to get your zero counts. Not sure if this is the issue but it's something you might want to investigate.

    Here's an article[/url] that discusses the impact of WHERE clause filters on LEFT JOINS.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply