使用不同数据集的 UNION ORDER BY (T-SQL)
我有一个查询,UNION
的两个有些相似的数据集,但它们都有一些在另一个中不存在的列(即,这些列在生成的 UNION
中具有 NULL 值) code>。)
问题是,我需要使用仅存在于一个或另一组中的那些列来ORDER
结果数据,以便以软件端友好的格式获取数据。
例如:Table1 具有字段ID、Cat、Price
。 Table2 具有字段ID、名称、缩写
。 ID
字段在两个表之间是通用的。
我的查询看起来像这样:
SELECT t1.ID, t1.Cat, t1.Price, NULL as Name, NULL as Abbrv FROM t1
UNION
SELECT t2.ID, NULL as Cat, NULL as Price, t2.Name, t2.Abbrv FROM t2
ORDER BY Price DESC, Abbrv ASC
ORDER BY
是我陷入困境的地方。 数据看起来像这样:
100 Balls 1.53
200 Bubbles 1.24
100 RedBall 101RB
100 BlueBall 102BB
200 RedWand 201RW
200 BlueWand 202BW
...但我希望它看起来像这样:
100 Balls 1.53
100 RedBall 101RB
100 BlueBall 102BB
200 Bubbles 1.24
200 RedWand 201RW
200 BlueWand 202BW
我希望这可以在 T-SQL 中完成。
I have a query that UNION
's two somewhat similar datasets, but they both have some columns that are not present in the other (i.e., the columns have NULL values in the resulting UNION
.)
The problem is, I need to ORDER
the resulting data using those columns that only exist in one or the other set, to get the data in a friendly format for the software-side.
For example: Table1 has fields ID, Cat, Price
. Table2 has fields ID, Name, Abbrv
. The ID
field is common between the two tables.
My query looks like something like this:
SELECT t1.ID, t1.Cat, t1.Price, NULL as Name, NULL as Abbrv FROM t1
UNION
SELECT t2.ID, NULL as Cat, NULL as Price, t2.Name, t2.Abbrv FROM t2
ORDER BY Price DESC, Abbrv ASC
The ORDER BY
is where I'm stuck. The data looks like this:
100 Balls 1.53
200 Bubbles 1.24
100 RedBall 101RB
100 BlueBall 102BB
200 RedWand 201RW
200 BlueWand 202BW
...but I want it to look like this:
100 Balls 1.53
100 RedBall 101RB
100 BlueBall 102BB
200 Bubbles 1.24
200 RedWand 201RW
200 BlueWand 202BW
I'm hoping this can be done in T-SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须以某种方式知道表 2 中的数据与表 1 相关联并共享价格。 由于 abbrv 中的 Null 将排在第一位,因此无需创建 SortAbbrv 列。
Somehow you have to know the data in table 2 are linked to table 1 and share the price. Since the Null in abbrv will come first, there is no need to create a SortAbbrv column.
您应该使用 UNION ALL 而不是 UNION 以节省重复检查的成本。
You should use UNION ALL instead of UNION to save the cost of duplicate checking.
一个快速的解决方案是对临时表或表变量进行两次插入,作为插入临时表的一部分,您可以设置一个标志列来帮助排序,然后按该标志列进行排序。
A quick solution would be to do 2 inserts into a temp table or a table variable and as part of insert into the temp table you can set a flag column to help with sorting and then order by that flag column.
我想说的是,最坏的情况是您创建一个临时表,其中所有字段都从 T1 和 T1 执行 INSERT INTO 临时表。 T2 然后使用 order by 从临时表中进行 SELECT。
IE。 创建一个临时表(例如#temp),其中包含字段 Id、Cat、Price、Name、Abbrv,然后:
注意:我不能 100% 确定插入中的 null 语法,但我认为它会起作用。
编辑:添加了按价格和价格排序 id 后的缩写...如果 Id 没有链接 T1 & 那么T2又是什么呢?
Off the top of my head i would say the worst case scenario is you create a temporary table with all the fields do an INSERT INTO the temp table from both T1 & T2 then SELECT from the temp table with an order by.
ie. Create a temp table (eg. #temp) with fields Id, Cat, Price, Name, Abbrv, and then:
NB: I'm not 100% sure on the null syntax from the inserts but i think it will work.
EDIT: Added ordering by Price & Abbrv after id... if Id doesn't link T1 & T2 then what does?
如果您的查询结果预期的结果数量有限,您可以使用 OFFSET / FETCH 关键字。 这里假设,结果集不会超过10000行。
此 SQL 语句将在内部对 table1 结果进行排序。
然后它会在内部对 table2 结果进行排序。
并且它将按此顺序合并这些子集。
例如 :
If you have a limited number of results expected from your query results you can use OFFSET / FETCH Keywords. Here assumption is , result set will not exceed 10000 rows.
This SQL Statement will order table1 results internally.
Then it will order table2 results internally.
And it will merge these subsets in this order.
For example :