DBAsupport.com Forums - Powered by vBulletin
Results 1 to 10 of 10

Thread: sql query not returns zero

  1. #1
    Join Date
    Feb 2003
    Posts
    3

    Angry sql query not returns zero

    Hi...

    The query below returns the number of times that each subject has been used, but if the subject has been used zero times it does not return it.


    Can please anyone tell me how can i change it in order to return zero as well...

    select offer_details.subject, count(offer.offerdate) AS "Number Of Times"
    from offer, offer_details
    where offer.offerDate > '1/jan/00'
    AND offer.offercode = offer_details.offercode
    group by offer_details.subject;

    Thanks on advance...

  2. #2
    Join Date
    Jul 2002
    Location
    Washington DC
    Posts
    110
    Just check that date format is correct .This might be tha time stamp problem with the date.U can try like this

    trunc(offer.offerDate)=trunc(to_date('01/jan/00','dd/mon/yy').

    Hope this would help u

  3. #3
    Join Date
    Feb 2003
    Posts
    3

    Angry no the date its correct...

    the date its correct... it returns the values after that day but it does not return the zero ( e.g. when a subject is not been used it doesnot returns (subject name No of times ))
    anything 0

  4. #4
    Join Date
    Jul 2002
    Location
    Washington DC
    Posts
    110
    This offerdate's type is date or not.

    Did u try in this way

    trunc(offer.offerDate)>trunc(to_date('01/jan/00','dd/mon/yy').

  5. #5
    Join Date
    Feb 2003
    Posts
    3

    Yes it is...

    It is a date... i tried it and is doing the same thing...

    Returns the subjects that have been used after that date... It also suppose to return the subject that havent been used after that date and in the column have 0...

  6. #6
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    It sounds like you need an OUTER JOIN - do I understand correctly that there are rows in offer_details with no corresponding rows in offer? (sounds very strange to me . . .)

    select offer_details.subject, count(offer.offerdate) AS "Number Of Times"
    from offer, offer_details
    where (offer.offerDate > '1/jan/00' OR offer.offerDate is NULL)
    AND offer.offercode(+) = offer_details.offercode
    group by offer_details.subject;
    "The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman

  7. #7
    Join Date
    May 2002
    Posts
    108

    Does it work !

    I believe Dapi's query is an answer for your problem.

    Are you finished yet?

    Please continue the thread if you are still in trouble.

    Cheers
    Nandu
    Never give up !

    Nanda Kumar - Vellore

  8. #8
    Join Date
    Sep 2001
    Posts
    37
    Originally posted by DaPi
    The query below returns the number of times that each subject has been used, but if the subject has been used zero times it does not return it.
    I understand this...

    If a subject has been used zero times in an offer, there is no sense that its record appears in the offers_details table.. Of course you have the last word here because this situation depends of the logic of your app.

    But if THIS would be your actual situation, you'll have to include your details master table in the logic of your query to get the infor that you want using.

    I am assuming that the details master table name is det_master, and its PK is subject.

    This query will give you the info that you need:

    select offer_details.subject, count(offer.offerdate) AS "Number Of Times"
    from offer, offer_details
    where (offer.offerDate > '1/jan/00' OR offer.offerDate is NULL)
    AND offer.offercode(+) = offer_details.offercode
    group by offer_details.subject
    union
    select det_master.subject, 0
    from det_master
    where
    not exists
    (
    select 1
    from offer, offer_details
    where
    offer_details.subject=det_master.subject
    AND (offer.offerDate > '1/jan/00' OR offer.offerDate is NULL)
    AND offer.offercode(+) = offer_details.offercode
    );

    Is this what you need?????...
    Let us know or provide more info.. because is not clear what exactly do you want..

  9. #9
    Join Date
    Nov 2002
    Location
    Geneva Switzerland
    Posts
    3,142
    quote:
    --------------------------------------------------------------------------------
    Originally posted by DaPi
    The query below returns the number of times that each subject has been used, but if the subject has been used zero times it does not return it.

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

    'twasn't I !
    "The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman

  10. #10
    Join Date
    Sep 2001
    Posts
    37
    'twasn't I ! [/B]
    You're right Dapi.. my mistake..

    Wrong quote button i think..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width