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 August SQL Puzzle: SQL Advance Query – Find the Order basis on thier Status and Step

SQL Puzzle: SQL Advance Query – Find the Order basis on thier Status and Step

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

Check the below input data and require output data to find order which step is 0 with status D and for the same order other status are P.

Input Data:

1
2
3
4
5
6
7
8
9
OrderID Step Status
------- ----------- ------
ABC 0 D
ABC 1 P
ABC 2 P
ABC 3 P
XYZ 0 D
XYZ 1 D
EFQ 0 D

Expected Output:

1
2
3
OrderID
-------
ABC

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
CREATE TABLE tbl_Orders
(
OrderID CHAR(5)
,Step INTEGER
,Status CHAR(1)
)
GO
 
INSERT INTO tbl_Orders
VALUES
('ABC', 0, 'D'),
('ABC', 1, 'P'),
('ABC', 2, 'P'),
('ABC', 3, 'P'),
('XYZ', 0, 'D'),
('XYZ', 1, 'D'),
('EFQ', 0, 'D'),
('EFQ', 1, 'D')
GO

Solution 1: Using CTE

1
2
3
4
5
with ctetest as
(
SELECT OrderID,Step,Status FROM tbl_Orders WHERE Step = 0 and Status = 'D'
)
select distinct a.OrderID from ctetest a inner join tbl_Orders b on a.OrderID = b.OrderID and b.Status = 'P'

Solution 2: Using GROUP BY .. Having

1
2
3
4
5
6
SELECT OrderID
FROM tbl_Orders
GROUP BY OrderID
HAVING COUNT(*) =
COUNT(CASE WHEN Step <> 0 AND Status = 'P' THEN 1 ELSE NULL END)
+ COUNT(CASE WHEN Step = 0 AND Status = 'D' THEN 1 ELSE NULL END)

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

