sql - SQL Merge 来自同一个 SQL 表的结果
问题描述
我是 sql 新手,并试图将结果合并到 sql 查询的不同行的同一列中,如下所示。我该怎么做?
(select distinct szdoc
from proddta.f47047 (nolock)
where szvr01 = 'RC12/1810/04,3345' and sznxtr >580
) union
(select distinct szdoc
from proddta.f47047 (nolock)
where szvr01 = 'RC12/1810/04,3345' and sznxtr >580
)
我得到这个:
szdoc
_______
18004932
预期成绩:
szdoc
_______
18004932
_______
18004932
解决方案
union
删除重复项。对于此查询,您需要union all
:
(select distinct szdoc
from proddta.f47047 (nolock)
where szvr01 = 'RC12/1810/04,3345' and sznxtr >580
) union all
(select distinct szdoc
from proddta.f47047 (nolock)
where szvr01 = 'RC12/1810/04,3345' and sznxtr >580
)
但是,我强烈建议改用派生表:
select szdoc
from (select distinct szdoc
from proddta.f47047 f
where szvr01 = 'RC12/1810/04,3345' and
sznxtr > 580
) f cross join
(values (1), (2)) v(n)
推荐阅读
- vue.js - TypeError:尝试记录我的 $refs 时无法读取未定义的属性“0”
- c++ - 在 Win32 应用程序中,如何检索应用程序类别信息
- katalon-studio - 如果 sql 查询返回正确的值,katalon 测试用例
- java - How to return the value of an asynchronous callback?
- javascript - Handling errors from forms - Node / Express
- java - Interface null pointer exception in Network Callback onAvailable method
- c# - Problem with huge numbers in an incremental game (C#, Unity)
- nestjs - process.env.NODE_ENV always 'development' when building nestjs app with nrwl nx
- jquery - Why isn't jQuery Number Counter updating on html attribute change?
- python - 为什么 python 抛出 ModuleNotFoundError: No module named `blah`