在我的表中有1,00,000条记录,其中有1000行和100列,表中的一些记录有NULL值,我想要的是如果记录的整行的Null值将是第一个我可以很容易地检查哪一行包含哪一行以及哪一列包含NULL值
我的表是这样的:
Staff_ID First_Name Last_Name
1 Stephen Jiang
2 Ravi chaganti
3 Michael Blythe
4 NULL NULL
5 Jillian NULL
6 NULL Vargas
我希望输出如下:
Staff_ID First_Name Last_Name
4 NULL NULL
5 Jillian NULL
6 NULL Vargas
1 Stephen Jiang
2 Ravi chaganti
3 Michael Blythe
我需要一个查询。
答案 0 :(得分:3)
select Staff_ID, First_Name, Last_Name
from YourTable
order by cast(Staff_ID as varchar(10))+First_Name+Last_Name,
Staff_ID
首先会订购 NULL
个值。
将值与NULL
连接时,结果为NULL
,因此如果First_Name+Last_Name
NULL
为NULL
,则cast(Staff_ID as varchar(10))+...
将为First_Name+Last_Name
。
NULL
不 Staff_ID
时, int
可以保证您所需的订单(我假设Staff_ID
是{{1} }})。
按First_Name+Last_Name
列的额外订单可以保证您希望NULL
declare @TableName sysname = 'YourTable'
declare @Sql nvarchar(max) = 'select * from '+quotename(@TableName)+ ' order by '
select @Sql = @Sql+'+cast('+COLUMN_NAME+' as varchar(max))'
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = @TableName
exec (@Sql)
<强>更新强>
您可以使用INFORMATION_SCHEMA.COLUMNS动态构建查询。这可能会做你想要的。请注意,这并未针对所有类型的数据类型进行测试。
;with C as
(
select *,
row_number() over(order by (select 1)) as rn
from YourTable
)
select C1.*
from C as C1
cross apply (select *
from C as C2
where C1.rn = C2.rn
for xml path('x'), type) as T(N)
order by T.N.value('count(/x/*)', 'int')
更新2
使用XML的非动态版本。
row_number()
更新3
如果您知道表的主键并且不介意输入where子句,则可以使用它而不是使用select Y1.*
from YourTable as Y1
order by (select *
from YourTable as Y2
where Y1.Staff_ID = Y2.Staff_ID
for xml path('x'), type).value('count(/x/*)', 'int')
。
{{1}}
答案 1 :(得分:1)
select Staff_ID, First_Name, Last_Name FROM [tablename]
可以获得您描述的输出; P特别不需要打印出空值。
如果要查找特定的空值,请使用WHERE columnname IS NULL
编辑:如果您只是想要按null排序,请首先执行ORDER BY Last_Name,First_Name ASC / DESC,具体取决于您要对其进行排序的方式。
答案 2 :(得分:0)
SELECT Staff_ID, First_Name, Last_Name
FROM myTable
ORDER BY Last_Name DESC
也试试这个:
SELECT Staff_ID, First_Name, Last_Name
FROM myTable
ORDER BY (CASE WHEN Last_Name IS NULL THEN 0 ELSE 1 END), Last_Name DESC
答案 3 :(得分:0)
select Staff_ID , First_Name , Last_Name from Staff where (First_Name Is Null or
Last_Name is Null)
答案 4 :(得分:0)
'Union' is the answer.
(SELECT Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is null AND Last_Name is null)
Union
(SELECT Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is not null AND Last_Name is null)
union
(SELECT Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is null AND Last_Name is not null)
union
(SELECT Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is not null AND Last_Name is not null)
按顺序查询将具有挑战性,所以这是:
Select Staff_ID, First_Name, Last_Name FROM
(
(SELECT '1' as firstSortPref, Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is null AND Last_Name is null)
Union
(SELECT '2' as firstSortPref, Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is not null AND Last_Name is null)
union
(SELECT '3' as firstSortPref, Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is null AND Last_Name is not null)
union
(SELECT '4' as firstSortPref, Staff_ID, First_Name, Last_Name
FROM myTable
Where First_Name is not null AND Last_Name is not null)
) as tbl1 order by tbl1.firstSortPref, tbl1.First_Name, tbl1.Last_Name