SQL Server SQL - Evaluate Record Over A Partition Using Window Functions

zaq34kh6  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(103)

The below data is at the trip level, a unique trip per row. I'm looking to set a 0 / 1 boolean when the same person has RETURNED to the same location, meaning their first trip to a destination will not have the 1 indicator. Ideally, I can do this with a window function but am open to other thoughts if we cannot compare a value within a partition against all of the other values within partition.

My initial attempt only compared against one record within the partition and won't pick up if they went back to the same Destination on a trip beyond their next one.

My initial attempt was: CASE WHEN Destination = LAG(Destination) OVER (PARTITION BY Person) THEN 1 ELSE 0 END

STARTING DATA EXAMPLE BELOW
| Person | Trip | Destination |
| ------------ | ------------ | ------------ |
| Bob | a | Hawaii |
| Bob | b | Hawaii |
| Bob | c | Florida |
| Kevin | a | Texas |
| Kevin | b | Iowa |
| Tina | a | Arizona |
| Tina | b | Arizona |
| Tina | c | California |
| Tina | d | New Mexico |
| Tina | e | Arizona |
| Tina | e | California |

DESIRED OUTPUT BELOW

PersonTripDestinationSame_Destination
BobaHawaii0
BobbHawaii1
BobcFlorida0
KevinaTexas0
KevinbIowa0
TinaaArizona0
TinabArizona1
TinacCalifornia0
TinadNew Mexico0
TinaeArizona1
TinafCalifornia1
vdgimpew

vdgimpew1#

You can modify your condition to use first_value :

case when 
  destination = First_Value(destination) over(partition by person order by trip rows unbounded preceeding) 
    and trip != First_Value(trip) over(partition by person order by trip rows unbounded preceeding)
then 1 else 0 end

Edit

Based on your addition comments I think this would be simpler to implement using a correlated subquery. Does the following work for you?

select *, case when exists (
    select * from t t2 
    where t2.person = t.person 
      and t2.destination = t.destination 
      and t2.trip < t.trip
) then 1 else 0 end
from t;

相关问题