Aug 12, 2019Anvesh Patel
SQL Server: How to compare your objects between two DatabasesPostgreSQL: How to Delete all duplicate rows Except one
Comments: 19
  1. Harbeer Kadian
    September 9, 2019 at 10:04 am

    select distinct orderid
    from tbl_orders where orderid in (select orderid from tbl_orders where step = 0 and status = ‘D’)
    group by orderid, status
    having status = ‘P’

    ReplyCancel
  2. Pavan Shetty
    October 22, 2019 at 8:50 am

    ;WITH CTE
    AS
    (
    SELECT * FROM TBL_ORDERS
    WHERE Status=’D’ AND STEP=0
    )
    SELECT DISTINCT ORDERID FROM CTE C WHERE EXISTS (SELECT 1 FROM TBL_ORDERS T WHERE C.ORDERID=T.ORDERID AND T.STATUS=’P’ )

    ReplyCancel
  3. n
    October 22, 2019 at 8:54 am

    Hi
    I think the first solution and also Harbeer Kadian solution in the comments are not good.
    In case for example we have an additional record such as (‘ABC’, 4, ‘D’)
    this solutions will not work
    only Solution 2: Using GROUP BY .. Having is good

    ReplyCancel
  4. Samrat
    October 24, 2019 at 4:25 am

    SELECT DISTINCT OrderID
    FROM #TEMP O
    WHERE Step=0 AND Status=’D’
    AND EXISTS(SELECT 1 FROM #TEMP I WHERE I.OrderID=O.OrderID AND I.Status = ‘P’)

    ReplyCancel
  5. koustav
    October 25, 2019 at 10:55 am

    select orderid from report.tbl_Orders t1 where step=0 and status=’D’
    and exists
    (select 1 from report.tbl_Orders where orderid=t1.orderid and status =’P’)

    ReplyCancel
  6. HARISH
    November 5, 2019 at 2:51 am

    –USING SUBQUERY WITH RANKING FUNCTION
    SELECT ORDERID FROM
    (
    SELECT ORDERID, ROW_NUMBER() OVER(ORDER BY ORDERID) AS ROWNUMBER
    FROM tbl_Orders
    )RESULT
    WHERE ROWNUMBER = 1

    ReplyCancel
  7. Ajay
    November 7, 2019 at 9:14 am

    select orderid from tbl_Orders where step=0 and status=’d’and orderid=’abc’

    ReplyCancel
  8. AK
    November 9, 2019 at 6:08 pm

    WITH cte ([OrderID], Step, Status) AS
    (SELECT [OrderID], Step, [Status] FROM tbl_Orders
    WHERE Step = 0 and [Status] = ‘D’)

    SELECT DISTINCT OrderId FROM tbl_Orders
    WHERE [Status] = ‘P’ and OrderID IN (SELECT orderid from cte)

    ReplyCancel
  9. karthick
    December 2, 2019 at 7:28 pm

    select distinct orderid from orders a
    where a.orderid in (select b.orderid from orders b where status=’p’)

    ReplyCancel
  10. R karthick
    December 3, 2019 at 6:38 pm

    select orderid from orders
    group by orderid
    having count(*) >2

    ReplyCancel
  11. Naveed Anjum
    December 16, 2019 at 12:37 pm

    Hi Anvesh,

    Both solution will only work on given data, but if I add one more status (lets say ‘X’) then “group by” method won’t work.

    suppose i add one more record like below
    INSERT INTO tbl_Orders VALUES (‘ABC’, 4, ‘X’)

    then it “Group by” method will not work,

    while, CTE will work.

    ReplyCancel
  12. Noam
    December 31, 2019 at 6:19 pm

    SELECT orderid
    FROM tbl_orders o
    WHERE step = 0
    AND status = ‘D’
    AND EXISTS
    (
    SELECT 1
    FROM tbl_orders o2
    WHERE o.orderid = o2.orderid
    AND o2.status = ‘P’
    )

    ReplyCancel
  13. Hareesh
    January 2, 2020 at 4:00 pm

    select OrderID from tbl_Orders
    where (Step = 0 and Status = ‘D’) and
    OrderID in
    (select distinct OrderID
    from tbl_Orders
    where Status = ‘P’)

    ReplyCancel
  14. Sri
    February 13, 2020 at 12:08 pm

    SELECT DISTINCT A.OrderID
    FROM(
    select OrderID
    from tbl_Orders a
    where step=0 and status=’D’
    ) A JOIN
    (select OrderID
    from tbl_Orders a
    where status=’P’
    ) B
    ON A.OrderID=B.OrderID

    ReplyCancel
  15. Krupa
    March 11, 2020 at 2:39 pm

    SELECT OrderID FROM tbl_Orders As A where Step =0 AND Status = ‘D’ AND OrderID IN (SELECT B.ORDERID FROM tbl_Orders AS B where B.Status = ‘P’ AND A.OrderID = B.OrderID)

    ReplyCancel
  16. Amit Kumar
    March 21, 2020 at 8:49 am

    SELECT ORDERID FROM tbl_Orders
    GROUP BY ORDERID,step
    HAVING STEP= (SELECT COUNT(ORDERID) FROM tbl_orders
    WHERE STATUS= ‘D’ AND STEP =0)

    ReplyCancel
  17. AMIT KUMAR
    March 21, 2020 at 8:51 am

    SELECT ORDERID FROM tbl_Orders
    GROUP BY ORDERID ,STEP
    HAVING STEP= (SELECT COUNT(ORDERID) FROM tbl_orders
    WHERE STATUS= ‘D’ AND STEP =0)

    ReplyCancel
  18. surekha
    April 22, 2020 at 10:44 am

    Select distinct(OrderId) from tbl_Orders
    Where Orderid in (
    Select OrderId from tbl_Orders
    Where step=0 and Status=’D’)
    and status=’P’

    ReplyCancel
  19. Pranay Bhoyar
    May 10, 2020 at 2:33 pm

    SELECT OrderID FROM #tbl_Orders
    WHERE Step=0 AND Status=’D’
    INTERSECT
    SELECT OrderID FROM #tbl_Orders
    WHERE Step0 AND Status=’P’

    ReplyCancel

Leave a Reply to HARISH Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

August 12, 2019 19 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, 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....