[英]With Clause using a IF Clause in Oracle SQL possible?
I have a table with values:我有一个包含值的表:
CREATE TABLE school (
classroom varchar(125),
girls int,
boys int,
sum_class int
);
INSERT INTO school
(classroom, girls, boys, sum_class)
values('1a',4,10,14);
INSERT INTO school
(classroom, girls, boys, sum_class)
values('1b',11,19,30);
INSERT INTO school
(classroom, girls, boys, sum_class)
values('2a',12,13,25);
INSERT INTO school
(classroom, girls, boys, sum_class)
values('2b',10,9,19);
At a later point the table school is filled with further entries, automatically (I don't write the exact command for the creation of the table; it should be solved in the SQL query).稍后,表 school 会自动填充更多条目(我没有编写创建表的确切命令;它应该在 SQL 查询中解决)。 Because the entry of the classroom is known but not filled in the table at the moment, I write the following SQL query:因为教室的入口是已知的,但暂时没有填写在表中,所以我写了以下 SQL 查询:
With exact_class AS (
SELECT '2c' AS classroom, 0 AS girls, 0 AS boys, 0 AS sum_class FROM dual
UNION
SELECT '2d' AS classroom, 0 AS girls, 0 AS boys, 0 AS sum_class FROM dual
)
SELECT classroom, girls, boys, sum_class
FROM school
UNION
SELECT * FROM exact_class
For bridging this query is helpful until the new values of the classroom '2c' and/or '2d' is/are coming.在教室“2c”和/或“2d”的新值出现之前,桥接此查询很有帮助。 For example:例如:
(classroom, girls, boys, sum_class)
values('2c',6,14,20);
Now, the SQL query show me the classroom '2c' two times in the table (next to the other values):现在,SQL 查询在表中两次显示教室“2c”(在其他值旁边):
'2c',6,14,20
'2c',0,0,0
Of course, I need only the correct row and not both of them and here begins my problem:当然,我只需要正确的行,而不是他们两个,这里开始我的问题:
Is it possible to switch in the SQL query with an IF-Clause?是否可以使用 IF 子句切换 SQL 查询? In one case it shows me the 0-values, when there is no entry.在一种情况下,当没有条目时,它会向我显示 0 值。 In the other case it shows the new values of the classroom.在另一种情况下,它显示了课堂的新价值。
Of course, I tried it intuitively with an IF-Clause but I got errors.当然,我用 IF 子句直观地尝试了它,但我得到了错误。 However, I think the syntax also doesn't make sense.但是,我认为语法也没有意义。
Is there a 'simple' solution for this SQL query?这个 SQL 查询是否有一个“简单”的解决方案? Or must I write the query in another way to get a solution?或者我必须以另一种方式编写查询以获得解决方案吗? If yes, how?如果是,如何?
Thanks in advance.提前致谢。
One way would be to use UNION ALL
with the table and the default values and then to aggregate:一种方法是将UNION ALL
与表和默认值一起使用,然后进行聚合:
SELECT classroom,
MAX(girls) AS girls,
MAX(boys) AS boys,
MAX(sum_class) AS sum_class
FROM (
SELECT classroom, girls, boys, sum_class FROM exact_class UNION ALL
SELECT '2c', 0, 0, 0 FROM dual UNION ALL
SELECT '2d', 0, 0, 0 FROM dual
)
GROUP BY classroom
You can just put a condition on the unioned query.您可以只对联合查询设置条件。
SELECT
CLASSROOM, GIRLS, BOYS, SUM_CLASS
FROM
SCHOOL
UNION ALL
Select
CLASSROOM, GIRLS, BOYS, SUM_CLASS
From
(SELECT * FROM exact_class) ec
Where NOT EXISTS (Select * From SCHOOL Where CLASSROOM = ec.CLASSROOM And SUM_CLASS > 0)
The NOT EXISTS query is checking if there is a row for the same classroom with some boys/girls - if there is no such row then the one with zeros will be created. NOT EXISTS 查询正在检查同一教室是否有一些男孩/女孩的行 - 如果没有这样的行,那么将创建带有零的行。 If such row exists then it was already selected by the first query and the one with zeros will not be.如果存在这样的行,那么它已经被第一个查询选中,而带有零的行将不会被选中。
/* Result with initial data
CLASSROOM GIRLS BOYS SUM_CLASS
----------- ---------- ---------- ----------
1a 4 10 14
1b 11 19 30
2a 12 13 25
2b 10 9 19
2c 0 0 0
2d 0 0 0
*/
/* Result after inserted row for 2c (6, 14, 20)
CLASSROOM GIRLS BOYS SUM_CLASS
----------- ---------- ---------- ----------
1a 4 10 14
1b 11 19 30
2a 12 13 25
2b 10 9 19
2c 6 14 20
2d 0 0 0
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.