当前位置:网站首页>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.
边栏推荐
- 垃圾回收器CMS和G1
- Ask God to answer, how should this kind of sql be written?
- Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
- 【 wheeled odometer 】
- Check if IP or port is blocked
- "NetEase Internship" Weekly Diary (2)
- LeetCode刷题日记:153、寻找旋转排序数组中的最小值
- 编码经验之谈
- PHP 使用 PHPRedis 与 Predis
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
猜你喜欢

Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings

MySQL8 下载、启动、配置、验证

Hash collisions and consistent hashing

MySQL optimization strategy

Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products

Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒

The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!

Hiring a WordPress Developer: 4 Practical Ways

AOF rewrite

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
随机推荐
2022-08-01 Reflection
2022河南青训联赛第(三)场
2022-08-01 安装mysql监控工具phhMyAdmin
『网易实习』周记(一)
乱七八糟的网站
messy website
bool框架::PosInGrid (const简历:关键点kp, int &posX, int诗句)
Data transfer at the data link layer
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
Simple example of libcurl accessing url saved as file
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
ALCCIKERS Shane 20191114
libcurl访问url保存为文件的简单示例
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
Win Go开发包安装配置、GoLand配置
Chengdu openGauss user group recruit!
Redis Persistence - RDB and AOF
【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
Use baidu EasyDL implement factory workers smoking behavior recognition
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline