SQL grouping and summing impossible?

I want to create a sql query to summarize some data, but I'm starting to think that it is impossible with sql only. The data I have is of the following form:
TRAN_DT     TRAN_RS     DEBT     CRED
10-Jan     701     100     0
20-Jan     701     150     0
21-Jan     701     250     0
22-Jan     705     0     500
23-Jan     571     100     0
24-Jan     571     50     0
25-Jan     701     50     0
26-Jan     701     20     0
27-Jan     705     0     300
The data are classified by TRAN_DT and by TRAN_RS. THA grouping and summing data based on tran_rs, but only when it changes. If in the table above, I don't want to see all the first 3 records but only one value DEBT the sum of these 3 i.e. 100 + 150 + 250 = 500. If the table above after grouping would be similar to that below:
TRAN_DT     TRAN_RS     DEBT     CRED
21-Jan     701     500     0
22-Jan     705     0     500
24-Jan     571     150     0
26-Jan     701     70     0
27-Jan     705     0     300
The TRAN_DT is the last value of the record sum. I undestand that the tran_dt are not selectable. What I've tried so far is the following query:
select tran_dt,
         tran_rs,
         sum(debt)over(partition by tran_rs order by tran_dt rows unbounded preceding),
         sum(cred)over(partition by tran_rs order by tran_dt rows unbounded preceding) from that_table
Is it still possible with only sql, any thoughts?

The report that I am creating in BI Publisher.Maybe it is possible to group the data in the model and my question here?

The Re: tutorial method Tabibitosan by Aketi Jyuuzou me would be very useful here:

