SQL GROUP BY 子句不会生成不同的行

发布于 2024-12-27 05:57:28 字数 2139 浏览 1 评论 0原文

我有两个表:

  • t1日期 d,姓名 c(20),借方 n(7),贷方 n(7)
  • t2日期 d,名称 c(20),类型 c(25),obal n(7)

这是示例数据。

T1:

DATE        NAME        DEBIT   CREDIT
01.01.12    dad                 5000
01.01.12    mum                 6000
05.01.12    school      1000    
01.02.12    dad                 5000
01.02.12    mum                 6000
10.02.12    tuition     300 
10.02.12    snacks      100 
01.03.12    dad                 5000
01.03.12    mum                 6000
01.03.12    books       500 
02.03.12    rice        500 
02.03.12    vegetables  900 
03.03.12    snacks      100 
01.04.12    dad                 5000
01.04.12    mum                 6000
01.04.12    meat        200 
21.04.12    guest       800 

T2:

DATE        NAME        TYPE        OBAL
01.01.12    dr fee      medical     8000
01.01.12    medicine    medical     -10000
01.01.12    dad         income      400000
01.01.12    mum         income      450000
05.01.12    school      education   0
10.02.12    tuition     education   0
10.02.12    snacks      misc        0
01.03.12    books       education   0
02.03.12    rice        food        0
02.03.12    vegetables  food        0
01.04.12    meat        food        0
21.04.12    guest       misc        0

预计按类型 (t2) 分组的每个名称的(obal + 贷方 - 借方)总和。使用的查询和获得的输出是:

SELECT 
    t2.type, obal + SUM(NVL(credit - debit, 0)) as bal 
FROM t2 
LEFT JOIN t1 ON t2.name = t1.name 
GROUP BY 
    t2.type, t2.obal

结果:

TYPE        BAL
education   -1800
food        -1600
income      420000
income      474000
medical     -10000
medical     8000
misc        -1000

我的问题是:为什么 类型 收入医疗 在查询输出中出现两次,尽管正在使用 GROUP BY 子句?我尝试在列 t2.type 之前添加 DISTINCT 子句,这也会产生相同的输出!我可以观察到的一件事是,表 t2type medical 的两条记录在 t1 中没有匹配的记录相同的名称和两条类型的记录收入t2obal强>。请帮助我克服这个问题。

I have two tables:

  • t1 (date d, name c(20), debit n(7), credit n(7))
  • t2 (date d, name c(20), type c(25), obal n(7))

Here's the sample data.

T1:

DATE        NAME        DEBIT   CREDIT
01.01.12    dad                 5000
01.01.12    mum                 6000
05.01.12    school      1000    
01.02.12    dad                 5000
01.02.12    mum                 6000
10.02.12    tuition     300 
10.02.12    snacks      100 
01.03.12    dad                 5000
01.03.12    mum                 6000
01.03.12    books       500 
02.03.12    rice        500 
02.03.12    vegetables  900 
03.03.12    snacks      100 
01.04.12    dad                 5000
01.04.12    mum                 6000
01.04.12    meat        200 
21.04.12    guest       800 

T2:

DATE        NAME        TYPE        OBAL
01.01.12    dr fee      medical     8000
01.01.12    medicine    medical     -10000
01.01.12    dad         income      400000
01.01.12    mum         income      450000
05.01.12    school      education   0
10.02.12    tuition     education   0
10.02.12    snacks      misc        0
01.03.12    books       education   0
02.03.12    rice        food        0
02.03.12    vegetables  food        0
01.04.12    meat        food        0
21.04.12    guest       misc        0

Sum of (obal + credit - debit) for each name grouped by type (t2) is expected. Query used and output obtained is:

SELECT 
    t2.type, obal + SUM(NVL(credit - debit, 0)) as bal 
FROM t2 
LEFT JOIN t1 ON t2.name = t1.name 
GROUP BY 
    t2.type, t2.obal

Results:

TYPE        BAL
education   -1800
food        -1600
income      420000
income      474000
medical     -10000
medical     8000
misc        -1000

My question is: why should the type income and medical comes twice in the query output in spite of GROUP BY clause being used? I've tried adding DISTINCT clause before the column t2.type and that too produces the same output! One thing I could observe is that the two records of type medical in table t2 has no records in t1 matching the same name and two records of type income has a non-zero obal in t2. Please help me to overcome this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末的微笑 2025-01-03 05:57:28

您正在对类型、obal的不同组合进行GROUP

如果您仅按 type 进行分组,那么每种类型都不会出现重复的条目,但您需要决定如何处理 obal 字段。

You are GROUPing on distinct combinations of type, obal.

If you only group on type then you would not have duplicate entries for each type, but you will need to decide how to handle the obal field.

不必了 2025-01-03 05:57:28

您有两个分组列:....

GROUP BY t2.type, t2.obal

数据库通过两个值的不同组合对值进行分组!

You have two grouping columns: ....

GROUP BY t2.type, t2.obal

The database groups the values by distinct combinations of both values!

懷念過去 2025-01-03 05:57:28

问题是您不仅按 type 分组,还按 obal 分组。

尝试:

select type,sum(bal) as bal
from(
  SELECT t2.type, obal + SUM(NVL(credit - debit, 0)) as bal FROM t2 LEFT JOIN t1 ;
  ON t2.name = t1.name GROUP BY t2.type, t2.obal
)a
group by type;

The problem is that you're not grouping just by type, but also by obal.

Try:

select type,sum(bal) as bal
from(
  SELECT t2.type, obal + SUM(NVL(credit - debit, 0)) as bal FROM t2 LEFT JOIN t1 ;
  ON t2.name = t1.name GROUP BY t2.type, t2.obal
)a
group by type;
昵称有卵用 2025-01-03 05:57:28

从 group by 子句中删除 t2.obal。

SELECT t2.type, SUM(NVL(credit - debit, 0), obal) as bal 
FROM t2 LEFT JOIN t1 ON t2.name = t1.name 
GROUP BY t2.type

remove t2.obal from your group by clause.

SELECT t2.type, SUM(NVL(credit - debit, 0), obal) as bal 
FROM t2 LEFT JOIN t1 ON t2.name = t1.name 
GROUP BY t2.type
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文