当前位置:网站首页>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.
边栏推荐
- 永磁同步电机36问(二)——机械量与电物理量如何转化?
- Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
- [ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
- 2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
- LeetCode brushing diary: 33. Search and rotate sorted array
- Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
- PHP uses PHPRedis and Predis
- BI-SQL丨WHILE
- bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
- 软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
猜你喜欢

Power button 1374. Generate each character string is an odd number

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

Safety (2)

手写一个博客平台~第三天

Ringtone 1161. Maximum In-Layer Elements and

The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string

手写博客平台~第二天

How to adjust the cross cursor too small, CAD dream drawing calculation skills

Garbage Collector CMS and G1

个人博客系统项目测试
随机推荐
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
记一个gorm初始化的坑
垃圾回收器CMS和G1
Handwritten Blog Platform ~ Day Two
使用DBeaver进行mysql数据备份与恢复
优炫数据库导库导错了能恢复吗?
Chengdu openGauss user group recruit!
Coding Experience Talk
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
Data transfer at the data link layer
编码经验之谈
The underlying data structure of Redis
C language inserted into the characters of simple exercises
messy website
2022-08-01 mysql/stoonedb慢SQL-Q18分析
Moonbeam and Project integration of the Galaxy, bring brand-new user experience for the community
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言