Advertisement
cydside

SQLITE NULL UNIQUE

Dec 11th, 2023 (edited)
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.88 KB | None | 0 0
  1. -- ==================================================
  2. -- Use ctrl-enter or shift-enter to execute sqlite3
  3. -- shell commands and SQL.
  4. -- If a subset of the text is currently selected,
  5. -- only that part is executed.
  6. -- ==================================================
  7. .nullvalue NULL
  8. .headers ON
  9.  
  10. DROP TABLE IF EXISTS test;
  11.  
  12. CREATE TABLE IF NOT EXISTS test (
  13.     id INTEGER NOT NULL PRIMARY KEY,
  14.     date_i TEXT NOT NULL,
  15.     numb INTEGER,
  16.     sub_n TEXT NOT NULL ON CONFLICT REPLACE DEFAULT "",
  17.     year_n INTEGER NOT NULL,
  18.     UNIQUE(numb, sub_n, year_n)
  19. );
  20.  
  21. --INSERT INTO test VALUES(NULL, '2023-12-09', NULL, NULL, 2023);
  22. --INSERT INTO test VALUES(NULL, '2023-12-09', NULL, NULL, 2023);
  23. --INSERT INTO test VALUES(NULL, '2023-12-09', 3, NULL, 2023);
  24. INSERT INTO test VALUES(NULL, '2023-12-08', 1, NULL, 2023);
  25. INSERT INTO test VALUES(NULL, '2023-12-08', 2, NULL, 2023);
  26. INSERT INTO test VALUES(NULL, '2023-12-09', 3, NULL, 2023);
  27. INSERT INTO test VALUES(NULL, '2023-12-10', 5, NULL, 2023);
  28. INSERT INTO test VALUES(NULL, '2023-12-11', 6, NULL, 2023);
  29. INSERT INTO test VALUES(NULL, '2023-12-12', 11, NULL, 2023);
  30. INSERT INTO test VALUES(NULL, '2023-12-14', 14, NULL, 2023);
  31.  
  32. SELECT * FROM test;
  33.  
  34. -- select min(numb) from test;
  35.  
  36. --SELECT value FROM generate_series(1, 5, 1) WHERE value NOT IN (SELECT numb FROM test);
  37.  
  38. --SELECT value FROM generate_series((select min(numb) from test), (select max(numb) from test), 1) WHERE value NOT IN (SELECT numb FROM test);
  39.  
  40. SELECT COUNT(*) AS is_free WHERE 3 IN (
  41. WITH bound(MIN,MAX) AS (SELECT MIN(numb),MAX(numb) FROM test)
  42. SELECT VALUE FROM generate_series JOIN bound ON START=MIN AND stop=MAX
  43. EXCEPT SELECT numb FROM test
  44. );
  45.  
  46. --SELECT numb FROM test WHERE numb < 4 ORDER BY numb DESC LIMIT 1;
  47.  
  48. --SELECT numb FROM test WHERE numb > 4 ORDER BY numb ASC LIMIT 1;
  49.  
  50. WITH lowBorder AS (
  51. SELECT numb FROM test WHERE numb < 4 ORDER BY numb DESC LIMIT 1
  52. ),
  53. upBorder AS (
  54. SELECT numb FROM test WHERE numb > 4 ORDER BY numb ASC LIMIT 1
  55. )
  56. SELECT * FROM test WHERE numb IN (SELECT * FROM lowBorder UNION SELECT * FROM upBorder)
  57. AND (date_i >= '2023-12-10' OR date_i <= '2023-12-10');
  58.  
  59. WITH lb(d1, n1) AS (
  60. SELECT date_i, numb FROM test WHERE numb < 4 ORDER BY numb DESC LIMIT 1
  61. ),
  62. ub(d2, n2) AS (
  63. SELECT date_i, numb FROM test WHERE numb > 4 ORDER BY numb ASC LIMIT 1
  64. )
  65. SELECT COUNT(*) FROM lb,ub WHERE lb.d1 <= '2023-12-11' AND ub.d2 >= '2023-12-11';
  66.  
  67. WITH lb(d1, n1) AS (
  68. SELECT date_i, numb FROM test WHERE numb < 12 ORDER BY numb DESC LIMIT 1
  69. ),
  70. ub(d2, n2) AS (
  71. SELECT date_i, numb FROM test WHERE numb > 12 ORDER BY numb ASC LIMIT 1
  72. )
  73. SELECT * FROM lb,ub ;
  74.  
  75. WITH lb(d1, n1) AS (
  76. SELECT date_i, numb FROM test WHERE numb < 12 ORDER BY numb DESC LIMIT 1
  77. ),
  78. ub(d2, n2) AS (
  79. SELECT date_i, numb FROM test WHERE numb > 12 ORDER BY numb ASC LIMIT 1
  80. )
  81. SELECT COUNT(*) FROM lb,ub WHERE lb.d1 <= '2023-12-15' AND ub.d2 >= '2023-12-15';
  82.  
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement