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 2017 October SQL Puzzle: SQL Advance Query – Solve the challenge of DISTINCT

SQL Puzzle: SQL Advance Query – Solve the challenge of DISTINCT

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

Check the below input data and expected output to find a distinct value for column 3 and accordingly move additional data of Column 1 to Column 2.

Input Data:

1
2
3
4
5
6
7
8
Col1 Col2 Col3
----------- ----------- -----------
888 NULL 111
555 NULL 333
777 NULL 444
222 NULL 333
666 NULL 444
999 NULL 111

Expected Output:

1
2
3
4
5
Col1 Col2 Col3
----------- ----------- -----------
222 888 111
666 555 333
999 777 444

Create a table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE tbl_DataColumn
(
Col1 INT
,Col2 INT
,Col3 INT
)
GO
INSERT INTO tbl_DataColumn VALUES
(888,NULL ,111)
,(555,NULL ,333)
,(777,NULL ,444)
,(222,NULL ,333)
,(666,NULL ,444)
,(999,NULL ,111)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
;WITH CTE AS
(
SELECT Col1 , ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) rnk
FROM tbl_DataColumn
),
CTE1 AS
(
SELECT
Col3
,COUNT(*) OVER() as Counts
,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) t1
FROM tbl_DataColumn
GROUP BY Col3
)
,CTE2 AS
(
SELECT
Col1
,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) t2
FROM CTE
WHERE rnk > (SELECT TOP 1 Counts FROM CTE1 )
)
,CTE3 AS
(
SELECT
Col1 Col2
,ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) t3
FROM CTE
WHERE rnk <= (SELECT TOP 1 Counts FROM CTE1 )
)
SELECT
CTE2.Col1
,Col2
,CTE1.Col3
FROM CTE1
INNER JOIN CTE2
ON CTE1.t1 = CTE2.t2
INNER JOIN CTE3
ON CTE2.t2 = CTE3.t3

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

Oct 6, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the range of missing yearsSQL Puzzle: SQL Advance Query - Print comma separated Divisions
Comments: 5
  1. Dinesh I.S
    October 9, 2017 at 1:17 pm

    ;
    WITH C1
    As
    (Select Row_Number()Over(Order By Col3) As SLNo,Col3
    From (Select Distinct Col3
    From TBL_DataColumn) A) ,
    C2
    As
    (Select Row_Number()Over(PARTITION By GSLNo Order By Col1) As SLNo,GSLNo,Col1
    From (Select NTILE(2)Over(Order By Col1) As GSLNo,Col1 From TBL_DataColumn) A )
    Select C2.Col1 As Col2,C3.Col1 As Col3,C1.Col3
    From C1 Inner Join C2 On C1.SLNo=C2.SLNo
    Inner JOin C2 As C3 On C3.SLNo=C1.SLNo
    Where C2.GSLNo=1
    And C3.GSLNo=2

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      October 9, 2017 at 2:29 pm

      Perfect!

      ReplyCancel
  2. Dinesh I.S
    October 9, 2017 at 1:29 pm

    —While Looping We can get Result

    Declare @TableResult Table(Col1 Int,Col2 Int,Col3 Int)
    Delete From @TableResult
    Declare @Amt As Int
    Select @Amt=Min(Col3) From TBL_DataColumn

    While @Amt Is Not Null
    Begin
    Insert Into @TableResult
    (Col1,Col2,Col3)
    Select *,@Amt From (
    Select Row_Number()Over(Order By Col1) As SLNo,Col1
    From TBL_DataColumn Where Col3=@Amt)A
    PIVOT(Max(Col1) For SLNo In([1],[2])) A
    Select @Amt=Min(Col3) From TBL_DataColumn Where Col3>@Amt
    End
    Select * From @TableResult

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      October 9, 2017 at 2:29 pm

      Good!

      ReplyCancel
  3. Marcel Hofman
    March 21, 2019 at 12:47 pm

    ; WITH firstcycle as
    (
    SELECT
    ROW_NUMBER () OVER ( ORDER BY (SELECT 0) ) AS n
    , col3
    , col1 AS newcolumntwo
    , NULL AS newcolumnone
    FROM tbl_DataColumn
    ORDER BY (SELECT 0)
    OFFSET 0 ROWS
    FETCH NEXT (SELECT COUNT(DISTINCT col3) FROM tbl_DataColumn) ROWS ONLY
    )
    ,
    secondcycle AS
    (
    SELECT
    ROW_NUMBER () OVER ( ORDER BY (SELECT 0) ) AS n
    , col3
    , NULL AS newcolumntwo
    , col1 AS newcolumnone
    FROM tbl_DataColumn
    ORDER BY (SELECT 0)
    OFFSET (SELECT COUNT(DISTINCT col3) FROM tbl_DataColumn) ROWS
    FETCH NEXT (SELECT COUNT(DISTINCT col3) from tbl_DataColumn) ROWS ONLY
    )
    SELECT s.newcolumnone AS col1, f.newcolumntwo AS col2, f.col3
    FROM firstcycle f
    LEFT JOIN secondcycle s
    ON s.n -(SELECT COUNT(DISTINCT col3) FROM tbl_DataColumn) = f.n

    ReplyCancel

Leave a Reply to Dinesh I.S Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

October 6, 2017 5 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, DISTINCT, 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....