简体   繁体   English

SSMS SQL-将表中的一行与上一行进行比较-状态更改

[英]SSMS SQL - Compare one row in a table to previous - Status change

I use a table that contains weekly records of opportunities and their respective status. 我使用的表格包含每周机会及其各自状态的记录。 I want to compare the most recent status of an opportunity and the week prior to it (ex: the status changes from Prospecting to Negotiation). 我想比较机会的最新状态及其之前的一周(例如:状态从“正在探查”变为“谈判”)。 I want to take all the those that have a changed value this week compared to the week prior and insert into a new table. 我想将与上周相比本周所有值已更改的那些插入到新表中。 Note: In this table for Week, 0 is this current week, 1 is the previous week, 2, is two weeks prior, etc, etc.. 注意:在该表的“周”中,0是当前周,1是前一周,2是前两周,依此类推。

Ex of Table History: 表历史记录示例:

+--------+---------+-------------+-----+
|Acc_Num | Opp_Num |    Stage    | Week|
+--------+---------+-------------+-----+
|   1    |    1    |     Won     |  0  |
+--------+---------+-------------+-----+
|   1    |    1    | Negotiation |  1  |
+--------+---------+-------------+-----+
|   1    |    1    | Prospecting |  2  |
+--------+---------+-------------+-----+
|   1    |    2    | Prospecting |  0  |
+--------+---------+-------------+-----+
|   1    |    2    | Prospecting |  1  |
+--------+---------+-------------+-----+
|   2    |    1    | Negotiation |  0  |
+--------+---------+-------------+-----+
|   2    |    1    | Prospecting |  1  |
+--------+---------+-------------+-----+

Ex of Goal Table: 例如目标表:

+--------+---------+-------------+-----+
|Acc_Num | Opp_Num |    Stage    | Week|
+--------+---------+-------------+-----+
|   1    |    1    |     Won     |  0  |
+--------+---------+-------------+-----+
|   1    |    1    | Negotiation |  1  |
+--------+---------+-------------+-----+
|   2    |    1    | Negotiation |  0  |
+--------+---------+-------------+-----+
|   2    |    1    | Prospecting |  1  |
+--------+---------+-------------+-----+

I'm stuck on the first step of comparing stages from the previous week. 我停留在比较上周各个阶段的第一步。 This is what I've tried but is not working SQL Code: 这是我尝试过但无法使用的SQL代码:

SELECT *
INTO Goal_Table
FROM (
SELECT  Acc_Num
        ,Opp_Num
        ,Stage
        ,Week
        ,CONCAT(Acc_Num, Opp_Num,Week) AS Unq_ID
        FROM Table_History) as A
Left Join
(SELECT  Acc_Num
        ,Opp_Num
        ,Stage
        ,Week
        ,CONCAT(Acc_Num, Opp_Num,Week) AS Unq_ID
        FROM Table_History) as B
ON A.Unq_ID=B.Unq_ID AND A.Week=B.Week-1

I keep getting an error about Columns names needing to be unique. 我不断收到有关列名需要唯一的错误。 This is the actual error text: "Column names in each table must be unique. Column name 'Acc_Num' in table 'Goal_Table' is specified more than once." 这是实际的错误文本:“每个表中的列名必须是唯一的。表'Goal_Table'中的列名'Acc_Num'被多次指定。

Thank you for any help or guidance 感谢您的帮助或指导

this can be done much easier using analytics functions, which are easier to read and mostly even are faster. 使用分析功能可以更轻松地完成此操作,分析功能更易于阅读,甚至更快。 I am providing an example using t-sql as you are referencing to ssms: 当您引用ssms时,我将提供一个使用t-sql的示例:

create table dbo.Table_History
(
    Acc_Num int,
    Opp_Num int,
    Stage nvarchar(50),
    WeekNumber int
)
insert into dbo.Table_History
values
    (1,1,'won',0),
    (1,1,'Negotiation',1),
    (1,1,'Prospecting',2),
    (1,2,'Prospecting',0),
    (1,2,'Prospecting',1),
    (2,1,'Negotiation',0),
    (2,1,'Prospecting',1)

WITH CompareWeeks AS
(
    SELECT
        Acc_Num AS Acc_Num,
        Opp_Num AS Opp_Num,
        Stage AS Stage_Cur,
        WeekNumber as WeekNumber_cur,
        lag(Stage) over (partition by Acc_Num, Opp_Num order by WeekNumber desc) AS Stage_prev,
        lag(WeekNumber) over (partition by Acc_Num, Opp_Num order by WeekNumber desc) as WeekNumber_prev
    FROM dbo.Table_History
)
SELECT
    Acc_Num,
    Opp_Num,
    Stage_Cur,
    WeekNumber_cur
--INTO dbo.GoalTable
FROM CompareWeeks
WHERE Stage_Cur <> Stage_prev and Stage_prev is not null

please be aware that your example provided above does not seem to be consistent. 请注意,您上面提供的示例似乎不一致。 You are not taking into account the first row for (1,1), but you do for (2,1) even as there is no week before to compare to. 您没有考虑(1,1)的第一行,但考虑了(2,1),即使没有一周可以比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM