sql problem more difficult to appear in the forum: 23 (filled with random questions)

Original: SQL problem more difficult to appear in the forum: 23 (filled with random questions)

Recently, in the forum, I met a lot more difficult sql problem, although they can be resolved, but found a few days later, they can not remember, forget the solution of.

So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.


1, a section of SQL wording, seeking method!

http://bbs.csdn.net/topics/390705441


select * from k11 have 10 records

files1 files2 files3 files4
AA BB CC DD
ZZ TT YY EE
NN MM GG UU

another table User 99 records, now random randomly selected three records in the User table inside
select top 3 NewID () AS Random, [the UserId] from [the User]
Random the UserId
E81A4DBB Ming
F31B3B6C Ling
16,574,317 Ting

the User UserId value of the result on the table evenly onto k11 above the table, the final results are shown below

files1 files2 files3 files4 the UserId
A1 BB the CC ming DD
A2 TT EE YY ling
A3 MM GG UU ling
A4 XX XX XX ming
A5 XX XX XX ling
A6        XX     XX       XX     Ting
A7        XX     XX       XX     ming
A8        XX     XX       XX     ling
A9        XX     XX       XX     ling
A10       XX     XX       XX     ming


sql Code:


   
   
  1. create table k11(
  2. files1 varchar( 10),
  3. files2 varchar( 10),
  4. files3 varchar( 10),
  5. files4 varchar( 10),
  6. UserId varchar( 10))
  7. insert into k11
  8. select 'A1', 'BB', 'CC', 'DD', 'ming' union all
  9. select 'A2', 'EE', 'TT', 'YY', 'ling' union all
  10. select 'A3', 'MM', 'GG', 'UU', 'Ting' union all
  11. select 'A4', 'XX', 'XX', 'XX', 'ming' union all
  12. select 'A5', 'XX', 'XX', 'XX', 'ling' union all
  13. select 'A6', 'XX', 'XX', 'XX', 'Ting' union all
  14. select 'A7', 'XX', 'XX', 'XX', 'ming' union all
  15. select 'A8', 'XX', 'XX', 'XX', 'ling' union all
  16. select 'A9', 'XX', 'XX', 'XX', 'Ting' union all
  17. select 'A10', 'XX', 'XX', 'XX', 'ming'
  18. create table [ user]
  19. (
  20. UserId varchar( 10)
  21. )
  22. insert into [ user]
  23. select 'ming' union all
  24. select 'ling' union all
  25. select 'Ting'
  26. go
  27. ;with t
  28. as
  29. (
  30. select [UserId],
  31. ROW_NUMBER() over( order by newid()) as rownum
  32. from [ User]
  33. ),
  34. tt
  35. as
  36. (
  37. select *,
  38. case when rownum1 % 3 = 0 then 3 else rownum1 % 3 end as rownum
  39. from
  40. (
  41. select *,
  42. ROW_NUMBER() over( order by getdate()) as rownum1
  43. from k11
  44. )a
  45. )
  46. select tt.files1,tt.files2,tt.files3,tt.files4,t.UserId
  47. from tt
  48. left join t
  49. on tt.rownum = t.rownum
  50. and t.rownum <= 3
  51. /*
  52. files1 files2 files3 files4 UserId
  53. A1 BB CC DD ming
  54. A2 EE TT YY ling
  55. A3 MM GG UU Ting
  56. A4 XX XX XX ming
  57. A5 XX XX XX ling
  58. A6 XX XX XX Ting
  59. A7 XX XX XX ming
  60. A8 XX XX XX ling
  61. A9 XX XX XX Ting
  62. A10 XX XX XX ming
  63. */

2, the random data return 100:

If the table is relatively large, there are tens of millions of data, by code, you can return 100 random data, random here, not in the sense of truly random, but it does make people feel is random, that is, each time you return data are not the same, the key is very fast, close to 0 sec consumed only 62 milliseconds.


   
   
  1. select *
  2. from
  3. (
  4. select *,
  5. ROW_NUMBER() over( order by @@servername) as rownum
  6. from dbo.xxx
  7. )t
  8. where rownum between CHECKSUM( getdate()) % 10000 and CHECKSUM( getdate()) % 10000+ 99


SQL Server 分析和编译时间: 
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。

(100 行受影响)

 SQL Server 执行时间:
   CPU 时间 = 0 毫秒,占用时间 = 62 毫秒。



Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12020040.html