当前位置:网站首页>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.
边栏推荐
猜你喜欢

用位运算为你的程序加速

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

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

Win Go开发包安装配置、GoLand配置

『网易实习』周记(二)

Ringtone 1161. Maximum In-Layer Elements and

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域

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

【Unity入门计划】2D Game Kit:初步了解2D游戏组成

垃圾回收器CMS和G1
随机推荐
LeetCode刷题日记:153、寻找旋转排序数组中的最小值
编码经验之谈
2022-08-01 Install mysql monitoring tool phhMyAdmin
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
Handwriting a blogging platform ~ Day 3
手写博客平台~第二天
Data transfer at the data link layer
Can Youxuan database import wrongly be restored?
NIO's Sword
[ORB_SLAM2] SetPose, UpdatePoseMatrices
Force buckle, 752-open turntable lock
MySQL - CRUD operations
记一个gorm初始化的坑
LeetCode Brushing Diary: 74. Searching 2D Matrix
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
LeetCode 213. Robbery II (2022.08.01)
Redis 订阅与 Redis Stream
NIO‘s Sword(牛客多校赛)
2022-07-30 mysql8执行慢SQL-Q17分析