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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL newbie, need help.

SQL newbie, need help.

Scheduled Pinned Locked Moved Database
databasecsshelp
9 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.
  • J Offline
    J Offline
    JON10884
    wrote on last edited by
    #1

    Hello people, I want to run a query for customers whose last order was placed before the beginning of this month. Since there is no LastOrder field, and I can only use OrderDate, how do you write a statement that would take only the latest OrderDate that is less than last month. I really only know the basic stuff, I'm guessing I might need to write an if statement or a loop. Thanks in advance.

    C J M 3 Replies Last reply
    0
    • J JON10884

      Hello people, I want to run a query for customers whose last order was placed before the beginning of this month. Since there is no LastOrder field, and I can only use OrderDate, how do you write a statement that would take only the latest OrderDate that is less than last month. I really only know the basic stuff, I'm guessing I might need to write an if statement or a loop. Thanks in advance.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      JON10884 wrote: I'm guessing I might need to write an if statement or a loop. Writing loops in SQL is very inefficient, you should only consider this as a last resort. Database systems are fine tuned to work on sets of data so by extracting the set into individual rows and operating on the individual rows is very inefficient. It would be helpful if you could post the relevant tables and columns


      Do you want to know more? WDevs.com - Member's Software Directories, Blogs, FTP, Mail and Forums

      1 Reply Last reply
      0
      • J JON10884

        Hello people, I want to run a query for customers whose last order was placed before the beginning of this month. Since there is no LastOrder field, and I can only use OrderDate, how do you write a statement that would take only the latest OrderDate that is less than last month. I really only know the basic stuff, I'm guessing I might need to write an if statement or a loop. Thanks in advance.

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        doesn't need a loop. You could use the horrible distinct clause select * from ( select distinct(customerid) from orders where orderdate < '2004-12-01 00:00' ) j left join ( select distinct(customerid) from orders where orderdate >= '2004-12-01 00:00' ) k on k.customerid=j.customerid where k.customerid is null

        1 Reply Last reply
        0
        • J JON10884

          Hello people, I want to run a query for customers whose last order was placed before the beginning of this month. Since there is no LastOrder field, and I can only use OrderDate, how do you write a statement that would take only the latest OrderDate that is less than last month. I really only know the basic stuff, I'm guessing I might need to write an if statement or a loop. Thanks in advance.

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          SELECT Customers.CustomerID
          FROM Customers
          INNER JOIN Orders
          ON Customers.CustomerID = Orders.CustomerID
          GROUP BY Customers.CustomerID
          HAVING MAX(Orders.OrderDate) < '20041201'

          Assuming you keep your orders in an Orders table, and your customers in a Customers table, of course! Stability. What an interesting concept. -- Chris Maunder

          J 1 Reply Last reply
          0
          • M Mike Dimmick

            SELECT Customers.CustomerID
            FROM Customers
            INNER JOIN Orders
            ON Customers.CustomerID = Orders.CustomerID
            GROUP BY Customers.CustomerID
            HAVING MAX(Orders.OrderDate) < '20041201'

            Assuming you keep your orders in an Orders table, and your customers in a Customers table, of course! Stability. What an interesting concept. -- Chris Maunder

            J Offline
            J Offline
            JON10884
            wrote on last edited by
            #5

            You guys are the best! thanks a lot! But one more question, I'm using Mike's algorithm, how do you select more fields to display?

            W 1 Reply Last reply
            0
            • J JON10884

              You guys are the best! thanks a lot! But one more question, I'm using Mike's algorithm, how do you select more fields to display?

              W Offline
              W Offline
              WoutL
              wrote on last edited by
              #6

              As long as you want to select more fields from the customers table, you should include the fields in both the select and group by clause. Like: SELECT Customers.CustomerID**, Customers.Name** FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.CustomerID**, Customers.Name** HAVING MAX(Orders.OrderDate) < '20041201' Wout Louwers

              J 1 Reply Last reply
              0
              • W WoutL

                As long as you want to select more fields from the customers table, you should include the fields in both the select and group by clause. Like: SELECT Customers.CustomerID**, Customers.Name** FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.CustomerID**, Customers.Name** HAVING MAX(Orders.OrderDate) < '20041201' Wout Louwers

                J Offline
                J Offline
                JON10884
                wrote on last edited by
                #7

                I did that but I want the date to display and if you group by date it shows the duplicates.

                W 1 Reply Last reply
                0
                • J JON10884

                  I did that but I want the date to display and if you group by date it shows the duplicates.

                  W Offline
                  W Offline
                  WoutL
                  wrote on last edited by
                  #8

                  I 'me not sure if this will work, but you could try: SELECT Customers.CustomerID, **MAX(Orders.OrderDate)** FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.CustomerID HAVING MAX(Orders.OrderDate) < '20041201' Wout Louwers

                  J 1 Reply Last reply
                  0
                  • W WoutL

                    I 'me not sure if this will work, but you could try: SELECT Customers.CustomerID, **MAX(Orders.OrderDate)** FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID GROUP BY Customers.CustomerID HAVING MAX(Orders.OrderDate) < '20041201' Wout Louwers

                    J Offline
                    J Offline
                    JON10884
                    wrote on last edited by
                    #9

                    You the man! Thanks a ton!

                    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