Skip to content

Instantly share code, notes, and snippets.

@Dobiasd

Dobiasd/asd.md Secret

Created Nov 13, 2020
Embed
What would you like to do?
CREATE TABLE X (
    a int,
    b int
);

INSERT INTO X (a, b) VALUES (1, 1);
INSERT INTO X (a, b) VALUES (1, 4);
INSERT INTO X (a, b) VALUES (4, 1);
INSERT INTO X (a, b) VALUES (4, 4);
INSERT INTO X (a, b) VALUES (2, 2);
INSERT INTO X (a, b) VALUES (2, 3);
INSERT INTO X (a, b) VALUES (3, 2);
INSERT INTO X (a, b) VALUES (3, 3);

SELECT * FROM X ORDER BY (a, b);
SELECT a, COUNT(1) AS ca, SUM(b) AS bs FROM X GROUP BY a ORDER BY a;
SELECT b, COUNT(1) AS cb, SUM(a) AS as FROM X GROUP BY b ORDER BY b;
a   b
1   1
1   4
2   2
2   3
3   2
3   3
4   1
4   4

a   ca  bs
1   2   5
2   2   5
3   2   5
4   2   5

b   cb  as
1   2   5
2   2   5
3   2   5
4   2   5
CREATE TABLE X (
    a int,
    b int
);

INSERT INTO X (a, b) VALUES (1, 2);
INSERT INTO X (a, b) VALUES (1, 3);
INSERT INTO X (a, b) VALUES (2, 1);
INSERT INTO X (a, b) VALUES (3, 1);
INSERT INTO X (a, b) VALUES (2, 4);
INSERT INTO X (a, b) VALUES (3, 4);
INSERT INTO X (a, b) VALUES (4, 2);
INSERT INTO X (a, b) VALUES (4, 3);

SELECT * FROM X ORDER BY (a, b);
SELECT a, COUNT(1) AS ca, SUM(b) AS bs FROM X GROUP BY a ORDER BY a;
SELECT b, COUNT(1) AS cb, SUM(a) AS as FROM X GROUP BY b ORDER BY b;
a   b
1   2
1   3
2   1
2   4
3   1
3   4
4   2
4   3

a   ca  bs
1   2   5
2   2   5
3   2   5
4   2   5

b   cb  as
1   2   5
2   2   5
3   2   5
4   2   5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment