TSQL-Compare values of two columns and add 0 or 1 to another column of the same table

GeoSax

I have a table with 3 Columns (Userid, Hits, In&Out).

Userid // Hits // In&Out

12 // 2022-05-2510:12:53.000

12 // 2022-05-2514:17:43.000

17 // 2022-05-2509:11:28.000

17 // 2022-05-2517:07:10.000

18 // 2022-05-2508:54:31.000

18 // 2022-05-2517:01:31.000

I want the Column In&Out to get 0 or 1 after a comparing of the value of Hit column (date&time) according to each UserID and the earlier datetime will be 0 and the latest will be 1

e.g

Userid // Hits // In&Out

12 // 2022-05-25 10:12:53.000 // 0

12 // 2022-05-25 14:17:43.000 // 1

17 // 2022-05-25 09:11:28.000 // 0

17 // 2022-05-25 17:07:10.000 // 1

18 // 2022-05-25 08:54:31.000 // 0

18 // 2022-05-25 17:01:31.000 // 1

P.Salmon

A simple lead test will give the result you asked for

SELECT *,
        CASE WHEN LEAD(HITS) OVER (PARTITION BY ID ORDER BY HITS) IS NULL THEN 1 ELSE 0 END 'IN&OUT'  
FROM T

Null in this case means that there is no later entry for this id.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare aggregate of column with another column of same table

How to get unique values to compare two columns of the same table?

Compare Two Values in Same Column

Need to compare the cell values of same column of a table based on another column value in SQL

How to compare two columns values in the same table PowerBI DAX

Comparing values in two columns in same table and creating new column

Compare values in multiple columns and add a new value in another column in Python

Filtering data values in one column based on another column and then inserting values into different columns in same SQL Table

compare two columns of df and making new column if there values are increasing then to write increasing in other column and same for decreasing

Compare two or more columns values only if another column value is True

How to add two columns of values from grouping two by two values from another column

SQL: combine 2 columns in one table to compare with 1 column in another table

Merge duplicate rows with same values across two columns in my mysql table and add the values in third column

how to compare values of two columns using php in the same table and color them if same value

MySQL query to find if a value of one column in one table is between two values in two columns on another table

SQL Query to compare two columns with one column from another table (and get two values)

How to compare two values of a column if the values in another column in the same rows match

Compare two values of same column in SQL Server

compare data of two columns in same table and if values match print "right" if not print"wrong" using r programming?

Compare two pairs of columns from one dataframe to detect mismatches and show the value from another column in the same row

compare two consecutive values in the same column

Compare two rows (both with different ID) & check if their column values are exactly the same. All rows & columns are in the same table

How to compare dates between rows with same column values except one or two columns?

How to add an array column to a table containing values from another column in the same table

Compare two columns and get the values from another column?

SQL Query to compare two columns with one column equal to a column in another table and second column is not equal to the second column from t2

using php/mysql to compare two tables and add missing columns from table1 to table2

Compare two columns and replace the value in column 2 with NA if same as column 1

Using Outer Apply to Compare 2 columns of one table that match values with 1 column in a different table