如何将特定行保留为查询 (T-SQL) 的第一个结果? [英] How to keep a specific row as the first result of a query (T-SQL)?

查看:34
本文介绍了如何将特定行保留为查询 (T-SQL) 的第一个结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 SQL 查询以获取 Report Builder 3.0 中报表的参数列表.我需要在结果中添加一个值为All"的额外行,如下所示:

I'm writing a SQL query to get a list of parameters for a report in Report Builder 3.0. I needed to add an extra row with the value 'All' to the results like this:

SELECT 'All'
UNION
SELECT DISTINCT    Manager
FROM               IS_Projects

这工作正常,但查询返回按字母顺序排序的行,我实际上希望全部"始终出现在顶部(即作为第一行返回).其余的结果可以按字母顺序排序.

This works fine, but the query returns the rows to me sorted in alphabetical order, where I actually want 'All' to appear at the top at all times (ie. come back as the first row). The rest of the results can be sorted alphabetically.

我已经看到了向表中添加排序顺序列的建议,但我对 SQL 还很陌生,不知道该怎么做.

I've seen suggestions on adding a sort-order column to the table, but I'm pretty new to SQL, and don't know how to do this.

感谢您的建议!

推荐答案

一种方式;

SELECT Name FROM (
    SELECT 'All'       as Name
    UNION 
    SELECT DISTINCT    Manager
    FROM               IS_Projects
) T
ORDER BY CASE Name WHEN 'All' THEN 0 ELSE 1 END, Name

这篇关于如何将特定行保留为查询 (T-SQL) 的第一个结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