当前位置:网站首页>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刷题日记:LCP 03.机器人大冒险

Ringtone 1161. Maximum In-Layer Elements and

十字光标太小怎么调节、CAD梦想画图算量技巧

LeetCode刷题日记:74. 搜索二维矩阵

Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion

Data transfer at the data link layer

Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas

2022-07-30 mysql8执行慢SQL-Q17分析

nacos startup error, the database has been configured, stand-alone startup

Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
随机推荐
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
The characteristics and principle of typescript29 - enumeration type
PHP uses PHPRedis and Predis
LeetCode brushing diary: 53, the largest sub-array and
typescript36-class的构造函数实例方法
volatile原理解析
搜罗汇总的效应
Constructor of typescript35-class
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
项目后台技术Express
待读书单列表
Win Go开发包安装配置、GoLand配置
Redis 底层的数据结构
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
Hiring a WordPress Developer: 4 Practical Ways
哈希冲突和一致性哈希
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
typescript32-ts中的typeof
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
Coding Experience Talk