当前位置:网站首页>BI - SQL 丨 WHILE
BI - SQL 丨 WHILE
2022-08-02 02:16:00 【PowerBI丨white tea】

WHILE
WHILE, the meaning of when.In the syntax of SQL, it can usually be used to repeatedly execute a certain SQL script.
In layman's terms, when XXX, execute a certain operation, which means a cycle.
Seeing this, friends may have questions, is there a similar operation in PowerBI?
The answer is yes, but we generally perform such operations in PowerQuery, and there are fewer scenarios that require loop processing in DAX.
In SQL, there are many scenarios that need to be processed using loop statements, such as data update or incremental calculation.
Syntax
WHILE conditional judgmentBEGINperform actionSET @[email protected]+1 -- parameter cycle incrementsENDNote:
If two or more WHILE loops are nested, all statements up to the end of the inner loop are run first, and then the execution of the next outer loop resumes.
Use examples
Case data:


There is a database named "CaseData" in the database of the white tea machine.
"Dim_Date" date table, "Dim_Product" product table, "Fact_Sales" sales fact table.
Example 1:
Loop printing numbers, from 1 to 9.
DECLARE @NUM INT;SET @NUM = 1;WHILE @NUM<= 9BEGINPRINT @NUMSET @NUM = @NUM + 1;ENDThe results are as follows:

Note: This operation cannot be performed in PowerBI, and an error will be reported.
Example 2:
Create a table and insert ProductName and Price whose Price is less than or equal to 10 in a loop.
USE CaseDataCREATE TABLE BaiCha(Pname VARCHAR(50),Prict INT)Create a table first, the result is as follows:
Execute the following statement:
USE CaseDataDECLARE @NUM INT;SET @NUM=1WHILE @NUM<= 10BEGININSERT INTO BaiCha (Pname, Prict)SELECT ProductName AS T1,Price AS T2 FROM Dim_Product WHERE [email protected] @NUM = @NUM + 1;ENDThe result is as follows:
Let's take a look at the data:

You can see that the product information whose price is less than or equal to 10 has been inserted into the target table.
Example 3:
Using a double loop, calculate the square of 1 to 9.
USE CaseDataDECLARE @NUM1 INT,@NUM2 INT ,@NUM3 VARCHAR(10);SET @NUM1=1;WHILE @NUM1<= 9BEGINSET @NUM2=1WHILE @NUM2<[email protected] @[email protected]*@NUM1SET @[email protected]+1ENDPRINT @NUM3SET @[email protected]+1ENDThe results are as follows:

Minor bug tips:
There are often small partners who are confused when they write in a loop, the execution cycle cannot be ended, and no results are displayed. In this case, it is necessary to check the incremental SET in BEGIN to see if it is due to failure.Set increment results.


Here is Bai Cha, a beginner in PowerBI.
边栏推荐
- BI-SQL丨WHILE
- PHP uses PHPRedis and Predis
- MySQL optimization strategy
- Check if IP or port is blocked
- 永磁同步电机36问(三)——SVPWM代码实现
- Golang分布式应用之Redis
- Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- to-be-read list
- 记一个gorm初始化的坑
猜你喜欢

【LeetCode每日一题】——103.二叉树的锯齿形层序遍历

软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言

记一个gorm初始化的坑

AWR analysis report questions for help: How can SQL be optimized from what aspects?

Ringtone 1161. Maximum In-Layer Elements and

记一次gorm事务及调试解决mysql死锁

密码学的基础:X.690和对应的BER CER DER编码

MySQL优化策略
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree

数据链路层的数据传输
随机推荐
Service discovery of kubernetes
LeetCode brushing diary: 33. Search and rotate sorted array
十字光标太小怎么调节、CAD梦想画图算量技巧
【 wheeled odometer 】
『网易实习』周记(二)
AntPathMatcher使用
Redis Persistence - RDB and AOF
项目后台技术Express
LeetCode刷题日记:53、最大子数组和
Power button 1374. Generate each character string is an odd number
How to adjust the cross cursor too small, CAD dream drawing calculation skills
垃圾回收器CMS和G1
AWR analysis report questions for help: How can SQL be optimized from what aspects?
2022-08-01 安装mysql监控工具phhMyAdmin
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
2022-08-01 反思
BI-SQL丨WHILE
Handwriting a blogging platform ~ the first day
TKU remembers a single-point QPS optimization (I wish ITEYE is finally back)
手写一个博客平台~第三天