SQL One Liners: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Snowflake= ==List row count for views in a schema== This creates a command you subsequently execute <syntaxhighlight lang="sql"> select listagg(xx, ' union all ') from (..." |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
); | ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==Convert date from known format== | |||
<syntaxhighlight lang="sql"> | |||
select to_date('3/4/2013', 'dd/mm/yyyy'); | |||
</syntaxhighlight> | |||
=MS SQL= | =MS SQL= | ||
==List row count for tables in a schema== | ==List row count for tables in a schema== | ||
Line 22: | Line 27: | ||
WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0 | WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0 | ||
ORDER BY o.NAME | ORDER BY o.NAME | ||
</syntaxhighlight> | |||
=Consecutive Days= | |||
<syntaxhighlight lang="sql"> | |||
SELECT | |||
ID, | |||
REASON_ID, | |||
START_DATE, | |||
DATEADD(day, - ROW_NUMBER() OVER (PARTITION BY ID, REASON_ID ORDER BY ID, REASON_ID, START_DATE), START_DATE ) AS PREV_START_DATE | |||
FROM | |||
LOGS | |||
</syntaxhighlight> | |||
This produces rows | |||
<syntaxhighlight lang="txt"> | |||
Row ID Reason Start Date Prev Start Date | |||
-------------------------------------------- | |||
1 3546 2 2023-06-30 2023-06-29 | |||
2 1869 2 2023-06-23 2023-06-22 | |||
3 1869 2 2023-06-26 2023-06-24 | |||
4 1869 2 2023-06-27 2023-06-24 | |||
5 1869 2 2023-06-28 2023-06-24 | |||
</syntaxhighlight> | |||
Now the row with consecutive dates have the same Prev Start Date. We can count PREV_START_DATE to understand the number of consecutive days, max date contains the start of range and min date contains the end of range. | |||
<syntaxhighlight lang="sql"> | |||
SELECT | |||
ID, | |||
REASON_ID, | |||
MAX(START_DATE) AS START_DATE, | |||
MIN(START_DATE) AS END_DATE, | |||
COUNT(PREV_START_DATE) AS CONTINUOUS_DAYS | |||
FROM ( | |||
SELECT | |||
ID, | |||
REASON_ID, | |||
START_DATE, | |||
DATEADD(day, - ROW_NUMBER() OVER (PARTITION BY ID, REASON_ID ORDER BY ID, REASON_ID, START_DATE), START_DATE ) AS PREV_START_DATE | |||
FROM | |||
LOGS | |||
) | |||
GROUP BY | |||
ID, | |||
REASON_ID, | |||
PREV_START_DATE; | |||
</syntaxhighlight> | |||
=Postgres= | |||
Update one table from another | |||
<syntaxhighlight lang="sql"> | |||
UPDATE PUB.Customer | |||
SET (Balance) = (SELECT myCustomer.Balance FROM PUB.myCustomer WHERE myCustomer.Custnum = Customer.Custnum) | |||
WHERE PUB.Customer.Custnum IN (SELECT Custnum FROM PUB.myCustomer) | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 01:37, 30 May 2024
Snowflake
List row count for views in a schema
This creates a command you subsequently execute
select listagg(xx, ' union all ')
from (
select 'select count(*) c, \'' || x || '\' v from ' || x as xx
from (
select TABLE_CATALOG ||'.'|| TABLE_SCHEMA ||'."'||TABLE_NAME||'"' x
from IHC.INFORMATION_SCHEMA.VIEWS
where table_schema='APPROVALPLUS'
)
);
Convert date from known format
select to_date('3/4/2013', 'dd/mm/yyyy');
MS SQL
List row count for tables in a schema
SELECT o.NAME,
i.rowcnt
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME
Consecutive Days
SELECT
ID,
REASON_ID,
START_DATE,
DATEADD(day, - ROW_NUMBER() OVER (PARTITION BY ID, REASON_ID ORDER BY ID, REASON_ID, START_DATE), START_DATE ) AS PREV_START_DATE
FROM
LOGS
This produces rows
Row ID Reason Start Date Prev Start Date
--------------------------------------------
1 3546 2 2023-06-30 2023-06-29
2 1869 2 2023-06-23 2023-06-22
3 1869 2 2023-06-26 2023-06-24
4 1869 2 2023-06-27 2023-06-24
5 1869 2 2023-06-28 2023-06-24
Now the row with consecutive dates have the same Prev Start Date. We can count PREV_START_DATE to understand the number of consecutive days, max date contains the start of range and min date contains the end of range.
SELECT
ID,
REASON_ID,
MAX(START_DATE) AS START_DATE,
MIN(START_DATE) AS END_DATE,
COUNT(PREV_START_DATE) AS CONTINUOUS_DAYS
FROM (
SELECT
ID,
REASON_ID,
START_DATE,
DATEADD(day, - ROW_NUMBER() OVER (PARTITION BY ID, REASON_ID ORDER BY ID, REASON_ID, START_DATE), START_DATE ) AS PREV_START_DATE
FROM
LOGS
)
GROUP BY
ID,
REASON_ID,
PREV_START_DATE;
Postgres
Update one table from another
UPDATE PUB.Customer
SET (Balance) = (SELECT myCustomer.Balance FROM PUB.myCustomer WHERE myCustomer.Custnum = Customer.Custnum)
WHERE PUB.Customer.Custnum IN (SELECT Custnum FROM PUB.myCustomer)