简体   繁体   中英

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..

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:

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."

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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