Home Forums SQL Server 2008 SQL Server Newbies Coding Counts and Subtotals by day, location and grand totals in T-SQL RE: Coding Counts and Subtotals by day, location and grand totals in T-SQL

  • Randy:

    This is great! This is about 90% of what I need.

    I have 2 questions.

    Your code generated the following:

    DateReleasedTotalCarsByDatesosb_SP_DescriptionTotal_NetTons

    12/20/2013 12 Belle Vernon, PA 1358.93

    12/20/2013 2 Claremont, NH 199.97

    12/20/2013 9 Fort Ann, NY 933.53

    12/19/2013 13 Belle Vernon, PA 1459.63

    12/18/2013 3 Claremont, NH 303.68

    12/16/2013 22 Du Bois, PA 2284.1

    MonthReleasedGrandTotalCarssosb_SP_DescriptionTotalNetTonsByMonth

    12 25 Belle Vernon, PA 2818.56

    12 5 Claremont, NH 503.65

    12 22 Du Bois, PA 2284.1

    12 9 Fort Ann, NY 933.53

    What I need is the following:

    Date Loads Location Net Tons

    12/18/2013 Detail Record 1

    12/18/2013 Detail Record 2

    Daily Totals: 12/18/2013 3 Claremont, NH303.68

    12/20/2013 Detail Record 1

    12/20/2013 Detail Record 2

    Daily Totals: 12/20/2013 2 Claremont, NH199.97

    Location Totals: 12 5 Claremont, NH503.65

    So how do I order your script results to keep the detail records and put the Daily Totals for each Location/Day as a footer for that day and same for the Location Totals?

    My 2nd question is how to I incorporate your script with my main script (i.e. how do I nest the select subqueries in the correct order/syntax within my script)?

    ------CSS Rail Activity Report - Released sql script

    Select

    SOSB_Car_Detail.SOSB_CarDetail_Key as Ticket

    , SOSB_Stockpile_Master.sosb_sp_location AS Location ------SOSB_SPLocation_Location

    , SOSB_Stockpile_Master.sosb_sp_description AS Location_Descr -----SOSB_SPLocation_Description

    , SOSB_Car_Detail.SOSB_CarDetail_CarNumber AS 'Car #'

    , SOSB_Product_Master.sosb_product_number As 'Product'

    , SOSB_Product_Master.sosb_product_description As 'ProdDesc'

    , SOSB_Car_Detail.SOSB_CarDetail_DateReleased AS 'Date Released'

    , SOSB_Car_Detail.SOSB_CarDetail_NetTons AS 'Net Tons'

    FROM SOSB_Car_Detail

    Left Outer Join SOSB_Stockpile_Master

    ON SOSB_Car_Detail.SOSB_CarDetail_CoToSP = SOSB_Stockpile_Master.SOSB_SP_Key

    LEFT OUTER JOIN SOSB_SPLocation_Master

    ON SOSB_Stockpile_Master.SOSB_SP_CoLocation = SOSB_SPLocation_Master.SOSB_SPLocation_Key

    Left Outer Join SOSB_Product_Master

    on SOSB_Stockpile_Master.sosb_sp_coproduct = SOSB_Product_Master.sosb_product_key

    and SOSB_Stockpile_Master.SOSB_SP_Company = SOSB_Product_Master.SOSB_Product_Company

    WHERE (ISNULL(SOSB_Car_Detail.SOSB_CarDetail_Void, 'No') = 'No')

    and SOSB_CarDetail_DateReleased between '12/16/13' and '12/22/13' -- added

    group by

    SOSB_SP_Description

    , SOSB_CarDetail_DateReleased

    , SOSB_Car_Detail.SOSB_CarDetail_Key

    , SOSB_Stockpile_Master.sosb_sp_location

    , SOSB_Car_Detail.SOSB_CarDetail_CarNumber

    , SOSB_Product_Master.sosb_product_number

    , SOSB_Product_Master.sosb_product_description

    , SOSB_Car_Detail.SOSB_CarDetail_NetTons

    ORDER BY SOSB_SP_Description, SOSB_CarDetail_DateReleased, SOSB_Car_Detail.SOSB_CarDetail_Key