Totals in Query

Hi Everybody,
Question: Is it possible to have total of a specific column when I create a query?
In my example I created a query for all the costs related to a project (tables OPOR and POR1). I have the column of the price of every lineitem (Price from table POR1). Query works but I would need to have the total of the prices, possibly in the field called QI Direct Access Mtx1 (the one in the grid of the results). Is it possible?
Thanks in advance

You can make totals in sql by using the group by close, but it may be inconvenient with detailed lists. You can also use something like this :
SELECT top 100
T0.ItemCode, T0.OnHand, T0.IsCommited, T0.AvgPrice, T0.Locked
FROM OITW T0
Union all
Select null,
(select  sum(w.onhand) from
SELECT top 100
T0.ItemCode, T0.OnHand, T0.IsCommited, T0.AvgPrice, T0.Locked
FROM OITW T0
) w)
,null,null,null

Similar Messages

  • TOTAL IN QUERY

    Hi,
    I want to display total in query for each row like unit price + landed cost.Here unit price is always there but landed cost is applicable some times .So in some records it is blank.so in those case the total also appears as blank.
    Pls let me know how to handle this.
    Rgds,
    Rajeev

    Hi Rajeev,
    When you try to add a NULL value to another number the result is NULL, thus you should substitute NULL with 0 for e.g. ISNULL(Table.Field, 0) and then add this to the item cost, now you should get a valid value always.
    Hope this helps,
    Regards,
    Murtaza

  • Compute totals in query

    Hi,
    I can not compute the t.quantity and t.linetotals in this query.
    SELECT T.ItemCode,  T.dscription, SUM(T.quantity) as HVH, sum(T.linetotal) as Omzet
    FROM
    (select
    itemcode, dscription, quantity, linetotal
    from
    inv1 t0 inner join oinv t1 on t0.docentry = t1.docentry
    where
    t1.cardcode between '300000' and '310000' and
    t1.docdate >= '20101101' and t1.docdate <= '20101130'
    and t0.itemcode in (select itemcode from oitm t2 where t2.cardcode = '600319')
    UNION ALL
    select
    itemcode, dscription, -quantity as quantity, -linetotal as linetotal
    from
    rin1 t0 inner join orin t1 on t0.docentry = t1.docentry
    where
    t1.cardcode between '300000' and '310000' and
    t1.docdate >= '20101101' and t1.docdate <= '20101130'
    and t0.itemcode in (select itemcode from oitm t2 where t2.cardcode = '600319')) T
    group by T.itemcode, T.dscription
    order by T.itemcode
    Is there somebody who can help??
    thx
    mark

    Hi Mark,
    did not Gordon answer your issue allready in this thread ? See totals in query on 1 line
    Thats the same query, isn't it?
    Regards Steffen

  • Needed a running total in query

    Dear Gurus...Can I've a running total in query like this:
    Item_ Open_Qty_ Rec_Qty_ Iss_Qty_ Bal_
    A 10 5 2 13
    A 0 4 5 12
    A 0 0 6 6
    In this query column Bal is required whose value should be calculated as:
    Bal = ((Open_Qty + Rec_Qty) - Iss_Qty)
    Thanx in advance.

    389 posts and still you seem unaware of:
    - how important it is to mention your database version.
    - the tag, which implicates that you haven't read the FAQ for a while.
    - the 'search' option you have on the right side of the screen.
    You can query running totals easily by using analytic function SUM.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Sum Total in Query Print Layout

    Hi, all
    I have a report which is created by a query, How can I insert a variable to show the Total Sum in footer.
    Thanks
    kimmy

    Hai!
    see this thread.
    Sub totals in PLD
    regards,
    Thanga Raj.K

  • Running Total Variation Query Time Optimization

    Hi all, 
    I've been struggling with this query for a while. I need to set a customer specific running total for 10 million rows (reset for every customer). But every time the number goes negative, I need to set it as zero.
    For example,
    member no              amount            wallet
    member1                 400                      400
    member1                 -500                     0
    member1                  200                    200
    member2                  700                    700
    member2                 -200                    500
    Query:
    DECLARE @member float
    DECLARE @prev_member float
    DECLARE @amount float
    DECLARE @wallet float
    DECLARE db_cursor CURSOR FOR  
    SELECT [Member no], [Transaction Amount] 
    FROM [wallet_master_3]
    ORDER BY [Member No], [rownum]
    FOR UPDATE
    OPEN db_cursor
    FETCH NEXT FROM db_cursor INTO @member, @amount
    SET @prev_member = @member
    set @wallet=0
    WHILE @@FETCH_STATUS = 0
    BEGIN   
           IF @prev_member <> @member set @wallet=0
           SET @wallet = @wallet + @amount
           IF @wallet < 0 SET @wallet = 0
           UPDATE [wallet_master_3] SET walletsize = @wallet
           WHERE CURRENT OF db_cursor
           set @prev_member=@member
           FETCH NEXT FROM db_cursor INTO @member, @amount
    END   
    CLOSE db_cursor   
    DEALLOCATE db_cursor
    I've tried using a cursor. In five minutes, it ran 17,000 rows but after running it for 15 hours, the code only manages to set the running total for 175,000 rows. I'm not exactly sure why. Is there a faster approach I can use? 
    Thanks!

    As an exercise a 'Quirky Update' may help you in this scenario. Try the below trick!
    DECLARE @Wallet AS TABLE
    MemberNo VARCHAR(10),
    RowNum INT,
    Amount INT,
    Wallet INT
    INSERT INTO @Wallet (MemberNo, RowNum, Amount) VALUES
    ('member1',1, 400),
    ('member1',2, -500),
    ('member1',3, 200),
    ('member2',1, 700),
    ('member2',2, -200)
    DECLARE @RunTotal AS INT
    UPDATE W1
    SET
    @RunTotal = W1.Wallet =
    CASE
    WHEN W1.RowNum = 1 THEN W1.Amount
    WHEN @RunTotal + COALESCE(W1.Amount, W2.Amount) < 0 THEN 0
    ELSE @RunTotal + COALESCE(W1.Amount, W2.Amount)
    END
    FROM @Wallet W1
    LEFT OUTER JOIN @Wallet W2
    ON W1.MemberNo = W2.MemberNo AND W2.RowNum = W1.RowNum - 1;
    SELECT * FROM @Wallet;
    RESULT
    MemberNo RowNum Amount Wallet
    member1 1 400 400
    member1 2 -500 0
    member1 3 200 200
    member2 1 700 700
    member2 2 -200 500
    You can read more on 'Quirky Update' in below articles
    Solving the Running Total and Ordinal Rank Problems - Jeff Moden
    Robyn Page's SQL Server Cursor Workbench
    NOTE: Please test it thoroughly before using in a production environment!
    Krishnakumar S

  • How to get grand total in query designer

    Hi,
       Antbody know how to get the grand total after the  last row in query designer? I
    Thanks

    Hi,
    If we select the very first Characterstic in the rows section and set the Supress result rows as "never".
    That will show the overall result in the end of the report,
    In the query properties tab ,check for display properties of the result.That should be set to bottom.
    Hope this will work.
    Thanks
    Mukesh

  • Daily Sales Total Comparison Query

    Hi Experts,
    I'm trying to make a query to get daily sales total for week and wish to make a graph.
    However, if there is no figures in credit note or Down payment invoice or invoice then query seems not showing any figures for particular date.
    I would be appreciated if anyone help this.
    SELECT DISTINCT
    GetDate(),
    SUM (DISTINCT T0.DocTotal) AS 'Daily INV Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE DateDiff(D,T0.DocDate,GetDate())=0 AND DateDiff(D,T1.DocDate,GetDate())=0 AND DateDiff(D,T2.DocDate,GetDate())=0
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 1, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 2, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 3, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 4, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 5, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 6, 0))
    UNION ALL
    SELECT
    DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)),
    SUM (DISTINCT T0.DocTotal) AS 'Daily Sales Sum',
    SUM (DISTINCT T2.DocTotal) AS 'Daily DT INV Sum',
    SUM (DISTINCT T1.DocTotal*-1) AS 'Daily CR Sum',
    SUM (DISTINCT T0.DocTotal) + SUM (DISTINCT T2.DocTotal) - SUM (DISTINCT T1.DocTotal) AS 'Daily Sales Total'
    FROM OINV T0, ORIN T1, ODPI T2
    WHERE T0.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)) AND T1.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0)) AND T2.DocDate = DATEADD(dd, 0, DATEADD(dd, DATEDIFF(dd, 0, GetDate()) - 7, 0))

    Could you let me know how to make pivot query?
                        AR INV TOTAL  |  AR Down Payment Total  | AR Credit Total  | (AR INV TOTAL+AR DP TOTAL-AR CREDIT TOTAL)
    Today's Sales
    Yesterday
    Until Week Before

  • How to make Columnwise total using Query in the picklist window

    Hi Experts,
    I want to make the column wise total for the values in the field(Picked) in the Pick list window through query.
    How to do it?
    Regards,
    Magesh.

    Hi Suda,
    I have the following values in the Picked fields:
                                  Picked
                                     10
                                     20
                                     30
                                     60
    i want to get the column total 60.. If iam clicking the Pickall button the values will get posted in the Picked field.. i want the query to total the value of picked field..
    How to do it?
    Regards,
    Magesh.

  • How to make a Moving total in Query Designer

    Hi,
    I make trend analysis over sales.
    Besides the actual sales figures, I would like to show a Moving Annual Total (MAT).
    I can make this based on a specific period (using an offset of 0 to -11, and so on), but now I want to see the trend over an interval (for instance 12 month).
    I am not able to incorporate the interval values into the period variable.
    Can anybody help me with this?
    Regards,
    Erik Henning

    Tanks again.
    I am able to make a time (Fiscal year period) variable of the type interval to set the limits to my query.
    I also was able to make a time (Fiscal year period) variable to set my offset to 0 to -11.
    The problem is combining those two.
    When I combine them, I have to enter the limit values and a value for the offset variable.
    When I do that, I get the offset value of the period I entered in all the periods within my limits and that is not what I want. I want this offset period variable to use all the periods of my interval.
    Can you tell me how to proceed?
    Erik

  • Counting Record Totals Within Query: Totals Output to New Table

    Hi All,
    I'm failry new to PL/SQL... my apologies if this is obvious... I'm trying to count the total number of BI_WRKFLW.BI_LMN_SRV_AREA occurences by each BI_WRKFLW.BI_LMN_SRV_AREA (The Code correspondes to a city name like Sundance, Sheridan, Etc.)
    So, I have a Query that executes fine:
    Query
    /* Begin Querey */
                    SELECT DISTINCT
                        "BI_SO_DET_VIEW_1"."BI_SO_STAT_CD",
                        "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED",
                        "BI_TYPE_SERVICE"."BI_ACCT",
                        "BI_SO_DET_VIEW_1"."BI_SO_NBR",
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_DESC",
                        "BI_SO_TYPE_REF"."BI_SO_TYPE_DESC",
                        "BI_SRV_STAT_REF"."BI_SRV_STAT_DESC",
                        "BI_DIST_OFC_REF"."BI_DIST_NAME",
                        "BI_CNTY_REF"."BI_COUNTY_DESC",
                        "BI_SO_DET_VIEW_1"."BI_CLOSE_DT",
                        "BI_WRKFLW_TASKS"."BI_EVENT_DT_TM",
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD",
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_AREA",
                        "BI_SO_MASTER"."BI_WO_WORKORD"
                    FROM
                        ((((((((("XXXXXX"."BI_SO_MASTER" "BI_SO_MASTER"
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SO_DET_VIEW_1" "BI_SO_DET_VIEW_1"
                    ON
                        "BI_SO_MASTER"."BI_SO_NBR"="BI_SO_DET_VIEW_1"."BI_SO_NBR")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_TYPE_SERVICE" "BI_TYPE_SERVICE"
                    ON
                        "BI_SO_DET_VIEW_1"."ACCT_NBR"="BI_TYPE_SERVICE"."BI_ACCT")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_WRKFLW" "BI_WRKFLW"
                    ON
                        "BI_SO_DET_VIEW_1"."BI_WRKFLW_KEY"="BI_WRKFLW"."BI_WRKFLW_KEY")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SO_TYPE_REF" "BI_SO_TYPE_REF"
                    ON
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"="BI_SO_TYPE_REF"."BI_SO_TYPE_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_WRKFLW_TASKS" "BI_WRKFLW_TASKS"
                    ON
                            "BI_WRKFLW"."BI_WRKFLW_KEY"="BI_WRKFLW_TASKS"."BI_WRKFLW_KEY")
                    AND (
                            "BI_WRKFLW"."BI_WORK_EVENT_CD"="BI_WRKFLW_TASKS"."BI_WORK_EVENT_CD"))
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_DIST_OFC_REF" "BI_DIST_OFC_REF"
                    ON
                        "BI_WRKFLW"."BI_DIST_OFC_CD"="BI_DIST_OFC_REF"."BI_DIST_OFC_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_LMN_SRV_REF" "BI_LMN_SRV_REF"
                    ON
                        "BI_WRKFLW"."BI_LMN_SRV_AREA"="BI_LMN_SRV_REF"."BI_LMN_SRV_AREA")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SRV_LOC" "BI_SRV_LOC"
                    ON
                        "BI_TYPE_SERVICE"."BI_SRV_LOC_NBR"="BI_SRV_LOC"."BI_SRV_LOC_NBR")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SRV_STAT_REF" "BI_SRV_STAT_REF"
                    ON
                        "BI_TYPE_SERVICE"."BI_SRV_STAT_CD"="BI_SRV_STAT_REF"."BI_SRV_STAT_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_CNTY_REF" "BI_CNTY_REF"
                    ON
                        "BI_SRV_LOC"."BI_CNTY_CD"="BI_CNTY_REF"."BI_CNTY_CD"
                    WHERE
                            "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='010'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='010NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='011'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='011NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='020'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='020NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='030'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='040'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='041'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='050'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='050A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='051'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='051A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='080'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='085'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='085NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='108'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='140'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='141'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='400'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='401'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='450'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='450A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='451'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='451A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='452'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='452A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='453'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='453A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='460'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='RHR'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='RHRNS')
                    AND "BI_SO_DET_VIEW_1"."BI_SO_STAT_CD"='O'
                    AND (
                            "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='ANNEXATION'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='AVIAN'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CAPVERIF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CBMRETIRE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CBMSERVCHG'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CONTSERVD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CUSTBLD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CUTOFF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='DVCM'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='ENERGIZE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='EQUIPCHANG'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='EQUIPVALID'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='HIGHLOAD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='LOCATE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='MAINT-EQ'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='MSYSTEMMGT'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NCBM'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NEWRES-WF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NEW-WF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='POWER QUAL'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RECONIDLE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RECVERIF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RETIRE1'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RETIRE2'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SERVCHANGE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SPECIALPRJ'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SUBD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SYSTEMMGT'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='TREE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='TRFX')
                    ORDER BY
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_DESC" DESC,
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"
                        /* End Querey */That is wrapped within a counting Block that is giving me trouble:
    SELECT
        WOCOUNT.Office,
        WOCOUNT.WOCOUNT
    FROM
            SELECT
                CASE
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '1'
                    THEN '1-SUNDANCE'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '2'
                    THEN '2-GILLETTE'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '3'
                    THEN '3-SHERIDAN'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '4'
                    THEN '4-West/Central'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '5'
                    THEN '5-East/Central'
                    ELSE '0-OTHER'
                END                                AS Office,
                COUNT (T1.BI_LMN_SRV_AREA ) AS WOCOUNT
            FROM
                    /* Begin Querey */
                    /* End Querey */
                ) T2
            JOIN
                BI_SO_DET_VIEW_1
            ON
                T2.BI_WRKFLW_KEY = BI_SO_DET_VIEW_1.BI_WRKFLW_KEY
            GROUP BY
                BI_WRKFLW.BI_LMN_SRV_AREA
                 )WOCOUNTThis is the 2 blocks together:
    SELECT
        WOCOUNT.Office,
        WOCOUNT.WOCOUNT
    FROM
            SELECT
                CASE
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '1'
                    THEN '1-SUNDANCE'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '2'
                    THEN '2-GILLETTE'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '3'
                    THEN '3-SHERIDAN'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '4'
                    THEN '4-West/Central'
                    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '5'
                    THEN '5-East/Central'
                    ELSE '0-OTHER'
                END                                AS Office,
                COUNT (T1.BI_LMN_SRV_AREA ) AS WOCOUNT
            FROM
                    /* Begin Querey */
                    SELECT DISTINCT
                        "BI_SO_DET_VIEW_1"."BI_SO_STAT_CD",
                        "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED",
                        "BI_TYPE_SERVICE"."BI_ACCT",
                        "BI_SO_DET_VIEW_1"."BI_SO_NBR",
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_DESC",
                        "BI_SO_TYPE_REF"."BI_SO_TYPE_DESC",
                        "BI_SRV_STAT_REF"."BI_SRV_STAT_DESC",
                        "BI_DIST_OFC_REF"."BI_DIST_NAME",
                        "BI_CNTY_REF"."BI_COUNTY_DESC",
                        "BI_SO_DET_VIEW_1"."BI_CLOSE_DT",
                        "BI_WRKFLW_TASKS"."BI_EVENT_DT_TM",
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD",
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_AREA",
                        "BI_SO_MASTER"."BI_WO_WORKORD"
                    FROM
                        ((((((((("XXXXXX"."BI_SO_MASTER" "BI_SO_MASTER"
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SO_DET_VIEW_1" "BI_SO_DET_VIEW_1"
                    ON
                        "BI_SO_MASTER"."BI_SO_NBR"="BI_SO_DET_VIEW_1"."BI_SO_NBR")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_TYPE_SERVICE" "BI_TYPE_SERVICE"
                    ON
                        "BI_SO_DET_VIEW_1"."ACCT_NBR"="BI_TYPE_SERVICE"."BI_ACCT")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_WRKFLW" "BI_WRKFLW"
                    ON
                        "BI_SO_DET_VIEW_1"."BI_WRKFLW_KEY"="BI_WRKFLW"."BI_WRKFLW_KEY")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SO_TYPE_REF" "BI_SO_TYPE_REF"
                    ON
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"="BI_SO_TYPE_REF"."BI_SO_TYPE_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_WRKFLW_TASKS" "BI_WRKFLW_TASKS"
                    ON
                            "BI_WRKFLW"."BI_WRKFLW_KEY"="BI_WRKFLW_TASKS"."BI_WRKFLW_KEY")
                    AND (
                            "BI_WRKFLW"."BI_WORK_EVENT_CD"="BI_WRKFLW_TASKS"."BI_WORK_EVENT_CD"))
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_DIST_OFC_REF" "BI_DIST_OFC_REF"
                    ON
                        "BI_WRKFLW"."BI_DIST_OFC_CD"="BI_DIST_OFC_REF"."BI_DIST_OFC_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_LMN_SRV_REF" "BI_LMN_SRV_REF"
                    ON
                        "BI_WRKFLW"."BI_LMN_SRV_AREA"="BI_LMN_SRV_REF"."BI_LMN_SRV_AREA")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SRV_LOC" "BI_SRV_LOC"
                    ON
                        "BI_TYPE_SERVICE"."BI_SRV_LOC_NBR"="BI_SRV_LOC"."BI_SRV_LOC_NBR")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_SRV_STAT_REF" "BI_SRV_STAT_REF"
                    ON
                        "BI_TYPE_SERVICE"."BI_SRV_STAT_CD"="BI_SRV_STAT_REF"."BI_SRV_STAT_CD")
                    LEFT OUTER JOIN
                        "XXXXXX"."BI_CNTY_REF" "BI_CNTY_REF"
                    ON
                        "BI_SRV_LOC"."BI_CNTY_CD"="BI_CNTY_REF"."BI_CNTY_CD"
                    WHERE
                            "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='010'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='010NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='011'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='011NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='020'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='020NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='030'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='040'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='041'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='050'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='050A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='051'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='051A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='080'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='085'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='085NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='108'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='140'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='141'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='400'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='401'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='450'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='450A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='451'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='451A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='452'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='452A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='453'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='453A'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='460'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='RHR'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='RHRNS')
                    AND "BI_SO_DET_VIEW_1"."BI_SO_STAT_CD"='O'
                    AND (
                            "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='ANNEXATION'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='AVIAN'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CAPVERIF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CBMRETIRE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CBMSERVCHG'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CONTSERVD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CUSTBLD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='CUTOFF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='DVCM'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='ENERGIZE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='EQUIPCHANG'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='EQUIPVALID'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='HIGHLOAD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='LOCATE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='MAINT-EQ'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='MSYSTEMMGT'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NCBM'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NEWRES-WF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='NEW-WF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='POWER QUAL'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RECONIDLE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RECVERIF'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RETIRE1'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='RETIRE2'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SERVCHANGE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SPECIALPRJ'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SUBD'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='SYSTEMMGT'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='TREE'
                        OR  "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"='TRFX')
                    ORDER BY
                        "BI_LMN_SRV_REF"."BI_LMN_SRV_DESC" DESC,
                        "BI_SO_DET_VIEW_1"."BI_SO_TYPE_CD"
                        /* End Querey */
                ) T2
            JOIN
                BI_SO_DET_VIEW_1
            ON
                T2.BI_WRKFLW_KEY = BI_SO_DET_VIEW_1.BI_WRKFLW_KEY
            GROUP BY
                BI_WRKFLW.BI_LMN_SRV_AREA
                 )WOCOUNTThe last error I received is: [Error Code: 904, SQL State: S0022] [Oracle][ODBC][Ora]ORA-00904: "T2"."BI_WRKFLW_KEY": invalid identifier
    I want the final Output to be something similar to:
    OFFICE          WOCOUNT 
    0-OTHER         44.0       
    1-SUNDANCE      14.0     
    2-GILLETTE      29.0       
    3-SHERIDAN      11.0        
    4-West/Central  37.0       
    5-East/Central  58.0       Thanks for your help,
    :) John

    Hi, John,
    Johnbr (Oracle11G) wrote:
    Hi All,
    I'm failry new to PL/SQL... That's okay; there's no PL/SQL involved in this problem, and there's no PL/SQL needed, only SQL.
    That is wrapped within a counting Block that is giving me trouble:
    SELECT
    WOCOUNT.Office,
    WOCOUNT.WOCOUNT
    FROM
    SELECT
    CASE
    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '1'
    THEN '1-SUNDANCE'
    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '2'
    THEN '2-GILLETTE'
    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '3'
    THEN '3-SHERIDAN'
    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '4'
    THEN '4-West/Central'
    WHEN "BI_WRKFLW"."BI_LMN_SRV_AREA" = '5'
    THEN '5-East/Central'
    ELSE '0-OTHER'
    END                                AS Office,
    COUNT (T1.BI_LMN_SRV_AREA ) AS WOCOUNT
    FROM
    /* Begin Querey */
    /* End Querey */
    ) T2
    JOIN
    BI_SO_DET_VIEW_1
    ON
    T2.BI_WRKFLW_KEY = BI_SO_DET_VIEW_1.BI_WRKFLW_KEY
    GROUP BY
    BI_WRKFLW.BI_LMN_SRV_AREA
    )WOCOUNT
    The sub-query called wocount only involves 2 "tables", namely
    (1) T2
    (2) BI_SO_DET_VIEW_1
    (I put "tables" in quotes because T2 isn't really a table; it's an in-line view. Judging by the name, I'm guessing BI_SO_DET_VIEW_1 isn't really a table, either, but it doesn't matter. You have 2 things that serve as tables, so I'll just call them tables.)
    Since those are the only 2 tables, you can't reference BI_WRKFLW in WOCOUNT. That table is known inside T2, but not outside of T2.
    You can include "BI_WRKFLW"."BI_LMN_SRV_AREA" in the SELECT clause of T2, assuming that doesn't make more rows distinct. T2 will then look like this:
    ...             SELECT DISTINCT
                        BI_SO_DET_VIEW_1.BI_SO_STAT_CD
                        BI_SO_MASTER.BI_WO_WORKORD,
                  BI_WRKFLW.BI_LMN_SRV_AREA          -- New column added
                    FROM ...Then, inside WOCOUNT, you can use BI_LMN_SRV_AREA like this:
    ...         CASE
                    WHEN BI_LMN_SRV_AREA = '1'
                    THEN '1-SUNDANCE'
                    WHEN BI_LMN_SRV_AREA = '2'
                    THEN '2-GILLETTE'
                    WHEN BI_LMN_SRV_AREA = '3'
                    THEN '3-SHERIDAN'
                    WHEN BI_LMN_SRV_AREA = '4'
                    THEN '4-West/Central'
                    WHEN BI_LMN_SRV_AREA = '5'
                    THEN '5-East/Central'
                    ELSE '0-OTHER'
                END                                AS Office,or, to save a little typing:
    ...         CASE  BI_LMN_SRV_AREA
                    WHEN  '1'
                    THEN '1-SUNDANCE'
                    WHEN '2'
                    THEN '2-GILLETTE'
                    WHEN '3'
                    THEN '3-SHERIDAN'
                    WHEN '4'
                    THEN '4-West/Central'
                    WHEN '5'
                    THEN '5-East/Central'
                    ELSE '0-OTHER'
                END                                AS Office, 
    Speaking of saving on typing:
    Instead of
    WHERE
                            "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='001NS'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='010'
                        OR  "BI_TYPE_SERVICE"."BI_PRIM_RATE_SCHED"='RHRNS'
                  )you can use the IN operator, like this:
    WHERE              BI_TYPE_SERVICE.BI_PRIM_RATE_SCHED  IN ('001', '001NS', '010', ..., 'RHRNS') 
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Simplify the problem as much as possible. For example, you would probably have the same problem if there were only 2 o4 3 tables involved in your query. Try to construct a problem that only has 2 or 3 tables, and post only those tables, and the results you want from them.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using.

  • Question On YTD Total Sales Query

    Hello,
    We use this query below to see YTD sales for each of our BP Customers:
    SELECT T0.CardCode 'Acct #', T0.CardName Company, T0.Address
    ' Address', T0.City ' City', T0.State1 State, T0.ZipCode
    'Billing Zip', T0.Phone1 Phone, T0.Balance ' Balance',
    T1.SlpName 'Sales Rep',
    T2.PymntGroup Terms, T3.GroupName 'Group', ((SELECT ISNULL(SUM(INV1.LINETOTAL),0)
    FROM INV1 INNER JOIN OINV ON INV1.DocEntry = OINV.DocEntry
    WHERE OINV.CardCode = T0.CardCode AND Year(INV1.DocDate) = Year(GetDate()))-(SELECT ISNULL(SUM(RIN1.LINETOTAL),0)
    FROM RIN1 INNER JOIN ORIN ON RIN1.DocEntry = ORIN.DocEntry
    WHERE ORIN.CardCode = T0.CardCode AND Year(RIN1.DocDate) = Year(GetDate()))) [YTD Sales]
    FROM OCRD T0
    LEFT JOIN OSLP T1 ON T1.SlpCode = T0.SlpCode
    LEFT JOIN OCTG T2 ON T2.GroupNum = T0.GroupNum
    LEFT JOIN OCRG T3 ON T3.GroupCode = T0.GroupCode
    WHERE T0.CardType = 'C'
    We use this query below to see daily Invoice and Credit Memo postings for a selected period:
    SELECT 'INVOICE' as "Doc Type", T0.DOCNUM as "Doc Number", T0.CARDCODE as "Customer Code", T0.CARDNAME as "Customer Name", T0.DOCDATE as "Posting Date", T0.NUMATCARD as "Customer Ref #", T0.DocDueDate, T0.DocTotal
    FROM [dbo].[OINV] T0 WHERE T0.DOCDATE BETWEEN '[%0]' And '[%1]'
    UNION ALL
    SELECT 'CREDIT MEMO', T0.DOCNUM,T0.CARDCODE, T0.CARDNAME, T0.DOCDATE, T0.NUMATCARD, T0.DocDueDate, -1*T0.DocTotal
    FROM [dbo].[ORIN] T0 WHERE T0.DOCDATE BETWEEN '[%0]' And '[%1]'
    My question is -- shouldn't the sum of the YTD column in the 1st Query be the same as the sum of the Doc Total column in the 2nd Query (given that all dates are selected in the 2nd Query)? 
    This doesn't appear to be the case and I was wondering why?
    Thanks,
    Mike

    Mike,
    The DocTotal may contain Freight and Handling expenses, While the first query only taken the SUM of the line total of the Items.
    Thats why they may be different.
    Suda

  • Help in Total sum query

    Hello, How I can sum(code1). Every code1 has a group and every group's total (total_per) should be 100. I want to add the comments column in the result as well. Thanks.
    create table #Cumulate (code numeric (10), code1 numeric (10), total_per numeric(10),code_desc char(20))
    insert into #Cumulate values (1,121,10,'DHJ')
    insert into #Cumulate values (2,121,90,'DHJ')
    insert into #Cumulate values (3,121,10,'DHJ')
    insert into #Cumulate values (4,122,10,'CRT')
    insert into #Cumulate values (5,122,80,'CRT')
    insert into #Cumulate values (6,122,05,'CRT')
    insert into #Cumulate values (7,122,10,'CRT')
    insert into #Cumulate values (8,123,10,'DRT')
    insert into #Cumulate values (9,123,60,'DRT')
    insert into #Cumulate values (10,123,10,'DRT')
    insert into #Cumulate values (11,123,10,'DRT')
    insert into #Cumulate values (12,123,10,'DRT')
    insert into #Cumulate values (13,124,20,'DRT')
    insert into #Cumulate values (14,124,60,'DRT')
    insert into #Cumulate values (15,124,10,'DRT')
    insert into #Cumulate values (16,124,10,'DRT')
    insert into #Cumulate values (17,124,10,'DRT')
    --Results
    Code1 total_per Commments
    121   100         Total is 100
    122   105         Total is not 100
    123   100         Total is 100
    124   110        Total is not 100

    Try the below:
    Select Code1, Sum(Total_per) total_per, 'Total is '+Case when Sum(Total_per)=100 then '' else ' not' end+'100' Comments
    From
    #Cumulate
    Group by Code1
    However, I would suggest you to do the comment session at presentation layer, which would be a good for performance.
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped.
     [Blog]

  • Best Way to calculate totals from query

    Could someone point me in the right direction to add up my
    data and distinctly show it in my query?
    I have a table with the following fields:
    id, team_id, compname, teamname, totallost
    I want to add up the "totallost" row where the "team_id" and
    "compname" fields are the same...then show the compname with the
    sum of the totallost once in my table and determine who is
    winning.

    Thank you for the great help. This code works well, but is
    there a way to display the highest totallost and differentiate
    between competition names? My example is for one compname, but the
    table will have multiple compname's and I want to build a table
    showing only the highest totallost for each compname.
    You guys have been a great help. I learned something new
    today already.

  • Total Line in BEX query output

    Hello,
        I have a requirement, where we have to show just total line in output for BEX query. We want to hide {though hide feature will not work} line items that we process. How can we do it?
       Requirement goes like this:
       Let say for orders, we have order requested date and actual delivered date. The query formula processes all line items for given selection and measures ontime, late and early shipments based on date in variable exit we have. At output (that is in total line)query will write have % ontime late early.
       So for selection calmonth we need to show % ontime late early but for that we need to process all line items to get % calculation. So, how can I keep only total line in query output.
      For example: Output should look like for selection of calmonth, plant - product  or shipping point etc..
      Calmonth plant - % ontime late early or
      Calmonth product - % ontime late early or
      Calmonth shipping point - % ontime late early
      It is working in a way it shows all line items and total line at last. All I want is total line and ignore line items.
      Thanks,
      Aj

    Remove the drilldowns for the "detail" characteristics that you do not want to see. The report will then display only the "totals" that you do want to see.
    Hope this helps...
    Bob

Maybe you are looking for

  • F4 help for a field in UI(CRM 5.1)

    Hi All Can anybody tell me how to do F4 help for a field in UI( CRM 5.1). scenario is given below. I have 2 customer fields added to UI using EEW and have custom table for maintaining field values. need to add F4 help for field1 ( field1 and field2 v

  • Calculate coherence for a series of Impacts

    Calculating coherence has been discussed several times - including this knowledge base article and community example. But I'm still missing something. How can I calculate the coherence so it updates after each hit (right now it's always 1)? On a rela

  • OSX 10.7 ends support

    I have a mid-july 2012 macbook air with osx 10.7.5. I'm receiving messages informing about the ending of support for this OS. It also says that from october 31st on my box sync won't be able to syncronize. What is all this about? What should I do? Wh

  • RSTRAN & RSBKDTP doesn't convert source system

    Dear All, after a system copy from BWP to BWT, and the opportune post-activities (BDLS & change of conversion of source system name after transport), there's a problem with the transport from BWS in BWT of transformation and DTP objects that, respect

  • Acrobat Reader DC: Open with right pane closed?

    Is there a preference so that Acrobat Reader DC will open a PDF with the right pane closed (for desktop, either Windows or OSX)?