question

liton avatar image
liton asked

Top 10 records in one row

Is there a way to display top 10 rows that have null value in one row? I have a table which has three columns(Table Name, Count, TopRecords) and I have a script which is used to fill the table. In the Table_Name it will have the name of the table, Count has the number of records that have a null fname in that table and I want to output top 10 record id in the TopRecords field. Is it possible to store 10 records in one row? Thanks.
selecttop
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

KenJ avatar image KenJ commented ·
Can you elaborate a bit on the output you're after? Are you after a 3 column result with Ten Table name values in the first column, ten counts in the second column, ten TopRecords in the third column? Are you after a 30 column result with each of the top 10 row's columns transferred to columns in the single row (so you have 10 TableName colums, ten count columns and ten toprecords columns? Something else?
0 Likes 0 ·
liton avatar image liton commented ·
Each row will have a table name and I want to display top 10 row for that colulm. First column will have the table name, second column will have the all record count that have null fname and third column will only output 10 id that have null value. It doesn't have to be top 10. I just want to output 10 random id that have null fname so people can manually check the record using ids. Thanks
0 Likes 0 ·

1 Answer

·
Kev Riley avatar image
Kev Riley answered
If you can get the record ids then you can construct a string made up of those values, for example: select top 10 object_id from sys.objects declare @TopTen varchar(max) set @TopTen = STUFF(( SELECT top 10 ', ' + cast([object_id] as varchar) FROM sys.objects FOR XML PATH('') ), 1, 2, '') select @TopTen
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.