Sql 在联合查询中对别名列排序
我有一个多次使用联合的SQL查询,在其中我创建了一个名为“Category”的别名列。当我在alias列上尝试一个简单的order by时,效果很好。但是,“我的别名”列中的数据有数字,它们的顺序是1,10,11,而不是1,2,3等 当我尝试将order by更改为order by len时,我突然出现两个错误。。。1表示“类别”列不存在,另一列表示“如果语句包含UNION、INTERSECT或EXCEPT运算符,则ORDER BY items必须出现在选择列表中”。当我尝试将其转换为INT时,也同样适用 查询的精简版本如下所示Sql 在联合查询中对别名列排序,sql,Sql,我有一个多次使用联合的SQL查询,在其中我创建了一个名为“Category”的别名列。当我在alias列上尝试一个简单的order by时,效果很好。但是,“我的别名”列中的数据有数字,它们的顺序是1,10,11,而不是1,2,3等 当我尝试将order by更改为order by len时,我突然出现两个错误。。。1表示“类别”列不存在,另一列表示“如果语句包含UNION、INTERSECT或EXCEPT运算符,则ORDER BY items必须出现在选择列表中”。当我尝试将其转换为INT时,
SELECT 'Category 1' as Category, category1Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
UNION
SELECT 'Category 2' as Category, category2Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
UNION
SELECT 'Category 10' as Category, category10Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
例如,我想按类别1、类别2、类别10进行订购。您可以在下面进行尝试-
select Category, categoryScore from
(
SELECT 'Category 1' as Category, category1Score as categoryScore,1 as ord
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
UNION
SELECT 'Category 2' as Category, category2Score as categoryScore,2
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
UNION
SELECT 'Category 10' as Category, category10Score as categoryScore,3
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019' and
'10/10/2019'
)A order by ord
子查询,然后按
类别
列排序:
SELECT Category, CategoryScore
FROM
(
SELECT 'Category 1' AS Category, category1Score AS categoryScore, 1 AS pos
FROM table1
WHERE lineNumber = 'Reflow 2' AND dateOfAudit BETWEEN '01/10/2019' AND '10/10/2019'
UNION ALL
SELECT 'Category 2', Category2Score, 2
FROM table1
WHERE lineNumber = 'Reflow 2' AND dateOfAudit BETWEEN '01/10/2019' AND '10/10/2019'
UNION ALL
SELECT 'Category 10', category10Score, 3
FROM table1
WHERE lineNumber = 'Reflow 2' AND dateOfAudit BETWEEN '01/10/2019' AND '10/10/2019'
) t
ORDER BY
pos;
注:
我在这里使用UNION ALL
而不是UNION
,因为您的意图很可能是不删除重复项。还请注意,您只需要在联合的第一个查询中使用别名。事实上,后续别名将被忽略
最后,我选择引入一个新的计算列,名为pos
,它为联合中的每个查询编码所需的顺序。虽然我们只能依赖于类别
,但如果您的查询可能会发生更改,则以后按类别排序可能会出现意外行为,因为它是文本,而不是纯数字。请尝试下面的查询
with CTE as
(
SELECT 'Category 1' as Category, category1Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019'
and '10/10/2019'
UNION
SELECT 'Category 2' as Category, category2Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019'
and '10/10/2019'
UNION
SELECT 'Category 10' as Category, category10Score as categoryScore
from table1
WHERE lineNumber = 'Reflow 2' and dateOfAudit between '01/10/2019'
and '10/10/2019'
) select Category,categoryScore from CTE
ORDER BY
Category ASC
为什么要使用
union
进行外部查询
select c.Category,
(case ord
when 1 then category1Score
when 2 then category2Score
when 3 then category3Score
when 4 then category4Score
when 5 then category5Score
when 6 then category6Score
when 7 then category7Score
when 8 then category8Score
when 9 then category9Score
when 10 then category10Score
when 1 then category1Score
end) as categoryScore
from table1 t1 cross join
(select 'Category 1' as category, 1 as ord union all
select 'Category 2', 2 as ord union all
select 'Category 3', 3 as ord union all
select 'Category 4', 4 as ord union all
select 'Category 5', 5 as ord union all
select 'Category 6', 6 as ord union all
select 'Category 7', 7 as ord union all
select 'Category 8', 8 as ord union all
select 'Category 9', 9 as ord union all
select 'Category 10', 10 as ord
) c
where t.lineNumber = 'Reflow 2' and
t.dateOfAudit between '2019-10-01' and '2019-10-10'
order by c.ord;
通过将表上的筛选条件仅放在一个位置,这样可以减少编写查询时的错误范围。可能会重复使用正在使用的数据库标记问题。