SQL查询可根据过滤器将数据传输到另一个表

问题描述

目前,我将数据存储在下表中:

#Tbl_A

Id  Customer_Id     Indicator
1   912538132       1  
2   912538132       2
3   912538132       3
4   912538132       1
5   912538132       1
6   912538132       2
7   912538132       1
8   912538132       2
9   912538132       3
10  912538132       4   

在这里,我需要通过应用以下过滤器将上述#Tbl_A数据传输到#Tbl_B表中:指标应大于或等于3。

将数据加载到#Tbl_B 后,

EXAMPLE-DATA

#Tbl_B

Id  Customer_Id     Indicator
1   912538132       1  
2   912538132       2
3   912538132       3

7   912538132       1
8   912538132       2
9   912538132       3
10  912538132       4

注意:由于指示符序列不大于3,因此其余数据不应插入#Tbl_B。

我尝试使用BETWEENIN运算符。但是我在#Tbl_B中得到了意外的结果。

解决方法

这是一个空白问题,您需要构建按顺序排列的相邻记录组。然后,您要过滤掉不包含指标3的组。

如果1总是递增insert into #tableb (id,customer_id,indicator) select id,indicator from ( select a.*,max(case when indicator = 3 then 1 else 0 end) over(partition by customer_id,id - indicator) has_3 from #tablea a ) a where has_3 = 1 而没有间隔,那么我们可以使用其值和增量之间的差来构建组:

id

否则,您可以使用row_number()生成另一个insert into #tableb (id,new_id - indicator) has_3 from ( select a.*,row_number() over(partition by customer_id order by id) new_id from #tablea a ) a ) a where has_3 = 1

   @IBOutlet weak var playerView: VersaPlayerView! @IBOutlet weak var controls: VersaPlayerControls override func viewDidLoad() { super.viewDidLoad() playerView.use(controls: controls) if let url = URL.init(string: "http://rmcdn.2mdn.net/Demo/html5/output.mp4") { let item = VersaPlayerItem(url: url) playerView.set(item: item) } } 
,

这似乎对我有用...

    DROP TABLE IF EXISTS #tableA,#tableB;
CREATE TABLE #tableA  (id INT,customer_id INT,indicator INT) ;
CREATE TABLE #tableB  (id INT,indicator INT) ;
INSERT INTO #tableA
(
    id,indicator
)
VALUES
(1,912538132,1),(2,2),(3,3),(4,(5,(6,(7,(8,(9,(10,4);



INSERT INTO #tableB
(
    id,indicator
)
SELECT id,indicator FROM #tableA WHERE indicator >=3


SELECT * FROM #tableB;

我得到的ID是3,9和10。塔就是您要找的东西,对吗?