一些可能会用到的SQL关键词:
SUM(sales) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) SUM(sales) OVER (PARTITION BY prodid ORDER BY sales DESC) SUM(sales) OVER (PARTITION BY storeid ORDER BY sales DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
累加:
SUM(sales) OVER (ORDER BY sales DESC ROWS UNBOUNDED PRECEDING)
日期:
SELECT ADD_MONTHS(DATE '2005-12-29',2); SELECT CURRENT_DATE + INTERVAL '3' YEAR; SELECT (DATE '2005-03-01' - DATE '2005-01-01') DAY; SELECT CAST ((DATE'2004-03-01'- DATE'2004-01-01') AS INTERVAL MONTH); SELECT EXTRACT (YEAR FROM DATE '2004-12-15')
时间:
SELECT (TIME '10:35:40'- TIME '08:00:00') HOUR; SELECT (TIME '10:35:40'-TIME '08:00:00') MINUTE(3); SELECT (TIME '23:59:59.99'- CURRENT_TIME) HOUR; SELECT CAST ((INTERVAL '3:45' HOUR TO MINUTE) AS INTERVAL MINUTE(3)); SELECT EXTRACT (HOUR FROM (TIME '09:45:20'));
排列:
SELECT storeid, prodid, sales, RANK() OVER (PARTITION BY storeid ORDER BY sales DESC)AS Rank_Sales FROM salestbl QUALIFY Rank_Sales <= 3 ; SELECT Prodid, Sumsales, RANK( ) OVER (ORDER BY Sumsales DESC) AS "Ranking" FROM (SELECT prodid, SUM(sales) FROM salestbl GROUP BY 1) AS dt(Prodid, Sumsales) QUALIFY Ranking <= 3;
分层:
SELECT department_number AS Dept ,budget_amount AS Budget ,WIDTH_BUCKET(budget_amount,100000,1000000,9) AS WB FROM department GROUP BY 1,2 ORDER BY 3,2;