查询查找信用和借记的总和

时间:2018-06-09 05:42:44

标签: sql sql-server tsql

我需要一个SQL查询来检索DATE范围内的SUM。

这是我的sudo代码:

 SELECT da.Debit,da.Credit,
    (SELECT SUM(daa.Debit) FROM Acc.DocumentAccount daa
    INNER JOIN Acc.Document dd ON dd.Id = daa.DocumentId
     WHERE dd.Date BETWEEN '2018-03-21' AND '2018-06-21' AND daa.Id=da.id) SumDebit
     FROM Acc.DocumentAccount da 
    INNER JOIN Acc.Document d ON d.Id = da.DocumentId
    WHERE d.Date BETWEEN '2018-06-21' AND '2019-03-21'

考虑以下示例数据:

CREATE TABLE Document(
    Id INT,
    [Date] date
);


INSERT INTO Document VALUES (1,'2018-01-01');
INSERT INTO Document VALUES (2,'2018-01-02');
INSERT INTO Document VALUES (3,'2018-07-01');
INSERT INTO Document VALUES (4,'2018-07-02');
CREATE TABLE DocumentAccount(
    DocumentId INT,
    Debit INT,
    Credit INT
    );

INSERT INTO DocumentAccount VALUES (1,100,200);
INSERT INTO DocumentAccount VALUES (2,150,300);
INSERT INTO DocumentAccount VALUES (3,150,300);
INSERT INTO DocumentAccount VALUES (4,160,310);
INSERT INTO DocumentAccount VALUES (4,170,320);

CREATE TABLE DocumentAccountDetail
(
    DocumentAccountId INT,
    DetailId INT 
)

我需要这个硬编码数据:

select * from 
(values (250, 500,0,0,480,0,0,0,930,0)) x(RemainDebit, RemainCredit,Q1Debit,Q2Debit,Q3Debit,Q4Debit,Q1Credit,Q2Credit,Q3Credit,Q4Credit)

RemainDebit是“2018-03-21”和“2018-06-21”之间debit的总和

RemainCreditcredit'2018-03-21'

之间'2018-06-21'的总和 在这种情况下,

Q1Debitdebit'2018-01-01'之间'2018-03-30'的总和,因为我的过滤范围是BETWEEN '2018-06-21''2019-03-21' Q1Debit是0

在这种情况下,

Q2Debitdebit'2018-04-01'之间'2018-06-30'的总和,因为我的过滤范围是BETWEEN '2018-06-21''2019-03-21' Q1Debit是0

在这种情况下,

Q3Debitdebit'2018-06-01'之间'2018-09-30'的总和,因为我的过滤范围是'2018-06-21''2019-03-21'之间我们在2018-07有3个文件,然后Q1Debit是150 + 160 + 170 = 480

等等......

5 个答案:

答案 0 :(得分:2)

我会选择以下

SELECT da.Debit,da.Credit,
(SELECT ISNULL(SUM(daa.Debit),0) FROM Acc.DocumentAccount daa
INNER JOIN Acc.Document dd ON dd.Id = daa.DocumentId
 WHERE dd.Date BETWEEN '2018-03-21' AND '2018-06-21' AND da.AccountId=daa.AccountId) SumDebit
 FROM Acc.DocumentAccount da 
INNER JOIN Acc.Document d ON d.Id = da.DocumentId
WHERE d.Date BETWEEN '2018-06-21' AND '2019-03-21'

答案 1 :(得分:2)

根据您的问题,您不需要select子句上的子查询,您可以在两个表中使用JOIN并在where子句上设置日期范围

Select 
   Sum(debit) as Totledebit,
   Sum(credit) as Totlecredit
From DocumentAccount da 
INNER JOIN Document d on da.DocumentId = d.id
WHERE d.[Date] BETWEEN '2018-03-21' AND '2019-03-21'

SQLFiddle

修改

我看到你编辑你的问题,认为有一些逻辑很奇怪。 但我相信,您可以尝试使用CASW WHEN表达式和Sum函数来实现您的期望。

