当前位置:网站首页>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.
边栏推荐
- AWR analysis report questions for help: How can SQL be optimized from what aspects?
- LeetCode刷题日记:53、最大子数组和
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- 『网易实习』周记(三)
- Handwritten Blog Platform ~ Day Two
- LeetCode 213. Robbery II (2022.08.01)
- 记一个gorm初始化的坑
- FOFAHUB使用测试
- Entry name 'org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt' collided
- Simple example of libcurl accessing url saved as file
猜你喜欢
使用docker安装mysql
MySQL8 下载、启动、配置、验证
Analysis of the status quo of digital transformation of manufacturing enterprises
Personal blog system project test
工程师如何对待开源
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
软件测试 接口自动化测试 pytest框架封装 requests库 封装统一请求和多个基础路径处理 接口关联封装 测试用例写在yaml文件中 数据热加载(动态参数) 断言
【 wheeled odometer 】
Win Go开发包安装配置、GoLand配置
手写一个博客平台~第三天
随机推荐
nacos startup error, the database has been configured, stand-alone startup
搜罗汇总的效应
Win Go development kit installation configuration, GoLand configuration
CodeTon Round 2 D. Magical Array
Service discovery of kubernetes
优炫数据库导库导错了能恢复吗?
ALCCIKERS Shane 20191114
Shell Beginners Final Chapter
Speed up your programs with bitwise operations
待读书单列表
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
The underlying data structure of Redis
【 wheeled odometer 】
2022-08-01 反思
LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
Force buckle, 752-open turntable lock
密码学的基础:X.690和对应的BER CER DER编码
用位运算为你的程序加速
2022-07-30 mysql8 executes slow SQL-Q17 analysis
菜刀webshell特征分析