我在LMS平台上对练习进行评分有问题。
我们有两个查询:
select
distinct c.city_name,'city' obj_type
from
shipping.city c
union all
select
distinct c.state,'state' obj_type
from
shipping.city c
union all
select
distinct d.first_name,'driver' obj_type
from
shipping.driver d
union all
select
distinct t.Make,'truck' obj_type
from
shipping.truck t
order by 1 desc
这:
select
c.city_name,'city' obj_type
from
shipping.city c
union
select
c.state,'state' obj_type
from
shipping.city c
union
select
d.first_name,'driver' obj_type
from
shipping.driver d
union
select
t.Make,'truck' obj_type
from
shipping.truck t
order by 1 desc
两者都能获得相同的结果,但第二点在平台上没有得分。同时,我在Metabase中使用除外检查结果,它使我“无结果!” (空)
platfrom的工作原理:
它发送2个查询作为基础,并检查学生和参考答案之间的差异。
我认为在Metabase中,“联合”和“全部联合”的数据可能参差不齐,但是 比较,除了什么也没给我。 谢谢!
UPD:解决问题。在两个查询中有不同的排序,也从platfrom检查,查询也不同。谢谢大家!
答案 0 :(得分:0)
如果同一表中的行具有相同的值,或者NULL
或city_name
或state
中的first_name
make
的结果可能有所不同。
编辑:尝试使用EXCEPT ALL
而不是EXCEPT
检查结果,否则您将看不到所有差异。
看看这个例子:
DECLARE @T1 TABLE (name VARCHAR(10))
DECLARE @T2 TABLE (surname VARCHAR(10))
DECLARE @R1 TABLE (descr VARCHAR(10), typ VARCHAR(10))
DECLARE @R2 TABLE (descr VARCHAR(10), typ VARCHAR(10))
INSERT INTO @T1
SELECT *
FROM( VALUES ('Abigail'), ('Luke'), ('Mat'), ('Mat'), ('Zoe')) x (d)
INSERT INTO @T2
SELECT *
FROM( VALUES ('Brown'), ('Doe'), ('Doe'), ('Smith'), ('Wilkinson')) x (d)
insert into @r1
select name descr, 'name' descr_type from @t1
union
select surname descr, 'surname' descr_type from @t2
insert into @r2
select name descr, 'name' descr_type from @t1
union all
select surname descr, 'surname' descr_type from @t2
select * from @r1
select * from @r2
如果检查结果:
select * from @r1
except
select * from @r2
将产生“没有结果!” (空)
select * from @r1
except all
select * from @r2
会产生以下结果:
descr typ
'Doe' 'surname'
'Mat' 'name'
如果您想查看差异详细信息:
select *
from
(select ROW_NUMBER() over (partition by descr order by descr) n, * from @r1) r1
full join
(select ROW_NUMBER() over (partition by descr order by descr) n, * from @r2) r2
on r1.n= r2.n and r1.descr = r2.descr
答案 1 :(得分:0)
问题是,由于{city,state,driver,truck}文字的常量不同,UNION的三个腿总是不同的。因此,将没有重复项,并且UNION
的结果与UNION ALL
相同。
示例:
-- some data ...
CREATE TABLE one
( num integer not null
, nam text
);
INSERT INTO one ( num , nam ) values (1,'one'), (2, 'two'), (3, 'three');
-- QUERY#1
SELECT 'from_first' AS "origin"
, num, nam
FROM one
UNION
SELECT 'from_second' AS "origin"
, num, nam
FROM one
ORDER BY 1,2
;
-- QUERY#2
SELECT 'from_first' AS "origin"
, num, nam
FROM one
UNION ALL
SELECT 'from_second' AS "origin"
, num, nam
FROM one
ORDER BY 1,2
;
结果:
CREATE TABLE
INSERT 0 3
origin | num | nam
-------------+-----+-------
from_first | 1 | one
from_first | 2 | two
from_first | 3 | three
from_second | 1 | one
from_second | 2 | two
from_second | 3 | three
(6 rows)
origin | num | nam
-------------+-----+-------
from_first | 1 | one
from_first | 2 | two
from_first | 3 | three
from_second | 1 | one
from_second | 2 | two
from_second | 3 | three
(6 rows)
当一个或两个子选择本身产生重复项时,情况就不同了,如:
-- some more data ...
CREATE TABLE two
( num integer not null
, nam text
);
INSERT INTO two ( num , nam ) values (1,'one'), (2, 'two'), (2, 'two');
-- QUERY#1a
SELECT 'from_first' AS "origin"
, num, nam
FROM two
UNION
SELECT 'from_second' AS "origin"
, num, nam
FROM two
ORDER BY 1,2
;
-- QUERY#2b
SELECT 'from_first' AS "origin"
, num, nam
FROM two
UNION ALL
SELECT 'from_second' AS "origin"
, num, nam
FROM two
ORDER BY 1,2
;
结果2:
CREATE TABLE
INSERT 0 3
origin | num | nam
-------------+-----+-----
from_first | 1 | one
from_first | 2 | two
from_second | 1 | one
from_second | 2 | two
(4 rows)
origin | num | nam
-------------+-----+-----
from_first | 1 | one
from_first | 2 | two
from_first | 2 | two
from_second | 1 | one
from_second | 2 | two
from_second | 2 | two
(6 rows)