Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. How to select persons who didn't pay they'r bill for two mounth

How to select persons who didn't pay they'r bill for two mounth

Scheduled Pinned Locked Moved Database
tutorial
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mohammadkaab
    wrote on last edited by
    #1

    hi , i have two table first table name is person and second table name is payment what i'd like to do is that , i want to select those persons who haven't paid they'r bill for two month

    select person.* from (person INNER JOIN
    payment ON person.national_number = payment.national_number)
    where (payment.payment_date > 'two month ago from now')

    but this w'll fetch those persons who paid their bill for the two month ( i don't want them) . but i need those who didn't pay they'r bill for the two month i don't know if you understand it or no .

    L Richard DeemingR P 4 Replies Last reply
    0
    • M mohammadkaab

      hi , i have two table first table name is person and second table name is payment what i'd like to do is that , i want to select those persons who haven't paid they'r bill for two month

      select person.* from (person INNER JOIN
      payment ON person.national_number = payment.national_number)
      where (payment.payment_date > 'two month ago from now')

      but this w'll fetch those persons who paid their bill for the two month ( i don't want them) . but i need those who didn't pay they'r bill for the two month i don't know if you understand it or no .

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Surely

      where (payment.payment_date < 'two month ago from now')

      ?

      1 Reply Last reply
      0
      • M mohammadkaab

        hi , i have two table first table name is person and second table name is payment what i'd like to do is that , i want to select those persons who haven't paid they'r bill for two month

        select person.* from (person INNER JOIN
        payment ON person.national_number = payment.national_number)
        where (payment.payment_date > 'two month ago from now')

        but this w'll fetch those persons who paid their bill for the two month ( i don't want them) . but i need those who didn't pay they'r bill for the two month i don't know if you understand it or no .

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        So.. I would never show up in your list if I paid a dollar each month? :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        M 1 Reply Last reply
        0
        • M mohammadkaab

          hi , i have two table first table name is person and second table name is payment what i'd like to do is that , i want to select those persons who haven't paid they'r bill for two month

          select person.* from (person INNER JOIN
          payment ON person.national_number = payment.national_number)
          where (payment.payment_date > 'two month ago from now')

          but this w'll fetch those persons who paid their bill for the two month ( i don't want them) . but i need those who didn't pay they'r bill for the two month i don't know if you understand it or no .

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Assuming that one person can have multiple payments, an INNER JOIN will return a record for every payment over two months old, regardless of whether the person has paid within the last two months. What you need to do is find the person records which don't have a payment record within the last two months. To do that, you can use a Not Exists filter:

          DECLARE @Today date = GetUtcDate();
          DECLARE @CutoffDate date = DateAdd(month, -2, @Today);

          SELECT
          *
          FROM
          person
          WHERE
          Not Exists
          (
          SELECT 1
          FROM payment
          WHERE payment.national_number = person.national_number
          And payment.payment_date > @CutoffDate
          )
          ;


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          P 1 Reply Last reply
          0
          • M mohammadkaab

            hi , i have two table first table name is person and second table name is payment what i'd like to do is that , i want to select those persons who haven't paid they'r bill for two month

            select person.* from (person INNER JOIN
            payment ON person.national_number = payment.national_number)
            where (payment.payment_date > 'two month ago from now')

            but this w'll fetch those persons who paid their bill for the two month ( i don't want them) . but i need those who didn't pay they'r bill for the two month i don't know if you understand it or no .

            P Online
            P Online
            PIEBALDconsult
            wrote on last edited by
            #5

            SELECT person.*
            FROM person
            LEFT OUTER JOIN payment
            ON person.national_number = payment.national_number
            AND payment_date > 'two month ago from now'
            WHERE payment_date IS NULL

            You'll never get very far if all you do is follow instructions.

            1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              Assuming that one person can have multiple payments, an INNER JOIN will return a record for every payment over two months old, regardless of whether the person has paid within the last two months. What you need to do is find the person records which don't have a payment record within the last two months. To do that, you can use a Not Exists filter:

              DECLARE @Today date = GetUtcDate();
              DECLARE @CutoffDate date = DateAdd(month, -2, @Today);

              SELECT
              *
              FROM
              person
              WHERE
              Not Exists
              (
              SELECT 1
              FROM payment
              WHERE payment.national_number = person.national_number
              And payment.payment_date > @CutoffDate
              )
              ;


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              P Online
              P Online
              PIEBALDconsult
              wrote on last edited by
              #6

              Won't that have to look up each national_number in payment separately? It always looks like it does. X| I don't use EXIST.

              You'll never get very far if all you do is follow instructions.

              Richard DeemingR 1 Reply Last reply
              0
              • P PIEBALDconsult

                Won't that have to look up each national_number in payment separately? It always looks like it does. X| I don't use EXIST.

                You'll never get very far if all you do is follow instructions.

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                No, the SQL optimizer is pretty good at generating a sensible query plan. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                1 Reply Last reply
                0
                • L Lost User

                  So.. I would never show up in your list if I paid a dollar each month? :)

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  Eddy Vluggen wrote:

                  Bastard Programmer from Hell

                  Oh so appropriate. Should be renamed The Edge Case from Hell

                  Never underestimate the power of human stupidity RAH

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups