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
Post a Comment