Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2019 July SQL Puzzle: SQL Advance Query – Generate the range of data basis on common combination

SQL Puzzle: SQL Advance Query – Generate the range of data basis on common combination

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to generate the report on range basis on the common combination of SendFlag and ReceiveFlag.

Input Data:

1
2
3
4
5
6
7
8
9
10
ID CreationDate SendFlag ReceiveFlag
----------- ------------ -------- -----------
1 2016-08-08 0 0
2 2016-10-01 0 0
3 2016-11-26 1 0
4 2016-12-26 1 0
5 2017-01-08 1 0
6 2017-03-10 1 1
7 2017-06-20 1 1
8 2017-08-15 0 1

Expected Output:

1
2
3
4
5
6
StartID EndID SendFlag ReceiveFlag
----------- ----------- -------- -----------
1 2 0 0
3 5 1 0
6 7 1 1
8 8 0 1

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CREATE TABLE Flags
(
ID INT
,CreationDate DATE
,SendFlag BIT
,ReceiveFlag BIT
)
GO
INSERT INTO Flags
VALUES
(1,'20160808',0,0)
,(2,'20161001',0,0)
,(3,'20161126',1,0)
,(4,'20161226',1,0)
,(5,'20170108',1,0)
,(6,'20170310',1,1)
,(7,'20170620',1,1)
,(8,'20170815',0,1)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
;WITH CTE1 AS
(
SELECT f.*, ROW_NUMBER() OVER (ORDER BY f.ID) rnk
FROM Flags f left JOIN Flags f2 on f.id + 1 = f2.id and f.SendFlag = f2.SendFlag and f.ReceiveFlag = f2.ReceiveFlag
WHERE f2.ID IS NULL
),
CTE2 AS
(
SELECT f2.*,ROW_NUMBER() OVER (ORDER BY f2.ID) rnk
FROM Flags f RIGHT JOIN Flags f2 on f.id = f2.id - 1 and f.SendFlag = f2.SendFlag and f.ReceiveFlag = f2.ReceiveFlag
WHERE f.ID IS NULL
)
SELECT c2.ID as StartID,c1.ID as EndID,c1.SendFlag,c2.ReceiveFlag
FROM CTE1 c1 INNER JOIN CTE2 c2 on c1.rnk = c2.rnk

Please try the different solution for this puzzle and share it via comment...

Jul 15, 2019Anvesh Patel
Database Theory: What is CAP theorem - Consistency, Availability, PartitionPostgreSQL: Create Index on Full Text Search tsvector Data
Comments: 6
  1. Dinesh
    July 17, 2019 at 7:02 am

    –we can get result below method alos.

    Select Min(ID) As FromID,Max(ID) ToID,SendFlag,ReceiveFlag
    From Flags
    Group By SendFlag,ReceiveFlag
    Order By FromID

    ReplyCancel
  2. Samrat
    October 24, 2019 at 6:52 am

    SELECT ISNULL((SELECT MIN(I.ID) FROM dbo.Flags I WHERE I.SendFlag=O.SendFlag AND I.ReceiveFlag=O.ReceiveFlag),0) AS ST,
    ISNULL((SELECT MAX(I.ID) FROM dbo.Flags I WHERE I.SendFlag=O.SendFlag AND I.ReceiveFlag=O.ReceiveFlag),0) AS ED,
    SendFlag,ReceiveFlag
    FROM dbo.Flags O
    GROUP BY SendFlag,ReceiveFlag
    ORDER BY ISNULL((SELECT MIN(I.ID) FROM dbo.Flags I WHERE I.SendFlag=O.SendFlag AND I.ReceiveFlag=O.ReceiveFlag),0)

    ReplyCancel
  3. Hareesh
    January 3, 2020 at 4:30 am

    select min(ID) as startID, Max(ID) as EndID, 0 as SendFlag, 0 as ReceivedFlag
    from Flags
    where SendFlag = 0 and ReceiveFlag = 0
    Union
    select min(ID) as startID, Max(ID) as EndID, 1 as SendFlag, 0 as ReceivedFlag
    from Flags
    where SendFlag = 1 and ReceiveFlag = 0
    union
    select min(ID) as startID, Max(ID) as EndID, 1 as SendFlag, 1 as ReceivedFlag
    from Flags
    where SendFlag = 1 and ReceiveFlag = 1
    Union
    select min(ID) as startID, Max(ID) as EndID, 0 as SendFlag, 1 as ReceivedFlag
    from Flags
    where SendFlag = 0 and ReceiveFlag = 1

    ReplyCancel
  4. Bramhateja Kompala
    February 27, 2020 at 3:02 pm

    SELECT
    SUBSTRING(X.IDs,1,1) StartID,
    CASE WHEN LEN(X.IDs) =1 THEN SUBSTRING(X.IDs,1,1)
    ELSE RIGHT(X.IDs,1)
    END AS EndID,
    SendFlag,
    ReceiveFlag
    FROM
    (
    SELECT STRING_AGG(CAST (ID AS VARCHAR(256)),’,’) IDs, SendFlag, ReceiveFlag FROM Flags GROUP BY SendFlag, ReceiveFlag
    )X
    ORDER BY StartID

    ReplyCancel
  5. BRAMHATEJA
    February 28, 2020 at 11:05 am

    SUBSTRING(X.IDs,1,1) StartID,
    CASE WHEN LEN(X.IDs) =1 THEN SUBSTRING(X.IDs,1,1)
    ELSE RIGHT(X.IDs,1)
    END AS EndID,
    SendFlag,
    ReceiveFlag
    FROM
    (
    SELECT STRING_AGG(CAST (ID AS VARCHAR(256)),’,’) IDs, SendFlag, ReceiveFlag FROM Flags GROUP BY SendFlag, ReceiveFlag
    )X
    ORDER BY StartID

    ReplyCancel
  6. Satish Kumar
    February 29, 2020 at 8:45 am

    SELECT * FROM #Flags
    select
    min(id) as startId,max(id) as EndID,SendFlag,ReceiveFlag
    from
    #Flags
    where SendFlag = 0 and ReceiveFlag = 0
    group by SendFlag,ReceiveFlag

    UNION

    select
    min(id) as startId,max(id) as EndID,SendFlag,ReceiveFlag
    from
    #Flags
    where SendFlag = 1 and ReceiveFlag = 0
    group by SendFlag,ReceiveFlag

    UNION

    select
    min(id) as startId,max(id) as EndID,SendFlag,ReceiveFlag
    from
    #Flags
    where SendFlag = 1 and ReceiveFlag = 1
    group by SendFlag,ReceiveFlag

    UNION

    select
    min(id) as startId,max(id) as EndID,SendFlag,ReceiveFlag
    from
    #Flags
    where SendFlag = 0 and ReceiveFlag = 1
    group by SendFlag,ReceiveFlag

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 15, 2019 6 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, RANGE, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....