如何使用SAP HANA Web-based Development工具进行SQLScript练习

发布时间:2021-12-16 11:47:35 作者:小新
来源:亿速云 阅读:240

小编给大家分享一下如何使用SAP HANA Web-based Development工具进行SQLScript练习,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

通过csv文件提供的数据库表内容:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

links.csv的格式:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

movies.csv格式,一个movie可以有多种风格(genres),通过|分隔:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

ratings.csv:

用户给movie打得分:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

tags.csv:movie的标签

如何使用SAP HANA Web-based Development工具进行SQLScript练习

练习一:

列出四张表的总记录数:

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

执行结果:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

练习2:计算总共9125部电影,一共包含多少艺术类别?

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;

执行结果,总共包含18种:

如何使用SAP HANA Web-based Development工具进行SQLScript练习

练习3:计算每种艺术类别总共包含多少部电影:

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工具进行SQLScript练习

练习4:列出每部电影包含的风格数目:

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工具进行SQLScript练习

练习5:罗列出每部电影的风格分布情况

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个风格的电影,有2793部,2个风格的电影有3039部,等等。

如何使用SAP HANA Web-based Development工具进行SQLScript练习

练习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工具进行SQLScript练习

明细情况:

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部电影的用户投票数为5票

如何使用SAP HANA Web-based Development工具进行SQLScript练习

练习7:统计用户投票情况

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工具进行SQLScript练习

练习8:统计用户投票得分情况

SELECT "RATING", COUNT(1) as "RATING_COUNT"

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

GROUP BY "RATING" ORDER BY 1 DESC;

有15095份用户投票,打的分数是5分

如何使用SAP HANA Web-based Development工具进行SQLScript练习

看完了这篇文章,相信你对“如何使用SAP HANA Web-based Development工具进行SQLScript练习”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 怎么使用Eclipse连接SAP云平台上的HANA数据库
  2. 如何使用JDBC操作SAP云平台上的HANA数据库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

development

上一篇:企业级CI/CD工具部署 Serverless 应用的落地实践是怎样的

下一篇:Linux sftp命令的用法是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》