您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
通過csv文件提供的數(shù)據(jù)庫表內(nèi)容:
links.csv的格式:
movies.csv格式,一個movie可以有多種風(fēng)格(genres),通過|分隔:
ratings.csv:
用戶給movie打得分:
tags.csv:movie的標(biāo)簽
列出四張表的總記錄數(shù):
select 'links' as "table name", count(1) as "row count" from "MOVIELENS"."public.aa.movielens.hdb::data.LINKS"
union all
select 'movies' as "table name", count(1) as "row count" from "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES"
union all
select 'ratings' as "table name", count(1) as "row count" from "MOVIELENS"."public.aa.movielens.hdb::data.RATINGS"
union all
select 'tags' as "table name", count(1) as "row count" from "MOVIELENS"."public.aa.movielens.hdb::data.TAGS";
執(zhí)行結(jié)果:
DO
BEGIN
DECLARE genreArray NVARCHAR(255) ARRAY;
DECLARE tmp NVARCHAR(255);
DECLARE idx INTEGER;
DECLARE sep NVARCHAR(1) := '|';
DECLARE CURSOR cur FOR SELECT DISTINCT "GENRES" FROM "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES";
DECLARE genres NVARCHAR (255) := '';
idx := 1;
FOR cur_row AS cur() DO
SELECT cur_row."GENRES" INTO genres FROM DUMMY;
tmp := :genres;
WHILE LOCATE(:tmp,:sep) > 0 DO
genreArray[:idx] := SUBSTR_BEFORE(:tmp,:sep);
tmp := SUBSTR_AFTER(:tmp,:sep);
idx := :idx + 1;
END WHILE;
genreArray[:idx] := :tmp;
END FOR;
genreList = UNNEST(:genreArray) AS ("GENRE");
SELECT "GENRE" FROM :genreList GROUP BY "GENRE";
END;
執(zhí)行結(jié)果,總共包含18種:
DO
BEGIN
DECLARE genreArray NVARCHAR(255) ARRAY;
DECLARE tmp NVARCHAR(255);
DECLARE idx INTEGER;
DECLARE sep NVARCHAR(1) := '|';
DECLARE CURSOR cur FOR SELECT DISTINCT "GENRES" FROM "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES";
DECLARE genres NVARCHAR (255) := '';
idx := 1;
FOR cur_row AS cur() DO
SELECT cur_row."GENRES" INTO genres FROM DUMMY;
tmp := :genres;
WHILE LOCATE(:tmp,:sep) > 0 DO
genreArray[:idx] := SUBSTR_BEFORE(:tmp,:sep);
tmp := SUBSTR_AFTER(:tmp,:sep);
idx := :idx + 1;
END WHILE;
genreArray[:idx] := :tmp;
END FOR;
genreList = UNNEST(:genreArray) AS ("GENRE");
SELECT "GENRE", count(1) FROM :genreList GROUP BY "GENRE";
END;
SELECT
"MOVIEID"
, "TITLE"
, OCCURRENCES_REGEXPR('[|]' IN GENRES) + 1 "GENRE_COUNT"
, "GENRES"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES"
ORDER BY "GENRE_COUNT" ASC;
SELECT
"GENRE_COUNT"
, COUNT(1)
FROM (
SELECT
OCCURRENCES_REGEXPR('[|]' IN "GENRES") + 1 "GENRE_COUNT"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES"
)
GROUP BY "GENRE_COUNT" ORDER BY "GENRE_COUNT";
比如至少擁有1個風(fēng)格的電影,有2793部,2個風(fēng)格的電影有3039部,等等。
SELECT DISTINCT
MIN("RATING_COUNT") OVER( ) AS "MIN",
MAX("RATING_COUNT") OVER( ) AS "MAX",
AVG("RATING_COUNT") OVER( ) AS "AVG",
SUM("RATING_COUNT") OVER( ) AS "SUM",
MEDIAN("RATING_COUNT") OVER( ) AS "MEDIAN",
STDDEV("RATING_COUNT") OVER( ) AS "STDDEV",
COUNT(*) OVER( ) AS "CATEGORY_COUNT"
FROM (
SELECT "MOVIEID", COUNT(1) as "RATING_COUNT"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.RATINGS"
GROUP BY "MOVIEID"
)
GROUP BY "RATING_COUNT";
明細(xì)情況:
SELECT "RATING_COUNT", COUNT(1) as "MOVIE_COUNT"
FROM (
SELECT "MOVIEID", COUNT(1) as "RATING_COUNT"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.RATINGS"
GROUP BY "MOVIEID"
)
GROUP BY "RATING_COUNT" ORDER BY "RATING_COUNT" asc;
比如有397部電影的用戶投票數(shù)為5票
SELECT "RATING_COUNT", COUNT(1) as "USER_COUNT"
FROM (
SELECT "USERID", COUNT(1) as "RATING_COUNT"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.RATINGS"
GROUP BY "USERID"
)
GROUP BY "RATING_COUNT" ORDER BY 1 DESC;
有一位用戶投了2391票,一位用戶投了1868票:
SELECT "RATING", COUNT(1) as "RATING_COUNT"
FROM "MOVIELENS"."public.aa.movielens.hdb::data.RATINGS"
GROUP BY "RATING" ORDER BY 1 DESC;
有15095份用戶投票,打的分?jǐn)?shù)是5分
看完了這篇文章,相信你對“如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。