sql server运行总计,带有借方贷方余额的条件

时间:2015-05-21 05:26:07

标签: sql sql-server

我想在下表中根据借方和贷方进行总计金额运行,其中有一列drcr,其中' c'代表信用和' d'借记 它也应该用分组名称来完成。

我希望使用sql server query跟随输出

    Name  amt drcr Total
    | 'abc'| 1| 'c'|1 |
    | 'abc'| 5| 'd'|-4|
    | 'abc'| 2| 'c'|-2|
    | 'abc'| 1| 'c'|-1|
    | 'bcd'| 1| 'c'|15|
    | 'bcd'| 1| 'd'|0 |
    | 'bcd'| 5| 'c'|5 |
    | 'bcd'| 8| 'd'|-3|
    | 'bcd'| 0| 'c'|-3|

来自以下输入

CREATE TABLE #temp
(
  intId INT IDENTITY(1, 1) ,
  varName VARCHAR(50) ,
  decAmt DECIMAL(28, 0) ,
  charCrDr CHAR(1) ,  -- c cr, d dr
  decTotal DECIMAL(28, 0)
)
INSERT  INTO #temp
    ( varName, decAmt, charCrDr )
VALUES  ( 'abc', 1, 'c' ),
    ( 'abc', 5, 'd' ),
    ( 'abc', 2, 'c' ),
    ( 'abc', 1, 'c' ),
    ( 'bcd', 15, 'c' ),
    ( 'bcd', 15, 'd' ),
    ( 'bcd', 5, 'c' ),
    ( 'bcd', 8, 'd' ),
    ( 'bcd', 0, 'c' )

我使用以下解决方案解决了它

SELECT  *
INTO    #temp1
FROM    #temp AS T

UPDATE  #temp1
SET     #temp1.intAmt = CAST(-#temp1.decAmt AS DECIMAL)
WHERE   charCrDr = 'd'

SELECT  T.intId ,
    T.varName ,
    T.charCrDr ,
    T.decAmt ,
    SUM(T2.decAmt) AS balance
FROM    #temp1 AS T
    INNER JOIN #temp1 AS T2 ON T2.varName = T.varName
                               AND T2.intId <= T.intId
GROUP BY T.intId ,
    T.varName ,
    T.charCrDr ,
    T.decAmt

但我想知道最好的方法

谢谢你

3 个答案:

答案 0 :(得分:3)

试试这个:(改进

SELECT intId, varName, decAmt, charCrDr, 
       SUM(CASE WHEN charCrDr = 'c' THEN
               decAmt
           ELSE
              decAmt * -1
           END )
           OVER (PARTITION BY varName ORDER BY intId) As decTotal
FROM #Temp;

see fiddle here

说明:

  • CASE将决定是否将decAmt作为正数或负数添加到SUM
  • PARTITION BY表示将为每个varName单独计算总和(在另一个答案中缺失)
  • ORDER BY将确保正确计算金额

这适用于任何受支持的Sql Server版本(2005及以上版本)。

答案 1 :(得分:2)

HI Navnit这很简单你在语句中使用'Sum(在charCrDr ='c'然后decAmt else -decAmt结束时的情况)结束(由intId命令)`。

select *,Sum( case when charCrDr = 'c' Then decAmt else -1*decAmt end) over( PARTITION BY varName ORDER BY intId) as Total
     from #temp t

答案 2 :(得分:0)

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<%@ page import="java.util.Calendar" %>
<%
//java Code
String date = (new java.util.Date()).toString();
String UserName = request.getParameter("UserName");
String CusId= request.getParameter("CusId");
String AccountNo = request.getParameter("AccountNo");
String Debit = request.getParameter("Debit");
String Credit=request.getParameter("Credit");
String Balance=request.getParameter("Balance");
String sDate=request.getParameter("sDate");
try
{
String s="jdbc:odbc:Database1";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(s);
Statement smt=conn.createStatement();
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
String sql= "SELECT  sDate, (SUM(Debit)*-1) + SUM(Credit)Balance FROM abcbank GROUP BY  sDate";
ResultSet result = smt.executeQuery(sql);
int count = 0;
while (result.next())
{
result.getString(1,"Debit");
result.getString(2,"Credit");
result.getString(3,"Balance");
result.getDate(4,startDate);

}

String update= "UPDATE abcbank SET Balance =Debit + Balance   WHERE AccountNo="+AccountNo+" ";
PreparedStatement statement = conn.prepareStatement(update);
//statement.setString(1,"AccountNo");
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated ==0)
{
out.println("This Emp does not Exists!");
}
else if(rowsUpdated ==1)
{
out.println("An existing user was updated successfully!");
}
conn.close();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
  }

%>
</body>
</html>