溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

發(fā)布時間:2021-12-16 11:47:35 來源:億速云 閱讀:235 作者:小新 欄目:大數(shù)據(jù)

小編給大家分享一下如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

通過csv文件提供的數(shù)據(jù)庫表內(nèi)容:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

links.csv的格式:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

movies.csv格式,一個movie可以有多種風(fēng)格(genres),通過|分隔:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

ratings.csv:

用戶給movie打得分:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

tags.csv:movie的標(biāo)簽

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)一:

列出四張表的總記錄數(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é)果:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)2:計算總共9125部電影,一共包含多少藝術(shù)類別?

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種:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)3:計算每種藝術(shù)類別總共包含多少部電影:

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;

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)4:列出每部電影包含的風(fēng)格數(shù)目:

SELECT

"MOVIEID"

, "TITLE"

, OCCURRENCES_REGEXPR('[|]' IN GENRES) + 1 "GENRE_COUNT"

, "GENRES"

FROM "MOVIELENS"."public.aa.movielens.hdb::data.MOVIES"

ORDER BY "GENRE_COUNT" ASC;

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)5:羅列出每部電影的風(fēng)格分布情況

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部,等等。

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)6:計算movie的rating分布情況

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";

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

明細(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票

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)7:統(tǒng)計用戶投票情況

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票:

如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)

練習(xí)8:統(tǒng)計用戶投票得分情況

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í)

看完了這篇文章,相信你對“如何使用SAP HANA Web-based Development工具進(jìn)行SQLScript練習(xí)”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(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)容。

AI