当前位置:网站首页>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.
边栏推荐
- 项目后台技术Express
- LeetCode刷题日记:153、寻找旋转排序数组中的最小值
- Chengdu openGauss user group recruit!
- AI目标分割能力,无需绿幕即可实现快速视频抠图
- 优炫数据库导库导错了能恢复吗?
- PHP live source code to achieve simple barrage effect related code
- Garbage Collector CMS and G1
- Shell Beginners Final Chapter
- Effects of Scraping and Aggregation
- Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
猜你喜欢
随机推荐
Redis Subscription and Redis Stream
AOF rewrite
Personal blog system project test
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
NIO's Sword
Redis 持久化 - RDB 与 AOF
Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
2022-08-01 反思
Hash collisions and consistent hashing
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
个人博客系统项目测试
【Unity入门计划】2D Game Kit:初步了解2D游戏组成
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)
The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
永磁同步电机36问(二)——机械量与电物理量如何转化?
Coding Experience Talk
【 wheeled odometer 】
The underlying data structure of Redis
LeetCode 213. Robbery II (2022.08.01)
Analysis of volatile principle







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