Select 
   Sum(CASE WHEN d.[Date]  between '2018-01-01' AND '2018-06-21'  THEN debit ELSE 0 END) as Totledebit,
   Sum(CASE WHEN d.[Date]  BETWEEN '2018-01-01' AND '2018-06-21'  THEN credit ELSE 0 END) as Totlecredit,
   SUM(CASE WHEN d.[Date]  between '2018-01-01' AND '2018-03-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21' THEN debit ELSE 0 END) as Q1Debit,
   SUM(CASE WHEN d.[Date]  between '2018-04-01' AND '2018-06-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN debit ELSE 0 END) as Q2Debit,
   SUM(CASE WHEN d.[Date]  between '2018-07-01' AND '2018-09-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN debit ELSE 0 END) as Q3Debit,
   SUM(CASE WHEN d.[Date]  between '2018-10-01' AND '2018-12-31'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN debit ELSE 0 END) as Q4Debit,
   SUM(CASE WHEN d.[Date]  between '2018-01-01' AND '2018-03-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN Credit ELSE 0 END) as Q1Credit,
   SUM(CASE WHEN d.[Date]  between '2018-04-01' AND '2018-06-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN Credit ELSE 0 END) as Q2Credit,
   SUM(CASE WHEN d.[Date]  between '2018-07-01' AND '2018-09-30'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN Credit ELSE 0 END) as Q3Credit,
   SUM(CASE WHEN d.[Date]  between '2018-10-01' AND '2018-12-31'  AND  d.[Date]  BETWEEN '2018-06-21' AND '2019-03-21'  THEN Credit ELSE 0 END) as Q4Credit
From DocumentAccount da 
INNER JOIN Document d on da.DocumentId = d.id

sqlfiddle:https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=de775aaf8c5e03b25c346dca3bd80e21

CASE WHEN

答案 2 :(得分:1)

这里似乎需要一个支点,这里有一个想法如何计算:

为每个数据范围指定一个数字,所以

  • '2018-03-21'和'2018-06-21'之间的日期应标记为1
  • '2018-06-21'和'2019-03-21'之间的日期应标记为2
  • 等......

要实现此目的,您只需在表格上创建一个计算列。

然后你只需要在新列上使用group by并制作SUM。

以下是代码示例:

WITH temp_tbl (Debit, Credit, date_grp) AS (
SELECT da.Debit AS Debit,da.Credit AS Credit
    CASE d.Date 
    WHEN BETWEEN '2018-03-21' AND '2018-06-21' THEN 1
    WHEN BETWEEN '2018-06-21' AND '2019-03-21' THEN 2
    ELSE 0
    END AS date_grp
FROM Acc.DocumentAccount da 
INNER JOIN Acc.Document d ON d.Id = da.DocumentId)

SELECT SUM(Debit), SUM(Credit), date_grp
FROM temp_tbl
GROUP BY date_grp

希望有所帮助。

答案 3 :(得分:1)

这可能会指出你正确的方向......

Select 
   Sum(debit) as Dr,
   Sum (credit) as cr,
   Eomonth(datecolumn) as monthend
From table
    Group by eomonth (datecolumn)

如果您只想用Dr和cr输出一行,请删除代码中的第4行并过滤日期。

答案 4 :(得分:1)

你可以通过创建SQL函数来实现这一点,尽管另一个答案也非常可观。

将为Credit sum提供结果的函数:

CREATE FUNCTION CreditSum (@Dt1 Date ,@Dt2 Date)
AS
BEGIN 

DECLARE @Sum decimal(10,2)

SELECT @sum = SUM(Credit) FROM Acc.DocumentAccount da 
INNER JOIN Acc.Document d ON d.Id = da.DocumentId WHERE DT BETWEEN @Dt1 And @Dt2

RETURNS @Sum
END

将为借方总和提供结果的函数:

CREATE FUNCTION DebitSum (@Dt1 Date ,@Dt2 Date)
AS
BEGIN 

DECLARE @Sum decimal(10,2)

SELECT @sum = SUM(Debit) FROM Acc.DocumentAccount da 
INNER JOIN Acc.Document d ON d.Id = da.DocumentId WHERE DT BETWEEN @Dt1 And @Dt2

RETURNS @Sum
END

现在你可以随时

获得结果
SELECT DBO.CreditSum('2018-03-21','2018-06-21'),DBO.DebitSum('2018-03-21','2018-06-21'),DBO.CreditSum('2018-06-21',CAST(GETDATE() AS DATE)),DBO.DebitSum('2018-06-21',CAST(GETDATE() AS DATE))

希望它会有所帮助。