with sample_data as (select to_date('10/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 100 debt, 0 cred from dual union all
                     select to_date('20/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 150 debt, 0 cred from dual union all
                     select to_date('21/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 250 debt, 0 cred from dual union all
                     select to_date('22/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 500 cred from dual union all
                     select to_date('23/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 100 debt, 0 cred from dual union all
                     select to_date('24/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 50 debt, 0 cred from dual union all
                     select to_date('25/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 50 debt, 0 cred from dual union all
                     select to_date('26/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 20 debt, 0 cred from dual union all
                     select to_date('27/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 300 cred from dual),
     tabibitosan as (select tran_dt,
                            tran_rs,
                            debt,
                            cred,
                            dense_rank() over (order by tran_dt, tran_rs, rownum)
                              - dense_rank() over (partition by tran_rs order by tran_dt, rownum) grp
                     from   sample_data)
select max(tran_dt),
       tran_rs,
       sum(debt) debt,
       sum(cred) cred
from   tabibitosan
group by tran_rs,
         grp
order by 1, 2;

TRAN_DT       TRAN_RS       DEBT       CRED
---------- ---------- ---------- ----------
21/01/2012        701        500          0
22/01/2012        705          0        500
24/01/2012        571        150          0
26/01/2012        701         70          0
27/01/2012        705          0        300

ETA: I added the rownum in analytical functions to take account of the "encounters" where a tran_rs has multiple entries for the same tran_dt. If you have some other unique key to use (for example, the primary key of the table) then I would use instead.

Published by: Boneist on February 17, 2012 11:41

Tags: Database

Similar Questions

  • Problem with grouping and SUM

    .

    Hello

    Whenever you have a problem, post a small example of data (CREATE TABLE and INSERT, only relevant columns instructions) for all the tables involved and the results desired from these data.
    Explain, using specific examples, how you get these results from these data.

    Looks like you have a fan trap , where exist one-to-many relationships between tables, so that you are adding contains multiple copies of the same row of a table. If this is what is happening, then perform the GROUP BY and the AMOUNT on the table before joining. Without seeing some examples of data and the results desired from this data, I can't be sure if this is the problem, or any more tell you in detail how to fix it.

  • How to group multiple columns at the same level and sum one

    With the following XML data, I want to simulate the following query in the model. Exactly how can I accomplish this?

    Thank you
    Kurzweil4

    < pre >
    < G_MAIN >
    < LINE_CREATION_DATE > 17 December 09 < / LINE_CREATION_DATE >
    XYZ < ITEM > < / ITEM >
    HHOENT < PLANNER > < / PLANNER >
    < ITEM_TOT_PEND_QTY > 7000 < / ITEM_TOT_PEND_QTY >
    COMPANY ABC < client_name > < / client_name >
    < CATEGORY_SEGMENT1 > UU98 < / CATEGORY_SEGMENT1 >
    < OSTAND_QTY > 1000 < / OSTAND_QTY >
    SHIP_001 < LOCATION > < / LOCATION >
    JJK < SHIP_FROM_ORG > < / SHIP_FROM_ORG >
    < ITEM_TOTAL_OSTAND_QTY > 38000 < / ITEM_TOTAL_OSTAND_QTY >
    < / G_MAIN >
    < G_MAIN >
    < LINE_CREATION_DATE > 17 December 09 < / LINE_CREATION_DATE >
    XYZ < ITEM > < / ITEM >
    HHOENT < PLANNER > < / PLANNER >
    < ITEM_TOT_PEND_QTY > 7000 < / ITEM_TOT_PEND_QTY >
    COMPANY ABC < client_name > < / client_name >
    < CATEGORY_SEGMENT1 > UU98 < / CATEGORY_SEGMENT1 >
    < OSTAND_QTY > 1000 < / OSTAND_QTY >
    SHIP_001 < LOCATION > < / LOCATION >
    JJK < SHIP_FROM_ORG > < / SHIP_FROM_ORG >
    < ITEM_TOTAL_OSTAND_QTY > 38000 < / ITEM_TOTAL_OSTAND_QTY >
    < / G_MAIN >
    < / pre >

    < pre >
    SELECT
    LINE_CREATION_DATE,
    AGENDA,
    PLANNER,
    ITEM_TOT_PEND_QTY,
    CLIENT_NAME,
    CATEGORY_SEGMENT1,
    LOCATION,
    SHIP_FROM_ORG,
    ITEM_TOTAL_OSTAND_QTY,
    sum (OSTAND_QTY)
    Of
    table-name
    Group
    LINE_CREATION_DATE,
    AGENDA,
    PLANNER,
    ITEM_TOT_PEND_QTY,
    CLIENT_NAME,
    CATEGORY_SEGMENT1,
    LOCATION,
    SHIP_FROM_ORG,
    ITEM_TOTAL_OSTAND_QTY
    < / pre >

    http://winrichman.blogspot.com/search/label/grouping

    Certainly, we can group any level that you want

    for each group with data semi flattened

    Count and sum based grouping (could be linked to the crosstab)
    How to make a total in my model?
    Where is my Total issue

    
    
    
    

    It's a group on the Date of creation
    or

    
    
    
    

    It's a grouping on the POINT

    or

    
    
    
    

    It's a grouping on the POINT and PLANNER

  • latitude longitude coordinates SQL Server and the plot on the map of the brochure

    My question is basically:

    How to transform the data from SQL Server and map by unfolding?

    TIP: We run 11 Coldfusion, SQL Server 2000, flyer and JQuery if that helps somehow. If I need to download a javascript library, I can easily do.

    I ambulance and fire events in a database with the xy coordinates. My goal is to ask them about the news and place them on a map of the brochure. All our ambulances and fire trucks have gps in them and they broadcast at all times their location and status. Here's my query to get the latest data from gps for each truck;

    /*

    SQL Server 2000

    David Kulpanowski

    June 4, 2015

    Lee County EMS

    */

    SELECT

    DateTimeStamp

    RTRIM (CallSign) AS [Guide]

    RTRIM (UnitStatus) AS [UnitStatus]

    Latitude

    Longitude

    CONVERT (VARCHAR (15), DateTimeStamp, 106) AS [CurrentDate]

    CONVERT (VARCHAR (10), DateTimeStamp, 108) AS [CurrentTime]

    OF FireEMSGPSDatabase

    RIGHT OUTER JOIN

    (

    SELECT

    RTrim (CallSign) AS [Ambulance]

    MAX (DateTimeStamp) AS [MostRecentTime]

    OF HISTORY_201506

    WHERE RTRIM (CallSign) AS "LCM__".

    RTRIM GROUP (CallSign)

    )

    LIKE [MostRecent] on DateTimeStamp = MostRecent.MostRecentTime

    ORDER BY RTRIM (CallSign) CSA

    ;

    Shown below is an example of this query output;

    Call DateTimeStamp Latitude UnitStatus Longitude CurrentDate CurrentTime

    2015-06-04 17:14:17.357 LCM01 QA 26.56428 - 81.87044 June 4, 2015 17:14:17

    2015-06-04 17:14:17.357 LCM01 QA 26.56428 - 81.87044 June 4, 2015 17:14:17

    2015-06-04 17:14:18.670 LCM02 QA 26.64074 - 81.86507 June 4, 2015 17:14:18

    2015-06-04 17:14:34.420 LCM03 AR 26.64157 - 81.90973 June 4, 2015 17:14:34

    2015-06-04 17:14:20.420 LCM04 TA 26.63885 - 81.94159 June 4, 2015 17:14:20

    2015-06-04 17:14:21.297 LCM05 QA 26.4377 - 82.07806 June 4, 2015 17:14:21

    Below is my Coldfusion page with the flyer plan added in and the cfquery with cfoutput;

    <! DOCTYPE html >

    < html lang = "en" >

    < head >

    < meta http-equiv = "Content-Type" content = text/html"; Charset = UTF-8 "/ >"

    < meta name = "viewport" content = "width = device-width, original scale = 1.0" >

    " < link rel ="stylesheet"href =" http://CDN.leafletjs.com/leaflet-0.7/leaflet.CSS "/>

    " < script src =" http://CDN.leafletjs.com/leaflet-0.7.3/leaflet.js "> < / script > .

    < / head >

    < body >

    < cfquery datasource = "GPSDatabase" name = "AmbulanceLocation" >

    SELECT

    DateTimeStamp

    RTRIM (CallSign) AS [Guide]

    RTRIM (UnitStatus) AS [UnitStatus]

    Latitude

    Longitude

    CONVERT (VARCHAR (15), DateTimeStamp, 106) AS [CurrentDate]

    CONVERT (VARCHAR (10), DateTimeStamp, 108) AS [CurrentTime]

    OF FireEMSGPSDatabase

    RIGHT OUTER JOIN

    (

    SELECT

    RTrim (CallSign) AS [Ambulance]

    MAX (DateTimeStamp) AS [MostRecentTime]

    OF HISTORY_201506

    WHERE RTRIM (CallSign) AS "LCM__".

    RTRIM GROUP (CallSign)

    )

    LIKE [MostRecent] on DateTimeStamp = MostRecent.MostRecentTime

    ORDER BY RTRIM (CallSign) CSA

    < / cfquery >

    < cfoutput query = "AmbulanceLocation" >

    < table >

    < b >

    < td > #DateTimeStamp # < table >

    < td > #CallSign # < table >

    < td > #UnitStatus # < table >

    < td > #Latitude # < table >

    < td > #Longitude # < table >

    < td > #CurrentDate # < table >

    < td > #CurrentTime # < table >

    < /tr >

    < /table >

    < / cfoutput >

    < div id = ' map' > < / div >

    < script >

    var L.map = map ('plan' {Center: [26.5641867068354,-81.8704100173261], fullscreenControl: true, zoom: 12});

    L.tileLayer ('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);

    < /script >

    < / body >

    < / html >

    To sum it all up - I have a SQL Server query that retrieves the last my ambulances xy coordinates. I have a Coldfusion page that has a map of the brochure and the cfquery. My question is how to plot coordinates in a flyer plan. I can't understand the middle of the book to get the results of my query in terms of the brochure.

    In addition, it is a dynamic environment. Ambulances are constantly in motion. This page must be dynamic, so that the page can be updated to show the latest data.

    You must create the map and tile layer prior to place markers and then run your loop inside of the script tag.

    I highly recommend reading through the tutorials on the site of the brochure.  They go on these things in detail.

    Quick start - leaflet guide - a JavaScript library for the mobile environment maps

  • Consecutive date grouping and find the largest number of consecutive group

    Hi all

    I have given dates and I want to find the largest number of consecutive dates

    WIN_DATE_DATA

    2015-09-22

    2015-09-23

    2015-09-27

    2015-09-28

    2015-09-29

    2015-09-30

    2015-10-1


    In this example, there are 2 group of consecutive dates

    Group 1

    2015-09-22

    2015-09-23

    Group 2

    2015-09-27

    2015-09-28

    2015-09-29

    2015-09-30

    2015-10-1


    The OUTPUT should 5 which is the largest grouping of consecutive number.


    Thanks in advance for the help!




    Please take a look at the Community document: 101 PL/SQL: grouping sequence ranges (method Tabibitosan)

    will allow you to do.

  • The difference between the groups and roles?

    Hi all

    What is the difference between the groups and roles?

    Thanks for your time and your help.

    Oracle doesn't have anything called a 'group '.

    A role is a named object that can contain a set of privileges. All members may be individual privileges or may be another role that contains its own set of privileges. Roles can then be granted to users (or other roles) so that the users (or roles) have the specified privileges.

    See the SQL language reference - http://docs.oracle.com/cd/B28359_01/server.111/b28286/toc.htm

    Read the topics for CREATE ROLE, GRANT and REVOKE

  • Grouping and backreferences with regular expressions on the window to replace the text

    I'm really appreciate the inclusion of regular Expressions in the search and replace functionality. One thing miss me that East of backreferences in the replacement expression. For example, in unix tools vi or sed, I could do something like this:
     s/\(firstPart\) \(secondPart\) \(oldThirdPart\)/\2 \1 newThirdPart/g 
    that allow me to switch the places of first and secondPart and substitute totally thirdPart. If grouping and backreferences are already present in the window replace text, how do you properly call them?

    Published by: Justin.Warwick on August 23, 2011 08:26

    You can vote on the request for this to the exchange of SQL Developer, to add weight to the implementation as soon as possible: https://apex.oracle.com/pls/apex/f?p=43135:7:3693861354483465:NO:RP, 7:P7_ID:16761

    Kind regards
    K.

  • How to group and summarize the values with a vertical drop of 100?

    Dear gurus
    How to group and summarize the values with a vertical drop of 100 as the data indicated below:

    Cas_Cod_ Cas_Amt_ Description
    300 1000A
    301 200 B
    302 350 C
    400 500 A
    401 100 B
    402 25

    Now the data should be grouped like this
    A (300,400) = 1500
    (301,401) B = 300
    C (302,402) = 375

    Make sure that I don't have a column as description in my table to group data on description, its here just to give u an example.

    Hello

    using mod for hundreds:

    with x as
    (
      select 300 code, 1000 amt, 'A' descr from dual union all
      select 301 code, 200 amt, 'B' descr from dual union all
      select 302 code, 350 amt, 'C' descr from dual union all
      select 400 code, 500 amt, 'A' descr from dual union all
      select 401 code, 100 amt, 'B' descr from dual union all
      select 402 code, 25 amt, 'C' descr from dual
      )
       select mod(code,100) code, sum (amt)
        from x
       group by mod(code,100);
    

    Using the mod you get the rest of a division, so mod (300,100) = 0, mod (301,100) = 1, etc.

    Herald tiomela
    http://htendam.WordPress.com

  • Why Firefox keep save my last sites traveled as a group and then open this group when I start next time? It will not stop, and I get really mad

    Every time I close Firefox and I have a tab open, it records the tab as if I had recorded as a group and it opens the next time I start firefox again.

    You can check the startup parameter:

    • Tools > Options > general > startup: 'When Firefox starts': 'Show my home page' "show a blank page.
  • I followed some groups and contributors

    But did not get any updates by email all weekend.

    Change something?

    Thank you!

    Holly

    Make sure that you always follow these groups and that it contributes.  Over the years, I found that from time to time the forum destroyed my following, and I need to re - follow the things that I'm interested.

    I don't know if that's a problem, or if there is a way to ensure that the user is always really interested in getting these emails, and if they aren't, it will reduce the number of servers forum need to send emails.  TOTAL speculation on my part, but it happened to me several times since 2005 when I began to participate in the forums.

  • Hello, I have a mini ipad 1st gen and his watch do not update to ios 9.2 and says "impossible to search for software updates".

    Hello, I have a mini ipad 1st gen and his watch do not update to ios 9.2 and says "impossible to search for software updates".

    All solutions?  Thank you

    sSimply reboot your device. If it still doesn't work do not I wanted to wait awhile

    Redfox49 if you need more support please do not hesitate to contact me!<3>

  • Added properties (Group and channel) are not a numeric data type.

    I use a use to add two groups and channel custom properties. The properties are text and digital, double and whole.

    When I create a request in the browser, I selects the custom using the drop-down list property, but has only the drop operator "=" or "<>". "" The property is displayed in the data portal is a number of float. I had this problem when I was adding properties custom by using a script. I then tried "Navigatorinstallation / my Index DataFinder/Reset/Reset", then repopulated my search box, using the use on my raw data files. At this point, all the properties of my have only the "=" or "<>" choice of operator in the query.

    It seems that my properties are digital in the data portal, but the the query string of the browser.

    Thanks in advance,

    Hi Bill,

    You don't have to delete the files.  Choose the NAVIGATOR menu "settings > My DataFinder > reset... '. "can clilck on the UPPER part of the two buttons, one called"reset the index.  Which will remove all records in database and re-index all of your data files.  If you one of these properties have already optimized, set their State not optimized before resetting the index, otherwise the optimized property data types will persist.

    If this does not help, then the problem could be in the use that you use.

    Brad Turpin

    Tiara Product Support Engineer

    National Instruments

  • How do you find the name of the Group and channel your TDMS file name?

    How do you find the name of the Group and channel your TDMS file name?

    Nevermind, I've used the file viewer.

  • Major difference between the Group and the organizational unit?

    Pls explain me what is the difference between the groups and the organization unit in simple terms with an example in real time.

    Hello

    https://en.Wikipedia.org/wiki/Organizational_unit _ (computing)

    https://en.Wikipedia.org/wiki/Group _ (computing)

    See you soon.

  • Error Code 0 x 080070570 Windows updates and 0x800A01AE impossible?

    Error Code 0 x 080070570 Windows updates and 0x800A01AE impossible? Regds, 'a journey of a thousand miles begins with a first step."

    Code 0x800A01AE error and 0 x 080070570

    Stop the Background Intelligent Transfer Service and the automatic updates service. To do this, follow these steps:
    Click Start, run, type cmd, and then click OK.
    At the command prompt, type the following commands and press ENTER after each command:

    net stop wuauserv
    net stop bits
    output
    Rename the Catroot2 folder.

    Note do not follow this step if you are using Microsoft Windows 2000.

    To rename the Catroot2 folder, follow these steps:
    Click Start, run, type cmd, and then click OK.
    At the command prompt, type the following commands and press ENTER after each command:

    net stop cryptsvc
    Ren %systemroot%\System32\Catroot2 oldcatroot2
    net start cryptsvc
    output
    Important do not rename the folder C:\windows\system32\catroot. The Catroot2 folder is automatically recreated by Windows, but the C:\windows\system32\catroot folder is not recreated if is renamed the folder C:\windows\system32\catroot.
    Rename the folder "C:\Windows\SoftwareDistribution":
    Click Start, click Run, type %any, and then click OK.
    Right-click the C:\Windows\SoftwareDistribution folder, and then click Rename.
    Type SoftwareDistribution.old, and then press ENTER to rename this folder.

    Save the files used by Windows Update and Microsoft Update. To do this, follow these steps:
    Click Start, run, type cmd, and then click OK.
    At the command prompt, type the following commands, press ENTER after each command, and then click OK every time that you receive a verification message:
    regsvr32 c:\windows\system32\wuapi.dll
    regsvr32 wuaueng1.dll
    regsvr32 c:\windows\system32\wuaueng.dll
    regsvr32 c:\windows\system32\dllcache\wucltui.dll
    regsvr32 wups2.dll
    regsvr32 c:\windows\system32\wups.dll
    regsvr32 wuweb.dll

    Start the service of automatic updates, Background Intelligent Transfer and the event log service. To do this, follow these steps:
    Click Start, run, type cmd, and then click OK.
    At the command prompt, type the following commands and press ENTER after each command:
    net start bits
    net start wuauserv
    net start Eventlog
    output

    Luck!

    Kind regards
    Sohail Patel

    Regds, 'a journey of a thousand miles begins with a first step."

Maybe you are looking for