当前位置:网站首页>BI-SQL丨WHILE
BI-SQL丨WHILE
2022-08-02 02:11:00 【PowerBI丨白茶】
WHILE
WHILE,当的意思。在SQL的语法中,通常可以用来重复执行某段SQL脚本。
通俗来说,就是当XXX时,执行某某操作,意为循环。
看到这里,小伙伴可能会有疑问,PowerBI中是否有类似的操作呢?
答案是有的,不过此类操作我们一般都是在PowerQuery中进行的,在DAX中需要进行循环处理的情景较少。
而在SQL中,有很多场景都需要使用循环语句进行处理,比如数据更新或者递增计算等。
语法
WHILE 条件判断
BEGIN
执行操作
SET @[email protected]+1 --参数循环递增
END
注意:
如果嵌套了两个或多个 WHILE 循环,首先运行内层循环结束之前的所有语句,然后下一个外层循环重新开始执行。
使用实例
案例数据:
在白茶本机的数据库中存在名为“CaseData”的数据库。
“Dim_Date“日期表、"Dim_Product"产品表、"Fact_Sales"销售事实表。
例子1:
循环打印数字,从1到9。
DECLARE @NUM INT;
SET @NUM = 1;
WHILE @NUM<= 9
BEGIN
PRINT @NUM
SET @NUM = @NUM + 1;
END
结果如下:
注:此操作无法在PowerBI中执行,会有报错。
例子2:
创建一张表,并在表中循环插入Price小于等于10的ProductName和Price。
USE CaseData
CREATE TABLE BaiCha
(Pname VARCHAR(50),
Prict INT
)
先创建一张表,结果如下:
执行以下语句:
USE CaseData
DECLARE @NUM INT;
SET @NUM=1
WHILE @NUM<= 10
BEGIN
INSERT INTO BaiCha(Pname,Prict)
SELECT ProductName AS T1,Price AS T2 FROM Dim_Product WHERE [email protected]
SET @NUM = @NUM + 1;
END
结果如下:
我们来查看一下数据:
可以看到,价格小于等于10的产品信息已经插入到目标表中。
例子3:
利用双层循环,计算1到9的平方。
USE CaseData
DECLARE @NUM1 INT,@NUM2 INT ,@NUM3 VARCHAR(10);
SET @NUM1=1;
WHILE @NUM1<= 9
BEGIN
SET @NUM2=1
WHILE @NUM2<[email protected]
BEGIN
SET @[email protected]*@NUM1
SET @[email protected]+1
END
PRINT @NUM3
SET @[email protected]+1
END
结果如下:
小Bug提示:
经常会有小伙伴循环写着写着就懵掉了,执行循环无法结束,也没有结果显示出来,这种情况下,需要对BEGIN里面的增量SET进行检测,看是否是由于未设置增量导致。
这里是白茶,一个PowerBI的初学者。
边栏推荐
- LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
- 2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
- Win Go development kit installation configuration, GoLand configuration
- AntPathMatcher使用
- swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
- LeetCode刷题日记:153、寻找旋转排序数组中的最小值
- "NetEase Internship" Weekly Diary (3)
- Handwritten Blog Platform ~ Day Two
- Data transfer at the data link layer
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
猜你喜欢
typescript31-any类型
3. Bean scope and life cycle
HSDC is related to Independent Spanning Tree
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
Use baidu EasyDL implement factory workers smoking behavior recognition
十字光标太小怎么调节、CAD梦想画图算量技巧
"NetEase Internship" Weekly Diary (1)
openGauss切换后state状态显示不对
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
随机推荐
手写博客平台~第二天
How to adjust the cross cursor too small, CAD dream drawing calculation skills
2022-08-01 Install mysql monitoring tool phhMyAdmin
数据链路层的数据传输
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
Use baidu EasyDL implement factory workers smoking behavior recognition
PHP uses PHPRedis and Predis
成都openGauss用户组招募啦!
Data transfer at the data link layer
CodeTon Round 2 D. Magical Array
swift project, sqlcipher3 -> 4, cannot open legacy database is there a way to fix it
手写一个博客平台~第一天
typescript36-class的构造函数实例方法
swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
LeetCode brush diary: LCP 03. Machine's adventure
Constructor instance method inheritance of typescript38-class (implement)
LeetCode brushing diary: 33. Search and rotate sorted array
2022河南青训联赛第(三)场