Home > database >  For grouping SQL query, the cumulative sum problem
For grouping SQL query, the cumulative sum problem

Time:09-29


According to the nature of capital coding, coding group projects to year, month, unit budget coding for query input in the condition of the accumulative amount
? For example query conditions for: month=2017, year=2, unit budget code=318010
The query result set is
2017,2,318010,01,01,30212,4000
2017,2,318010,01,02,30215,1600
2017,2,318010,01,03,30217,2800

Like this
Excuse me each great god SQL should be how to write? Thank you very much!

CodePudding user response:

.
This is the most basic
Select a, b, c, the sum (d), sum (e)
From the table name
Where a=? And b=? And c=?
Group by a, b, c;
Statement!

CodePudding user response:

The select years | | month, sum () for the month amount from group by years | | month | | budget coding

Look at your table can be year, month, budget Numbers joining together into a grouping processing field.

CodePudding user response:

Select year,
t.T. in
T. capital character encoding,
T. project coding,
T. economic classification and coding,
T. monthly amount
From the table t
In the where year=2017 and t. t.=2 and t. unit budget code=318010
Group by t. year,
T. in
T. capital character encoding,
T. project coding,
T. economic classification and coding

CodePudding user response:

 
SQL>
SQL> Create table test (
2 divid varchar (10),
3 m varchar (10),
4 AcctCode varchar (10),
Five planet econ varchar (10),
6 ApprMoney int
7);
The Table created
SQL> The begin
2 insert into the test values (318001, 1, '2190101', '30101', 500);
3 the insert into the test values (318001, 1, '2190101', '30102', 500);
4 the insert into the test values (318001, 1, '2190101', '30103', 500);
5 the insert into the test values (318001, 2, '2190101', '30101', 400);
6 insert into the test values (318001, 2, '2190101', '30102', 500);
7 the insert into the test values (318001, 2, '2190101', '30103', 600);
8 the end;
9/
PL/SQL procedure successfully completed
SQL> - the method 1
SQL> As with m (
The select divid, 2 m, AcctCode, planet econ,
3 the sum (ApprMoney) over (partition by divid, AcctCode, planet econ order by m) xx
4 the from test
5)
The select * from 6 m where divid='318001' and m='2'.
DIVID M ACCTCODE planet ECON XX
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
318001 2 2190101 30101 900
318001 2 2190101 30102 1000
318001 2 2190101 30103 1100
SQL> - the method 2
SQL> The select divid, m, AcctCode, planet econ,
2 (select sum (ApprMoney) from the test
3 where divid=t.d ivid and m & lt;=t.m and AcctCode=t.A cctCode and planet econ=t.e con
4) x
5 the from the test t
6 where divid='318001' and m='2'.
DIVID M ACCTCODE planet ECON X
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
318001 2 2190101 30101 900
318001 2 2190101 30102 1000
318001 2 2190101 30103 1100
SQL> Drop table test purge;
Table dropped

SQL>

CodePudding user response:

Visual inspection is financial industry,

CodePudding user response:

On a wrong
. Select t1. Annual, t1, t1. The unit budget coding, t1. Capital character encoding, t1. Project code, t1. Economic coding, t2. Monthly amount from table t1 left join
(select t. project coding,
T. economic classification and coding,
The sum (t) for the month amount as the month amount
From the table t
In the where year=2017 and t. t. & lt;=2 and t. unit budget code=318010
Group by t. project coding,
T. economic classification and coding) t2 on t1. Project code=t2. The program coding and t1. The economy classification and coding=t2. The classification and coding
Where t1) year=2017 and t1)=2 month and t1. The unit budget code=318010

CodePudding user response:

Is this
 
The SELECT nd, yf, dwysbm ZJXZBM, XMBM, JJFLBM, ljje
The FROM (SELECT t. *, SUM (dyfse) OVER (PARTITION BY ZJXZBM, XMBM) ljje
The FROM t_zj t
WHERE nd='2017' AND dwysbm='318010' AND yf & lt;=2) WHERE yf=2


  • Related