SQL GROUP BY 子句不会生成不同的行
我有两个表:
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 子句,这也会产生相同的输出!我可以观察到的一件事是,表 t2 中 type 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在对
类型、obal
的不同组合进行GROUP
。如果您仅按
type
进行分组,那么每种类型都不会出现重复的条目,但您需要决定如何处理obal
字段。You are
GROUP
ing on distinct combinations oftype, 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 theobal
field.您有两个分组列:....
数据库通过两个值的不同组合对值进行分组!
You have two grouping columns: ....
The database groups the values by distinct combinations of both values!
问题是您不仅按
type
分组,还按obal
分组。尝试:
The problem is that you're not grouping just by
type
, but also byobal
.Try:
从 group by 子句中删除 t2.obal。
remove t2.obal from your group by clause.