如何从表中获取NULL值记录?

时间:2011-10-05 11:01:52

标签: sql sql-server-2008 null

在我的表中有1,00,000条记录,其中有1000行和100列,表中的一些记录有NULL值,我想要的是如果记录的整行的Null值将是第一个我可以很容易地检查哪一行包含哪一行以及哪一列包含NULL值

  1. 如果ROW / COLUM的值= NULL,则打印所有空值Rows和列First
  2. 非空值列打印下一个NULL值行
  3. 我的表是这样的:

    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
    

    我需要一个查询。

5 个答案:

答案 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 NULLNULL,则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