当前位置:网站首页>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 incrementsEND
Note:
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;END
The 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;END
The 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]+1END
The 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.
边栏推荐
- The ultra-large-scale industrial practical semantic segmentation dataset PSSL and pre-training model are open source!
- 手写一个博客平台~第一天
- Centos7 install postgresql and enable remote access
- Use baidu EasyDL implement factory workers smoking behavior recognition
- 【 wheeled odometer 】
- Ask God to answer, how should this kind of sql be written?
- 雇用WordPress开发人员:4个实用的方法
- [LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
- 数据链路层的数据传输
- AI target segmentation capability for fast video cutout without green screen
猜你喜欢
Win Go开发包安装配置、GoLand配置
The state status is displayed incorrectly after the openGauss switch
Use baidu EasyDL implement factory workers smoking behavior recognition
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案
【 wheeled odometer 】
MySQL8 下载、启动、配置、验证
AOF重写
MySQL optimization strategy
volatile原理解析
nacos启动报错,已配置数据库,单机启动
随机推荐
nacos startup error, the database has been configured, stand-alone startup
Unable to log in to the Westward Journey
2022-08-01 反思
"NetEase Internship" Weekly Diary (1)
The state status is displayed incorrectly after the openGauss switch
Centos7 install postgresql and enable remote access
2022年NPDP考完多久出成绩?怎么查询?
nacos启动报错,已配置数据库,单机启动
垃圾回收器CMS和G1
Safety (1)
oracle query scan full table and walk index
"NetEase Internship" Weekly Diary (2)
Win Go开发包安装配置、GoLand配置
Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
How to adjust the cross cursor too small, CAD dream drawing calculation skills
Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案
Outsourcing worked for three years, it was abolished...
2022-08-01 Install mysql monitoring tool phhMyAdmin
to-be-read list
Win Go development kit installation configuration, GoLand configuration