Created
February 28, 2018 07:36
-
-
Save Goryudyuma/2e072eb4ccbb68cf01b9331ba1fe004e to your computer and use it in GitHub Desktop.
MISQUERY ONLINE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT n | |
FROM tbl | |
ORDER BY n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
SUM(n) AS sum, | |
MAX(n) AS max, | |
MIN(n) AS min | |
FROM tbl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT n, | |
CASE | |
WHEN n % 15 = 0 THEN 'FizzBuzz' | |
WHEN n % 3 = 0 THEN 'Fizz' | |
WHEN n % 5 = 0 THEN 'Buzz' | |
ELSE CAST(n AS text) | |
END AS f | |
FROM | |
GENERATE_SERIES( | |
(SELECT a FROM tbl), | |
(SELECT b FROM tbl)) AS tmp(n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT i, TRANSLATE(str, | |
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM' | |
) AS converted_str | |
FROM tbl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE solve(min, max, n) AS ( | |
VALUES(1.0, 2.1, 0) | |
UNION ALL | |
SELECT | |
( | |
CASE WHEN t.a * POWER( 2.718281828459045235360287471352, (s.min + s.max) / 2) + t.b * POW((s.min + s.max) / 2, 3) + t.c * (s.min + s.max) / 2 + t.d > 0 THEN | |
s.min | |
ELSE | |
(s.min + s.max) / 2 | |
END | |
), | |
( | |
CASE WHEN t.a * POWER( 2.718281828459045235360287471352, (s.min + s.max) / 2) + t.b * POW((s.min + s.max) / 2, 3) + t.c * (s.min + s.max) / 2 + t.d > 0 THEN | |
(s.min + s.max) / 2 | |
ELSE | |
max | |
END | |
), | |
n+1 | |
FROM (SELECT a,b,c,d FROM tbl) AS t, solve AS s | |
WHERE n < 100 | |
) | |
SELECT MAX(min) AS solution FROM solve LIMIT 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT n | |
FROM ( | |
(WITH RECURSIVE numbers (x) AS ( | |
SELECT 2 | |
UNION ALL | |
SELECT x + 1 FROM numbers WHERE x <= 10000) | |
SELECT tbl.n, COUNT(tbl.n) | |
FROM numbers AS n | |
INNER JOIN tbl | |
ON MOD(tbl.n, x) = 0 | |
GROUP BY tbl.n) | |
) AS ans | |
WHERE ans.count = 1 | |
ORDER BY ans.n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE solve(strrow,str) AS ( | |
SELECT str,str | |
FROM tbl | |
UNION ALL | |
SELECT strrow,REPLACE(REPLACE(REPLACE(str, '()', ''),'{}',''),'[]','') | |
FROM solve | |
WHERE str!=REPLACE(REPLACE(REPLACE(str, '()', ''),'{}',''),'[]','') | |
) | |
SELECT i, | |
CASE WHEN str IN | |
( | |
SELECT strrow | |
FROM solve | |
WHERE str = '' | |
) THEN 'YES' | |
ELSE 'NO' | |
END AS judgement | |
FROM tbl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH RECURSIVE | |
tblidx AS ( | |
SELECT ROW_NUMBER() OVER() AS id, x, y | |
FROM tbl), | |
root AS ( | |
SELECT ROW_NUMBER() OVER() AS id, map.aid AS aid, map.bid AS bid, map.len AS len | |
FROM ( | |
SELECT a.id AS aid, b.id AS bid, | |
SQRT(POW(a.x-b.x,2)+POW(a.y-b.y,2)) AS len | |
FROM tblidx AS a | |
CROSS JOIN tblidx AS b | |
ORDER BY len | |
) AS map | |
), | |
kruskalinit(data, n) AS ( | |
VALUES(ARRAY[1], 1) | |
UNION ALL | |
SELECT data || n + 1, n + 1 | |
FROM kruskalinit | |
WHERE n < ( | |
SELECT COUNT(1) | |
FROM tblidx | |
) | |
), | |
kruskal(ans, tablex, n) AS ( | |
(SELECT 0 :: DOUBLE PRECISION, data, 1 | |
FROM kruskalinit | |
ORDER BY n DESC | |
LIMIT 1) | |
UNION ALL | |
SELECT | |
CASE WHEN tablex[root.aid] != tablex[root.bid] | |
THEN | |
ans + root.len | |
ELSE | |
ans | |
END, | |
(SELECT ARRAY_AGG( | |
CASE WHEN i = tablex[root.bid]::INTEGER | |
THEN | |
tablex[root.aid]::INTEGER | |
ELSE | |
i | |
END) | |
FROM (SELECT UNNEST(tablex) AS i) AS x), | |
n+1 | |
FROM kruskal, root | |
WHERE n <= (SELECT COUNT(1) FROM root) AND root.id = n | |
) | |
SELECT MAX(ans) AS c FROM kruskal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment