同一列的两个和,而分组条件为两个的不同

时间:2018-11-25 06:53:20

标签: sql sql-server tsql sum

我要查询的合并试算表余额有两个不同的期间,一个作为期初余额,另一个作为当前期间。

我有一个名为JDT1的表,如下所示:

JDT1 Table

我想用与期初余额和经常余额有关的两个不同条件来获得借方和贷方的帐户和总和。我的查询是

Select Account, ProfitCode, FormatCode, Acctname, (Select sum(Debit) as  
OBDebit From JDT1 Where RefDate < {?From}),  (Select sum(Credit) as 
OBCredit From JDT1 Where RefDate < {?From}), (Select sum(Debit) as OBDebit 
From JDT1 Where RefDate >= {?From} and Refdate <= {?To}),  
(Select sum(Credit) as OBCredit From JDT1 Where RefDate >= {?From} 
 and  Refdate <= {?To}) From JDT1 Full outer join [OACT] on [JDT1].[Account]
= [OACT].[AcctCode] Full outer join [OPRC] on [JDT1].[ProfitCode] =
  [OPRC].[PrcCode] Group by Account, ProfitCode

但是不幸的是,总和由于不受影响而变成一个组一样的数量。

1 个答案:

答案 0 :(得分:0)

您遇到了一些问题。

  1. 进行分组时,选择列表中的任何项目都必须在group by子句中。
  2. 您需要过滤总和以匹配分组依据。
from pyspark.ml.linalg import Vectors
from pyspark.ml.classification import MultilayerPerceptronClassifier, MultilayerPerceptronClassificationModel
from pyspark.sql import Row