SQL Query Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shane8960
    New Member
    • Mar 2008
    • 8

    SQL Query Problem

    Hi All,

    I have a query:

    [code=sql]SELECT Sum(tbl_ExtrasO rder.OrderExtra sValue) AS SumOfOrderExtra sValue, Sum(tbl_FramesO rder.OrderFrame sValue) AS SumOfOrderFrame sValue, Sum(tbl_GlassOr der.OrderGlassV alue) AS SumOfOrderGlass Value, Sum(tbl_Service sOrder.OrderSer vicesValue) AS SumOfOrderServi cesValue, Sum(tbl_Windows Order.OrderWind owsValue) AS SumOfOrderWindo wsValue, tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
    FROM ((((tbl_Clients LEFT JOIN tbl_ExtrasOrder ON tbl_Clients.Cli entID = tbl_ExtrasOrder .ClientID) LEFT JOIN tbl_FramesOrder ON tbl_Clients.Cli entID = tbl_FramesOrder .ClientID) LEFT JOIN tbl_GlassOrder ON tbl_Clients.Cli entID = tbl_GlassOrder. ClientID) LEFT JOIN tbl_ServicesOrd er ON tbl_Clients.Cli entID = tbl_ServicesOrd er.ClientID) LEFT JOIN tbl_WindowsOrde r ON tbl_Clients.Cli entID = tbl_WindowsOrde r.ClientID
    GROUP BY tbl_ExtrasOrder .OrderExtrasDat e, tbl_FramesOrder .OrderFramesDat e, tbl_GlassOrder. OrderGlassDate, tbl_ServicesOrd er.OrderService sDate, tbl_WindowsOrde r.OrderWindowsD ate, tbl_Clients.Cli entID, tbl_Clients.Cli entSurname, tbl_Clients.Cli entDesigner
    HAVING (((Sum(tbl_Wind owsOrder.OrderW indowsValue)) Is Not Null) AND ((tbl_WindowsOr der.OrderWindow sDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Serv icesOrder.Order ServicesValue)) Is Not Null) AND ((tbl_ServicesO rder.OrderServi cesDate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Glas sOrder.OrderGla ssValue)) Is Not Null) AND ((tbl_GlassOrde r.OrderGlassDat e) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Fram esOrder.OrderFr amesValue)) Is Not Null) AND ((tbl_FramesOrd er.OrderFramesD ate) Between #" & dStartDate & "# And #" & dEndDate & "#)) OR (((Sum(tbl_Extr asOrder.OrderEx trasValue)) Is Not Null) AND ((tbl_ExtrasOrd er.OrderExtrasD ate) Between #" & dStartDate & "# And #" & dEndDate & "#))
    ORDER BY tbl_Clients.Cli entSurname;[/code]

    Basically its a query that links 5 order tables to a clients table using ClientID. Each order table DOES contain more than 1 record for each client. This query gets the sum on all the records for the client for each category. However there is a further twist, I need to be able to specify what dates I want the results to appear for (dStartDate & dEndDate, set when the user pressed the search button using VBA).

    However the problem I am having is that say if the client has ordered some windows on the 01/01/2008 and some glass on the 01/02/2008, when I search for orders in january it brings both the orders in and the same for february, however I only want it to bring in the orders marked with the dates which fall into the category.

    I know its a big query, if anybody has any advice on how I can fix it to do what I want or an alternative way of doing it, it would be much appreciated.

    Thanks, Shane
    Last edited by Stewart Ross; Apr 8 '08, 03:07 PM. Reason: Added code tags to SQL
  • FishVal
    Recognized Expert Specialist
    • Jun 2007
    • 2656

    #2
    Hi, Shane.

    Are you sure the query will return right results at all ???
    Table join is basically table multiplying.
    So, if, let us say, Client1 has 3 records in a first orders table and 4 records in the second one, then joining these three tables will give 12 records and each order in the first orders table will appear 4 times and from the second one 3 times. After that they will be summed.


    Kind regards,
    Fish

    Comment

    • shane8960
      New Member
      • Mar 2008
      • 8

      #3
      1) Yes the query does return the right results, if I dont put the SUM for each field it brings up multiple records as you stated. However the SUM is only the SUM of the actual records, rather than the duplicates that where shown.

      2) Ill try UNION instead of join see if that fixes it.

      3) Theres 5 order tables as there are 5 different types of item they can order (Window, Glass, Frame, Extras & Services), each type of order is completely different (ie the tables have 10-20 fields - all different for each order type so would mean having 80 or so fields in a table with about a 80% or so redundancy).

      Thanks, Shane

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Ok.

        How do you set values of dStartDate and dEndDate?

        Comment

        • shane8960
          New Member
          • Mar 2008
          • 8

          #5
          Originally posted by FishVal
          Ok.

          How do you set values of dStartDate and dEndDate?
          Basically I have a form, with controlsource set to the above (without the extra date criteria), I then two text boxes where the user enters a date, they then press a command button which takes the dates from the text boxes, assigns them to the dStartDate and dEndDate and sets the query above to the forms source and then refreshes the form.

          Also - I cant see how Union would work in this case. I need to be able to distinguish which table each of the values came from as I need to break it down into "Windows Value", "Glass Value" etc for each client.

          Comment

          • FishVal
            Recognized Expert Specialist
            • Jun 2007
            • 2656

            #6
            Originally posted by shane8960
            Basically I have a form, with controlsource set to the above (without the extra date criteria), I then two text boxes where the user enters a date, they then press a command button which takes the dates from the text boxes, assigns them to the dStartDate and dEndDate and sets the query above to the forms source and then refreshes the form.
            Are you sure you get it in proper m/d/y format?

            Also - I cant see how Union would work in this case. I need to be able to distinguish which table each of the values came from as I need to break it down into "Windows Value", "Glass Value" etc for each client.
            Not a problem. You may design UNION subqueries to return constant identifying source table.
            [code=sql]
            SELECT 'Windows Value' AS txtOrderType, .... FROM tbl_WindowsOrde r
            UNION
            SELECT 'Glass Value' AS txtOrderType, .... FROM tbl_GlassOrder;
            [/code]

            Regards,
            Fish

            P.S. I'm still confused as for what prevent JOIN to multiply your records. :)
            P.P.S. Have you tried to move date criteria from HAVING clause to WHERE clause?

            Comment

            • shane8960
              New Member
              • Mar 2008
              • 8

              #7
              Thanks Mate,

              Used UNION and it works a treat.

              Thanks again,

              Shane

              Comment

              • FishVal
                Recognized Expert Specialist
                • Jun 2007
                • 2656

                #8
                You are welcome.

                Best regards,
                Fish

                Comment

                Working...