Calculating 30,60,90 Days Totals in sql

Hi All,
Here I  have written a query to find the total for 30Days for a perticular member after the discharge date.
These 30Days Amount will be populated into another table based on joins.
Now my question is i am trying to get the total for 60 Days , 90 Days and 180 Days.
I can write Different queries changing datediff to 60,90,180 but i want all those fields in single query .
please suggest how to write in single query.... 
Select Distinct  ip.member
sum(isnull(NetAmt,0.00)) as 30DaysAmount
from claims c
join claimsDetail cd on c.claimsnbr=cd.claimsnbr
join inpatient ip on cd.membernbr=ip.membernbr
where c.formnbr='SNF' 
and (datediff(day,cd.specificdateofservice ,dischargedate) between 0 and 30
group by ip.member
I really appreciate your help.
Thanks,
Kalyan.

Hi Visakh16/Dan,
I tried using case statement in select statement itself using 
case when datediff(d,cd.specificdateofservice,dischargedate) between 0 and 30 then sum(isnull(NETAMT),0.00)
then it throws an error i cannot use between in select, then i removed then it says all other columns not part of aggregate must be part of group by(similar to that)
Then i included all  other fields in group by, but i am worried will the value be correct ?
Thanks for your reply i will try using your query as above:using SUM(case.....)
I dont have access to work network, so i am writing general wordings of errror and query...
Thanks,
Kalyan.
The CASE..WHEN should be inside the SUM
you can also use this
Select ip.member,
sum(case when datediff(day,cd.specificdateofservice ,dischargedate) >= 0 and datediff(day,cd.specificdateofservice ,dischargedate) <= 30 then isnull(NetAmt,0.00) else 0.00 end) as 30DaysAmount,
sum(case when datediff(day,cd.specificdateofservice ,dischargedate) >= 0 and datediff(day,cd.specificdateofservice ,dischargedate) <= 60 then isnull(NetAmt,0.00) else 0.00 end) as 60DaysAmount,
sum(case when datediff(day,cd.specificdateofservice ,dischargedate) >= 0 and datediff(day,cd.specificdateofservice ,dischargedate) <= 90 then isnull(NetAmt,0.00) else 0.00 end) as 90DaysAmount,
sum(isnull(NetAmt,0.00)) as 180DaysAmount
from claims c
join claimsDetail cd on c.claimsnbr=cd.claimsnbr
join inpatient ip on cd.membernbr=ip.membernbr
where c.formnbr='SNF'
and datediff(day,cd.specificdateofservice ,dischargedate) between 0 and 180
group by ip.member
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

Similar Messages

  • 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]

  • Calculation Based on Previous Day's Figure

    Hi all,
    This is a solution I implemented several years ago, which is now causing problems and must be rectified.  The intention was to compare a single field across two days and calculate the difference.  The only way I could figure out how to do that
    back then was to go back and loop through the whole table, calculating the difference each day until the present day.  This obviously isn't very efficient and now something has gone wrong.
    Here's the code:
    // Calculates Nett Oil Volume Increase
    double previousNettOilVolA = 82433; // value on 2/1/13 which is the live value on the day before this system begins
    double previousNettOilVolB = 51347; // value on 2/1/13 which is the live value on the day before this system begins
    foreach (DailyTankData tankdata in DataWorkspace.ApplicationData.DailyTankDatas)
    if (tankdata.NettOilVolume15TankA != null)//otherwise will crash if null value
    tankdata.NettOilVolumeIncreaseTankA = (double)tankdata.NettOilVolume15TankA - previousNettOilVolA;
    previousNettOilVolA = (double)tankdata.NettOilVolume15TankA;
    tankdata.NettOilVolumeIncreaseTankB = (double)tankdata.NettOilVolume15TankB - previousNettOilVolB;
    previousNettOilVolB = (double)tankdata.NettOilVolume15TankB;
    This is run on the _Compute event of another property in the table.  It seemed to work for a long time, but now is occasionally giving wildly inaccurate numbers.
    What's a better way to solve this problem?  Failing that, can anyone explain exactly how the above works and why it might be going wrong?
    Thanks,
    Liam

    Start by looking at your data. Any unusual spikes or negative values?
    A better way to handle it is to let the database do the calculations via a stored procedure.

  • Days total of songs wrong

    I have an odd question. I have a certain number of song in Itunes. The days total was 181 days. I have a lot of songs. In the process of copying the songs from one external drive to another external drive I messed up the process. When I finally got it straightened out I had the same number of songs but the days total had gone down to 84 days. There were no songs missing but obviously a huge difference in days. In the process of importing songs into Itunes does it inflate the days total somehow? Hope somebody can enlighten me because it's a strange situation. Thanks

    This is a difficult question for anyone on the internet to answer for you.
    All I can think of is start comparing the list from itunes #1 to the list of itunes #2 and see what the differences seem to be.
    That is a VERY big difference in the amount of play time, so I'd think you'd be able to see whether files were missing or not.
    +In the process of copying the songs from one external drive to another external drive I messed up the process. When I finally got it straightened out I had the same number of songs but the days total had gone down to 84 days.+
    For all I know, there were duplicates on the exHDs and when you got that straightened out, the song count/time went down to what it should have been in the first place.

  • Account calculation vs. Entity currency total

    Hi all,
    I'm trying to solve one issue regarding an account calculation...
    This calculation is simple one and calculates the ratio between several accounts. Let's say C = A / B.
    I want this calculation to be run not only for <Entity currency>, but also for <Entity currency adjustments> and <Entity currency total>. For <Entity currency> and <Entity currency adjustments> it's working correctly and it calculates the ratio. But for <Entity currency total> it simply sums <Entity currency> and <Entity currency adjustments> which is not good.
    How to force HFM to use the calculation also for <Entity currency total> and override the default setting which just sums <Entity currency> and <Entity currency adjustments>?
    BR
    Vladino

    Hi,
    I have tried this but this is not working... See the printscreen: http://i52.tinypic.com/2hq8kdw.jpg
    Here is the rule:
    Sub Calculate()
    HS.EXP "A#c = A#a / A#b"
    End Sub
    Accounts "a" and "b" are set to revenue type, account "c" is balance type.
    Any other idea?
    Vladino

  • Can't get iPhone 5 calculator to include tax in total - Help!

    Bought my iPhone 5 July 25, 2013, but haven't had much time to learn how it works. About the calculator... On most calculators, if I enter 239.00 x .0825 = I get 19.7175. If I then enter + = I get the total including tax 258.7175. For some reason, I can't get the calculator on my iPhone 5 to do this. What do I need to do? Thanks!

    Thanks to all for your input and attempts to help.
    Hot Spur, I got an email where you said:  "How is the iOS calculator going to know the tax in any given area? It's different everywhere."  I already know the tax rate so I don't need the calculator to know  it.
    I was just hoping the standard iPhone 5 calculator would allow me to enter the sale price, multiply it by the tax rate, hit the equal sign to see the tax amount, hit the plus sign, then the equal sign, then see the total due, including tax. This is the order it typically prints out on a receipt, too.
    Sale Price
    x Tax Rate = Tax Amount
    + = Total Due, including tax
    I'll use my WalMart and Staples calculators until Apple comes up with a calculator that can do this. I had no idea it was so complicated...

  • How to set the payment Due date calculation as per working days.

    how to make changes in the vendor Payment terms so that while calculating the Payment due date as per Payment terms, system calculates it based on Working Days & not the Calender days ?
    Help is much appriciated.
    Thank you,
    Amit

    Hi,
    This is the standard settings by SAP, normally we couldn't change it to working day.
    Good luck
    Tao

  • Baseline date calculation excluding non-Business days

    Hi,
    We have a requirement to exclude non-Business days while calculating Baseline date.
    Currently the Baseline Date being populated in the Accounting Document is Document date + 5days(including Holidays).
    For eg
    Current Scenario
    Document Date: 12th Feb 2008
    Baseline date: 17th feb 2008.
    Requirement:
    Document Date: 12th Feb 2008
    Baseline date: 19th feb 2008(Excluding Sat & Sun).
    Request you to share useful info in this regard.
    Thanks
    Binu

    Hi,
    Use user exit RV60FUS5 for these base line date calculations.
    Thanks
    Krishna.

  • Add one day to java.sql.date

    I ask user to enter a date: yyyy-mm-dd, then I want to add one day to that date, I don't know how to do it?
    java.sql.Date time2 = java.sql.Date.valueOf( args[0] );
    Many Thanks.

    since a date object is really nothing more than a long you can always add one day's worth of milliseconds to it....
    long milliInADay = 1000 * 60 * 60 * 24;
    Date d = ......
    d.setTime(d.getTime() + milliInADay);although there may be some issues with this i'm unaware of, but seems pretty straightforward to me.

  • Calculating the number of days based on transaction in Query

    Hi friends,
    I would be greatful if u can help me out. I have got a situtation where in i need to take the day when the transaction has occured otherwise i dont need to take that day. For example if one transaction occurs on 1st , i need to take that day and if the transaction dosent occur then i need to ignore it. If there is one transaction or multiple transactions i need to take the day only once.
    Like transaction has happened on 1st Jan so it will be considered,
    on 2nd the transaction dosent happen so i need to ignore it
      on 3rd there is a transaction so its taken.
    So its like 1st - 1
                   2nd - 0
                   3rd - 1
    So total will be 2.
    Transactions are being taken from a cube. Kindly let me know as to how to do this in query.
    Thanks,
    Kapil

    PLease give us some more inputs.. requirement is not clear.
    Do u have 0calday in ur Cube?
    Khaja

  • Date calculations based on working days

    I need to add date calculations to a worksheet to show the time elapsed between two actions.
    Just a straightforward subtraction works fine, but what I really need to be able to do is calculate the number of working days between the two point (as something taking 5 days over Christmas with the 2 bank holidays is different equivalent to something taking 3 days in a normal week)
    At the moment I'm exporting the data into excel for analysis of this, but would really like to be able to do it in Disco so that the end users can go straight to the report using viewer, rather than having to do the conversion for them.
    Is this possible?
    Cheers

    Hi,
    Every thing is possible, the Q is how complicated is it....
    My suggestion is to create a table with all the dates of the non-working days for ex:
    create a table with all the MON-THURSDAYS, union to this table the holidays you know of such as christmas and so on.
    After getting this table you can create a function that returns the number of working days between 1 date to another by subtracting the days exists in this table.
    I started with that, you are more then welcome to get ahead with it and let us know what happened...
    create table holiday
    D_date date,
    d_day varchar2(20)
    create or replace procedure holiday_proc is
    d_date date;
    begin
    d_date := trunc(sysdate);
    while d_date<'01-jan-2010'
    loop
    if to_char(trunc(d_date),'Day') not in ('Saturday ','Sunday ') then
    INSERT INTO Holiday (d_date,d_day)
    (select trunc(d_date),to_char(trunc(d_date),'Day') from dual);
    end if;
    d_date := d_date+1;
    end loop;
    commit;
    end holiday_proc;

  • TCODE for report of a single day total transaction

    Dear all
    Please tell me a TCODE by which I can see the report which show all the transaction and total balance of a g/l for a single day
    Thanks and regards
    Tamal

    HI,
    Program         Description                                                    T Code
    RFEPOJ00     Line Item Journal                           S_ALR_87009828
    RFBNUM00     Gaps in Document Number Assignment     S_ALR_87009880                                                                               
    S_ALR_87101003 General Audit
    RFVBER00     List of  Update Terminations     S_ALR_87101004
    RFBELJ00     Compact Document Journal     S_ALR_87009826
    Please check and confirm.
    Thanks and Regards
    Binoj M D

  • Calculating months instead of days

    Dammit!  My client wasn't happy with me calculating 90 days from a given date field.
    He wants 3 months.
    So I thought I'd solved my problem, but I haven't.  Can anyone help me to calculate 3 months instead of 90 days.
    eg. If the 'from' date is 23/05/11, he wants the 'to' date to result in 23/08/11.  Which of course will not necessarily be 90 days.
    Thanks,
    Peta

    Oops !!1 Sorry Peta. Here is the link https://acrobat.com/#d=gxvf6sFTf5T1W7Yr6KpotA. But it's not full proof. It will just give you a rough idea.
    Thanks,
    Bibhu.

  • Calculating the depreciation pro days instead of periods

    Hi All,
    I've to calculate the depreciation pro days instead of periods.
    Is it possble? How to do it?
    Thanks
    G.Rossi

    Hi Rossi,
    you have to make a transfer the values to a asset with a daily calculating depreciation key. You can not change the depreciation key from normal depreciation to daily depreciation on a asset which has already values.
    If you are interested in technical details: If you create a asset ANLB-PERFY is set according to the depreciation key (T090NA-XDAILY). ANLB-PERFY can not be changed in the standard. If you change the depreciation key from normal depreciation to a daily calculating depreciation key (T090NA-XDAILY = initial to T090NA-XDAILY = X) you will get the AA 662.
    regards Bernhard

  • Calculation of number of Days?

    Hi Experts,
    i have two key figures one is calulate the number of day and the other is calulate the total number of stocks.
    So now my requirment is the user want to see the data like this,in report level.
    for Exp:
    if Number of days in between   10 -60 total stock is 100
    if Number of days in between    70- 80 total stock is 200
    if Number of days in between    81-130 total stock is 500.l  ike this.
    so how can i achieve this in query designer as i am using BI(7.0)?
    Regards,
    sat

    Hi ,
    Before posting a thread please provide the minimum info so that you can get the exact reply for your post.
    What are the data fileds that you have in your target to calculate the no of days?
    What is the source and IC on which you want to design the report?
    If u have any smaple data how you want the report to be? etc....
    If you have the date fields in your info provider then create 2 formula variable of type replacement path on both the date fileds.
    create a formula where use these two formula variables to get the difference of days.
    after that create one more formula and use
    ((diff of days>=10)AND(diff ofdays<=60)*Kf) will give you the stock value during that bucket.
    For more info plz refer to the below link which explains about the bucketing scenario.
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/30f15839-0cf1-2b10-c6a7-ebe68cc87cdc?quicklink=index&overridelayout=true
    Regards
    KP

Maybe you are looking for