Customised sub-total via UNION query - how to exclude from totals?

Hi,
I have a client requirement that a couple of companies have their own sub-total in a report, a treatment which applies to no other companies in their group.
I have achieved this via a union all, but they also want a total of companies.
Is there anyway to emulate a Grand Total, other than another union query such that the double count is not double counted?
thanks for any ideas.
Robert.

only solution which i can think of is to create another union request for the grand total.. this way you wont add up your sub totals twice..

Similar Messages

  • Totals in BEx query not matching with actual total

    Hi,
    We have one report in BI, the total for few columns (Given by BI system i.e system generated) for the values of the key figures is not matching with actual total of that values...
    I am confused to see, how sap BI can do the totaling mistake...
    Please suggest and help....
    Thanks,
    Nitika

    Hi
    Please check following things
    1. Check key figure aggregation properties, how it is defined, it might have been defined as Avg, Last,first value or it might using exceptional aggregates.
    2. Double check your process of validation between cube and bex report for totals.
    Thanks
    Raghu.

  • How to exclude from layout if control is invisible?

    Hi,
    When I set visible property of any control to false I want that control to not take any size from parent container.
    Is there somthing like IncludeInLayout like in Adobe Flex framework?

    I use GridPane.
    First I add 5 children at 5 rows of GridPane. After some button click I dinamically add one more control between child 3 and 4 from my GridPane. Like I said previosly in this case the tab orde is wrong (after focus out from child 3 next focus child is 5, but must be recently added control 4).
    The other discomfort is for example:
    I have HBox with 3 children added, if I want to hide child 2 (remove it from HBox) and then want to show it (add it to HBox) I must insert it at specific position, which depends from other controls in that HBox (because meanwhile I mey remove and child 1 for example).

  • How to get Average of a column in UNION query

    Hi All,
    I will try to explain the issue as much as I can and If you need clarification on any piece please let me know.
    So, I have a Union Query with two sets of Criteria.
    The first Criteria Brings the list of Buildings and a set of Measures for it. The second criteria does the same but with a different set of filters.
    No I use a UNION between these both criteria and present them in a Pivot view so that we only have one row per each building. The values in each of the criteria in the UNION query are Summed
    Ex:
    Building
    Metric 1
    Metric 2
    1
    20
    30
    1
    25
    35
    2
    40
    50
    2
    45
    55
    So as I show in the pivot the result is
    Building
    Metric 1
    Metric 2
    1
    45
    65
    2
    85
    105
    Now the issue is with the Grand Total.
    I want the Grand Total to be an Average of the Buildings. So for Metric 1 The Grand Total should be (45+85)/2 because we have two buildings so the answer should be 65.
    How can I get that.
    Bottom Line: Need an Average as the Grand Total when the Aggregation rule on the column is set to Sum.
    we use 11.1.6.10 version

    Hi VJ
    What you are asking is impossible in the pivot table itself as you are asking one part of the pivot table to aggregate via one method, and another to aggregate via another method, on the same column ..
    It can be done using a calculated item. For your example above you'd use the formula ( $1 + $2) / 2.
    .. But thats HIGHLY explicit, and would only work if you KNOW how many buildings will come back in the query.. EVERYTIME.
    Why not restructure the query to combine it into a single query using an combinations of filters with OR in between?

  • How will write SQL query to fetch data from  each Sub-partition..

    Hi All,
    Anyone does have any idea about How to write SQL query to fetch data from Sub-partition.
    Actually i have one table having composite paritition(Range+list)
    Now if i want to fetch data from main partition(Range) the query will be
    SELECT * FROM emp PARTITION(q1_2005);
    Now i want to fetch data at sub-partition level(List) .But i am not able to get any SQL query for that.
    Pls help me to sort out.
    Thanks in Advance.
    Anwar

    SELECT * FROM emp SUBPARTITION(sp1);

  • How do I make a 'union' query?

    Hi,
    How do I make a union query in JDev? Something like this.
    select 'x' from dual
    union
    select 'y' from dual;
    to get a rowsetinfo for showing in a combobox control? Don't ask
    me to create view in the database:-). It may not be practical for
    all such adhoc queries.
    --Gopal
    null

    Hi
    Sorry for that one. I didn't do my rtfm before asking that. There
    is a constructor for QueryInfo which takes any sql statement.
    --Gopal
    Gopal (guest) wrote:
    : Hi,
    : How do I make a union query in JDev? Something like this.
    : select 'x' from dual
    : union
    : select 'y' from dual;
    : to get a rowsetinfo for showing in a combobox control? Don't
    ask
    : me to create view in the database:-). It may not be practical
    for
    : all such adhoc queries.
    : --Gopal
    null

  • Calculate total from a Union query

    Hi
    I want to UNION 3 query and need to create a total column. Below My table
    ID    Date           NoofVisit
    1    4/21/2014    1
    1    4/22/2014    1
    1    4/23/2014    1
    1    4/24/2014    1
    Another query
    ID    Date           NoofVisit
    1    4/21/2014    2
    1    4/24/2014    1
    1    4/26/2014    1
    Another query
    ID    Date           NoofVisit
    1    4/22/2014    2
    1    4/26/2014    1
    1    4/28/2014    1
    My query is below
    SELECT Id, Date, NoofVisit
    FROM dbo.vw_MO_All_FirstVisit
    UNION
    SELECT Id, Date, NoofVisit
    FROM vw_MO_All_SecondVisit
    UNION
    SELECT Id, Date, Noofvisit
    FROM vw_MO_All_ThirdVisit
    The result is
    ID    Date           NoofVisit
    1    4/21/2014    1
    1    4/22/2014    1                      Its should be 3
    1    4/23/2014    1
    1    4/24/2014    1                     Its should be 2
    1    4/26/2014    1                     Its should be 2
    1    4/28/2014    1
    Now what should the query is?
    Please help.
    Thanks in advance

    You can also get this output using JOIN
    SELECT COALESCE(t1.ID,t2.ID,t3.ID) AS ID,
    COALESCE(t1.[Date],t2.[Date],t3.[Date]) AS [Date],
    COALESCE(t1.NoOfVisit,0) + COALESCE(t2.NoOfVisit,0) + COALESCE(t3.NoOfVisit,0) AS TotalVisits
    FROM dbo.vw_MO_All_FirstVisit t1
    FULL JOIN dbo.vw_MO_All_SecondVisit t2
    ON t2.ID = t1.ID
    AND t2.[Date] = t1.[Date]
    FULL JOIN dbo.vw_MO_All_ThirdVisit t3
    ON t3.ID = t1.ID
    AND t3.[Date] = t1.[Date]
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to get total numbers and total price in query reports

    Hi,
    how to get total numbers and total price in query reports.for example:
    particular item is issued 3 times a week...I need total quantity of item issued
    my query...
    SELECT T0.[DocNum], T1.[ItemCode], T1.[Quantity], T1.[Price] FROM OIGE T0  INNER JOIN IGE1 T1 ON T0.DocEntry = T1.DocEntry WHERE T1.[ItemCode]  Like '%%[%1]%%'  and  T1.[U_WOType]='[%0]'

    Hi,
    Try this:
    SELECT T1.[ItemCode], SUM (T1.[Quantity]) as [Total Quantity], SUM (T1.[Quantity] * T1.[Price]) as [Total Price] FROM OIGE T0  INNER JOIN IGE1 T1 ON T0.DocEntry = T1.DocEntry WHERE T1.[ItemCode]  Like '%%[%1]%%'  and  T1.[U_WOType]='[%0]'
    Group By T1.[ItemCode]
    Beni.

  • Difference between 2 sub-totals in SAP query

    Hello Experts,
    Is there any possibility to find out the difference between 2 sub-totals and display it at the bottom of the ALV output( like total) in SAP Query using SQ01.
    For example: see below example(Request you to paste the below in an excel sheet for more readability).
    PRZ     25.05.2011     A 007 008 01 15     10     EA           0,00     INR          0,00     INR          2,00     INR               0,00     INR
    PRZ     27.05.2011     A 007 008 01 00     1     PC          10,00     INR          0,00     INR          0,20     INR               0,10     INR
    PRZ     27.05.2011     A 007 008 01 00     1     PC          10,00     INR          0,00     INR          0,20     INR               0,10     INR
    Sub-total               10     EA     20,00     INR          0,00     INR     2,40     INR     20     INR
                   2     PC                                        
    DRP     26.05.2011     WDB2020261X744924     1     PC          60,00     INR         50,00     INR         30,00     INR               0,00     INR
    DRP     31.05.2011     WDB2020261X744924     1     PC          60,00     INR         50,00     INR         30,00     INR               0,00     INR
    Sub-total               2     PC     120,00     INR     100,00     INR     60,00     INR               0,00     INR
    Grand totals               10     EA     140,00     INR     100,00     INR     62,40     INR     20     INR
                   4     PC                                        
    In the same way as Grand total, Is there any possibility to find out the difference between the 2 sub-totals and display at the bottom after the Grand Totals row.
    Please let me know if there is any possibility for the same.

    Just to update on my question.
    In the sub-totals line you can see that there is 20,00 INR value which is summation of the above fields present in individual line numbers above. And the same way for the rest of the fields as well.
    After calculating the sub-totals in this manned, I need to find out the difference between the subtotals and display it at the bottom of the output.
    Please let me know if this is possible.

  • How to have customised order in a SQL query

    say there is a column name ename having values shailesh,Shailesh,ruchi,Ramesh,carl,Celin
    i need a order of
    carl,Celine,ruchi,Ramesh,shailesh,Shailesh
    how to do that

    it will certenlly help, try this one-
    First u need to create a function to change the case. e.g.-
    create or replace function chg_case(str varchar2) return varchar2 is
    chr char(1);
    nstr varchar2(255);
    begin
    for i in 1..length(str) loop
    chr := substr(str,i,1);
    if chr = Upper(chr) then
    nstr := nstr| |lower(chr);
    else
    nstr := nstr| |upper(chr);
    end if;
    end loop;
    return nstr;
    end;
    now see the the result of this query-
    1 select name from (
    2 select name, upper(substr(name,1,1)) lcs, substr(chg_case(name),1,40) nm from tst order by 2,3
    3* ) t
    SQL> /
    NAME
    carl
    Celin
    ruchi
    Ramesh
    shailesh
    Shailesh
    6 rows selected.
    null

  • How to split batch to several sub-batch via 'MB1B'

    Hi, experts, i would like to split batch into several sub-batches via 'MB1B', is there relative move type?

    You can use 309 material to material transfer movement type. Transfer your stock to from Batch A to Batch B, C, D, E (in Receiving Batch Field) depends on the quantity you have.
    If It helps you, kindly give points.
    Regards,
    RM

  • How to find the total number of pair under particular parent according to pattern 1:2or2:1,1:1 day to day maximum up to 5 pair caping daily

    Dear friends,
    I provide full details here ,my database table structure ,data and stored procedure and my problem ,
    please review it and provide the solution or any idea to solve my problem.
    I am working on a project in which members are added in a tree pattern, and get the payment accordingly.
    below is my table structure ,data and stored procedure
    CREATE TABLE Associate_Income
    ID varchar(30) NOT NULL,
    ParentID varchar(30) NULL,
    IsLeft tinyint NULL,
    IsRight tinyint NULL,
    joingdate datetime NOT NULL
    go
    INSERT Associate_Income
    (ID, ParentID, IsLeft, IsRight, joingdate)
    SELECT 'Ramesh123', NULL, NULL, NULL '2014-01-03 16:31:15.000' UNION ALL
    SELECT 'Sonu', 'Ramesh123', 1, NULL, '2014-01-03 16:45:21.000' UNION ALL
    SELECT 'Pawan kumar', 'Ramesh123', NULL, 1, '2014-01-04 16:50:23.000' UNION ALL
    SELECT 'Ravi123', 'Sonu', 1, NULL, '2014-01-04 17:03:22.000' UNION ALL
    SELECT 'Vineet123', 'Sonu', NULL, 1, '2014-01-04 17:26:01.000' UNION ALL
    SELECT 'dev123', 'Ravi123', 1, NULL, '2014-01-05 19:35:16.000' UNION ALL
    SELECT 'Mukesh123', 'Ravi123', NULL, 1, '2014-01-05 19:40:41.000' UNION ALL
    SELECT 'poonam123', 'Vineet123', 1, NULL, '2014-01-05 19:49:49.000' UNION ALL
    SELECT 'monu', 'Pawan kumar', 1, NULL, '2014-01-05 17:32:58.000' UNION ALL
    SELECT 'Arti123', 'Pawan kumar', NULL, 1, '2014-01-05 19:54:35.000' UNION ALL
    My  database table Associate_Income  structure and data is as follow:
    ID ParentID IsLeft IsRight joingdate
    Ramesh123 NULL NULL NULL 2014-01-03 16:31:15.000
    Sonu Ramesh123 1 NULL 2014-01-03 16:45:21.000
    Pawan kumar Ramesh123 NULL 1 2014-01-04 16:50:23.000
    Ravi123 Sonu 1 NULL 2014-01-04 17:03:22.000
    Vineet123 Sonu NULL 1 2014-01-04 17:26:01.000
    dev123 Ravi123 1 NULL 2014-01-05 19:35:16.000
    Mukesh123 Ravi123 NULL 1 2014-01-05 19:40:41.000
    poonam123 Vineet123 1 NULL 2014-01-05 19:49:49.000
    monu Pawan kumar 1 NULL 2014-01-05 17:32:58.000
    Arti123 Pawan kumar NULL 1 2014-01-05 19:54:35.000
    by using below stored procedure i can count the total number of pairs under particular node in 2:1,1:1 ratio means first pair is completed when two node added to the left side of given parent node and one node added
    right side of given parent node after that all pairs are completed when one node added left side and one node added right side of parent node (1:1 ratio)
    example if i execute my stored procedure as follows it would return following.
    EXEC count_pairs 'Ramesh123'
    3
    so there is 3 pairs as shown in my figure.
    when we execute my stored procedure for ParentID 'sonu' it would return following.
    EXEC count_pairs 'sonu'
    2
    so there is 2 pairs as shown in my figure.
    My problem is to find the query which can return the total number of pair under particular node any given parent  node. day to
    day maximum 5 pairs in a day please any one can suggest us
    CREATE proc [dbo].[count_pairs]
    @ParentID nvarchar(50)
    as
    begin
    Declare @ParentSUM SMALLINT = 0
    Declare @SubLeftID nvarchar(50)
    Declare @SubRightID nvarchar(50)
    SELECT @SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE @SubLeftID END
    ,@SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE @SubRightID END
    FROM Associate_Income
    WHERE ParentID = @ParentID
    IF @SubLeftID IS NOT NULL AND @SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM Associate_Income WHERE [IsLeft] = 1 AND ParentID = @SubLeftID)
    BEGIN
    SET @ParentSUM = 1
    ;WITH Associate_Income_CTE AS
    SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level]
    FROM Associate_Income
    WHERE [ParentID] = @ParentID
    UNION ALL
    SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1
    FROM Associate_Income RecursiveMember
    INNER JOIN Associate_Income_CTE AnchorMember
    ON RecursiveMember.[ParentID] = AnchorMember.[ID]
    SELECT @ParentSUM = @ParentSUM + COUNT([ParentID])
    FROM
    SELECT [ParentID]
    ,'IsLeft' AS [Direction]
    ,1 AS [Value]
    FROM Associate_Income
    WHERE [IsLeft] = 1
    AND [ID] <> @ParentID --AND [ID] NOT IN (@SubLeftID, @ParentID)
    AND [ParentID] NOT IN (@ParentID, @SubLeftID)
    UNION ALL
    SELECT [ParentID]
    ,'IsRight' AS [Direction]
    ,1 AS [Value]
    FROM Associate_Income
    WHERE [IsRight] = 1
    AND [ParentID] <> @ParentID
    ) AS Associate_Income
    PIVOT
    MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
    ) PVT
    WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
    END
    SELECT @ParentSUM
    Jitendra Kumar Sr. Software Developer at Ruvixo Technologies 7895253402

    I don't think this is homework, I am not sure how helpful it was by Kalman to merge the two threads. It appears that two different persons posted the questions. It could be though, that it is the same problem and Jitendra has taken over Chandra's
    task.
    However, I was not able to understand the problem nor the figure. And nor the definition of pairs in this context. My assumption is that what Jitendra posted is an abstraction of the actual problem in order to not reveal intellecutal property. Possibly this
    makes the problem more difficult to understand for us outsiders.
    I've come so far that I worked out a table definition and INSERT statements with sample data, as well as a call to the procedure which returns 3 and this appears to map to the figure, but I don't know what it means. Since I don't know what this
    is about, I have not made any attepmts to understand the code, but I would appreciate clarification about the underlying business rules as well as the expected results for various test cases.
    CREATE TABLE Associate_Income(ID varchar(30) NOT NULL,
    ParentID varchar(30) NULL,
    IsLeft tinyint NULL,
    IsRight tinyint NULL,
    joingdate datetime NOT NULL)
    go
    INSERT Associate_Income (ID, ParentID, IsLeft, IsRight, joingdate)
    SELECT 'Ramesh123', NULL, NULL, NULL, '2014-01-03 16:31:15.000' UNION ALL
    SELECT 'Sonu', 'Ramesh123', 1, NULL, '2014-01-03 16:45:21.000' UNION ALL
    SELECT 'Pawan kumar', 'Ramesh123', NULL, 1, '2014-01-04 16:50:23.000' UNION ALL
    SELECT 'Ravi123', 'Sonu', 1, NULL, '2014-01-04 17:03:22.000' UNION ALL
    SELECT 'Vineet123', 'Sonu', NULL, 1, '2014-01-04 17:26:01.000' UNION ALL
    SELECT 'dev123', 'Ravi123', 1, NULL, '2014-01-05 19:35:16.000' UNION ALL
    SELECT 'Mukesh123', 'Ravi123', NULL, 1, '2014-01-05 19:40:41.000' UNION ALL
    SELECT 'poonam123', 'Vineet123', 1, NULL, '2014-01-05 19:49:49.000' UNION ALL
    SELECT 'monu', 'Pawan kumar', 1, NULL, '2014-01-05 17:32:58.000' UNION ALL
    SELECT 'Arti123', 'Pawan kumar', NULL, 1, '2014-01-05 19:54:35.000'
    go
    CREATE proc [dbo].[count_pairs]
    @ParentID nvarchar(50)
    as
    begin
    Declare @ParentSUM SMALLINT = 0
    Declare @SubLeftID nvarchar(50)
    Declare @SubRightID nvarchar(50)
    SELECT @SubLeftID = CASE WHEN [IsLeft] = 1 THEN [ID] ELSE @SubLeftID END
    ,@SubRightID = CASE WHEN [IsRight] = 1 THEN [ID] ELSE @SubRightID END
    FROM Associate_Income
    WHERE ParentID = @ParentID
    IF @SubLeftID IS NOT NULL AND @SubRightID IS NOT NULL AND EXISTS(SELECT 1 FROM Associate_Income WHERE [IsLeft] = 1 AND ParentID = @SubLeftID)
    BEGIN
    SET @ParentSUM = 1
    ;WITH Associate_Income_CTE AS
    SELECT [ID], [ParentID], [IsLeft], [IsRight], 0 AS [Level]
    FROM Associate_Income
    WHERE [ParentID] = @ParentID
    UNION ALL
    SELECT RecursiveMember.[ID], RecursiveMember.[ParentID], RecursiveMember.[IsLeft], RecursiveMember.[IsRight], Level + 1
    FROM Associate_Income RecursiveMember
    INNER JOIN Associate_Income_CTE AnchorMember
    ON RecursiveMember.[ParentID] = AnchorMember.[ID]
    SELECT @ParentSUM = @ParentSUM + COUNT([ParentID])
    FROM
    SELECT [ParentID]
    ,'IsLeft' AS [Direction]
    ,1 AS [Value]
    FROM Associate_Income
    WHERE [IsLeft] = 1
    AND [ID] <> @ParentID --AND [ID] NOT IN (@SubLeftID, @ParentID)
    AND [ParentID] NOT IN (@ParentID, @SubLeftID)
    UNION ALL
    SELECT [ParentID]
    ,'IsRight' AS [Direction]
    ,1 AS [Value]
    FROM Associate_Income
    WHERE [IsRight] = 1
    AND [ParentID] <> @ParentID
    ) AS Associate_Income
    PIVOT
    MAX([Value]) FOR [Direction] IN ([IsLeft], [IsRight])
    ) PVT
    WHERE [IsLeft] IS NOT NULL AND [IsRight] IS NOT NULL
    END
    SELECT @ParentSUM
    END
    go
    EXEC count_pairs 'Ramesh123'
    go
    DROP PROCEDURE count_pairs
    DROP TABLE Associate_Income
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Sum of columns in union query

    hi, i am using oracle 10g database..
    how to get the sum of column in union query ?
         select * from (select 100 records from dual), (select 50 available from dual)
    union
    select * from (select 200 records from dual), (select 150 available from dual)
    display should be like
                     records         available
                      100               50
                      200               150
    total            300               200thanks ...

    Peter vd Zwan wrote:
    try this:Grouping by records will not produce correct results:
    SQL> with a as
      2  (
      3  select * from (select 100 records from dual), (select 50 available from dual)
      4  union
      5  select * from (select 100 records from dual), (select 100 available from dual)
      6  union
      7  select * from (select 200 records from dual), (select 150 available from dual)
      8  )
      9  select
    10    case when grouping(records) = 0 then null else 'total' end tot
    11    ,sum(records)   records
    12    ,sum(available) available
    13  from
    14    a
    15  group by
    16  rollup (records)
    17  ;
    TOT      RECORDS  AVAILABLE
                 200        150
                 200        150
    total        400        300
    SQL> select  case grouping(rownum)
      2     when 1 then 'Total'
      3   end display,
      4          sum(records) records,
      5          sum(available) available
      6    from  (
      7            select * from (select 100 records from dual), (select 50 available from dual)
      8           union
      9            select * from (select 100 records from dual), (select 100 available from dual)
    10           union
    11            select * from (select 200 records from dual), (select 150 available from dual)
    12          )
    13    group by rollup(rownum)
    14  /
    DISPL    RECORDS  AVAILABLE
                 100         50
                 100        100
                 200        150
    Total        400        300
    SQL> SY.

  • 30,60,90 Days Totals in sql query

    Hi All,
    Here I  have written a query to find the total for 30Days to calculate costs....
    These 30Days Amount will be populated into another table based on joins.
    I know we can do by case in the final update but lot of logic is going into temptables1 and 2, so please help...
    select distinct ip.membernbr,ip.patcom,ip.admitdate,ip.dischargerdate,dateadd(day,1,ip.dishargedate) as afterdishcargedatestart,
    dateadd(day,30,ip.dishargedate) as afterdishcargedateend, cd.specificdateofservice as ipdos, cd.specificdateofservicethry
    as ipdosthru
    into #inpatientdoshospice30
    from tbl_inpatient ip
    join claimsdetail cd on cd.membernbr=ip.membernbr
    join claims c on c.claimsseqnbr=cd.claimnsseqnbr
    where c.formnbr='inp'
    and cd.specificdateofservice between dateadd(day,1,ip.dishargedate)
     and dateadd(day,30,ip.dishargedate) 
    and cd.specificdateofservicethru
    between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 
    --find part c hospice claims lines that that are already included in the inpatientcost(c.formnbr='INP')
    cost;
    --doing this in a separate step in case there are multiple inpatient claims which dos dates that overlap
    (e.g; 1/15 to 1/20 and ---1/18 to 1/20) causing us to overstate dollars
    select distinct ip.patcom,cd.claimssequencenbr
    into #Hospiceinpatientclaimnslines30
    from
    #inpatientdoshospice30 ip 
    join claimsdetail cd on cd.membernbr=ip.membernbr
    and cd.specificdateofservice between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 
    and cd.specificdateofservicethru between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 
    join claims c on c.claimsseqnbr=cd.claimnsseqnbr
    where c.formnbr='Hospice'
    --sum all hospice claims excluding those already included in the inpatient
    update pp
    set homehospicecost30day=isnull(sub.hospicecost,0)
    from inpatient pp
    left join(
    select ip.patcom,sum(isnull(cd.netamt,0.00)) as hospicecost
    from tbl_inpatient ip
    join claimsdetail cd on cd.membernbr=ip.membernbr
    and cd.specificdateofservice between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 
    and cd.specificdateofservicethru between dateadd(day,1,ip.dishargedate)  and dateadd(day,30,ip.dishargedate) 
    join claims c on c.claimsseqnbr=cd.claimnsseqnbr
    where (c.formnbr='Hospice' or (c.formnbr='PartB' and cd.placeofservice in ('34')))
    and cd.claimsseqnbr not in (select cklaimseqnbr from #hospiceinpatientclaimlines30 sub where sub.patcom=ip.patcom)
    group by ip.patcom
    )sub
    on pp.patcom=sub.patcom
    I really appreciate your help.
    Thanks,
    Kalyan.

    The UPDATE can be written this way:
    update pp
    set    homehospicecost30day = isnull(sub.hospicecost,0)
    from   inpatient pp
    left   join(select ip.patcom,
                       sum(CASE WHEN cd.specificdateofservice between 
                                     dateadd(day,1,ip.dishargedate)  AND
                                     dateadd(day,30,ip.dishargedate) 
                                 AND cd.specificdateofservicethru between
                                     dateadd(day,1,ip.dishargedate)  and 
                                     dateadd(day,30,ip.dishargedate) 
                                THEN isnull(cd.netamt,0.00)
                                ELSE 0
                            END) as hospicecost30
                from   tbl_inpatient ip
                join   claimsdetail cd on cd.membernbr=ip.membernbr
                join   claims c on c.claimsseqnbr=cd.claimnsseqnbr
                 where  (c.formnbr='Hospice' or
                        (c.formnbr='PartB' and cd.placeofservice in ('34')))
                   and cd.claimsseqnbr not in (select cklaimseqnbr from
                                               #hospiceinpatientclaimlines30 sub
                                               where sub.patcom=ip.patcom)
                 group by ip.patcom)sub
    on pp.patcom=sub.patcom
    The pattern shows how you can add further days.
    I was not able to figure out the meaning of the temp tables, and since you said that you typed it from memory without access to the code, I don't trust that you got everything right. Give it a second look on Monday.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • How to see customers total sales orders value

    Dear all,
    How can I create a query to see all customers total sales orders value.
    I have tried MC+E transaction, it is not giving accurate values when compare for a customer with VA05N report.
    moreover it doesn't have filter option.
    Please help me to resolve the issue. Your suggestion will be highly appreciated.
    Best regards,
    Raghu ram

    Hi,
    Try S_ALR_87012171  and S_ALR_87012186.   or
    S_ALR_87012160, select customer lite items only, enter customer number.
    Click on dynamic selection (Shift+F4). enter RV in document type field and press execute.
    Regards,
    Chandra

Maybe you are looking for