EY INTERVIEW QUESTION | Assign GroupID to each record using SQL

 --You are presented with a dataset of home listings, each with a unique Home ID and a Status. Your

--objective is to assign a grouping key to each record based on specific conditions. A new grouping key

--should be initiated every time a record has the status “New Listing” or “Relisted”. Each subsequent

--record, following either of these statuses, should inherit the same grouping key until the next

--occurrence of “New Listing” or “Relisted”.


DROP TABLE IF EXISTS HomeListings;

GO


CREATE TABLE HomeListings

(

ListingID  INTEGER PRIMARY KEY,

HomeID     VARCHAR(100),

[Status]     VARCHAR(100)

);

GO


INSERT INTO HomeListings (ListingID, HomeID, [Status]) VALUES 

(1, 'Home A', 'New Listing'),

(2, 'Home A', 'Pending'),

(3, 'Home A', 'Relisted'),

(4, 'Home B', 'New Listing'),

(5, 'Home B', 'Under Contract'),

(6, 'Home B', 'Relisted'),

(7, 'Home C', 'New Listing'),

(8, 'Home C', 'Under Contract'),

(9, 'Home C', 'Closed');

GO



SELECT * FROM HomeListings

Comments

Popular posts from this blog

50 Essential SQL Questions to Land Your Dream Job

How to find all the customers who placed orders on three consecutive days | SQL Scenario questions

ACCENTURE SQL INTERVIEW QUESTION | Change the ProductIDs