Tags
Complex SQL Challenges, Complex TSQL Challenge, Data Analysis, Interview Qs.SQL SERVER Questions, Interview Questions on SQL, InterviewQuestions, InterviewQuestions for SQL, Learn complex SQL, Learn SQL, Learn T-SQL, PL/SQL Challenges, Puzzles, Queries for SQL Interview, SQL, SQL 2012, SQL 2014, SQL 2014 Interview Questions, SQL Challenge, SQL Challenges, SQL pl/sql puzzles, SQL Puzzles, SQL Server Data Tools, SQL SERVER Interview questions, SQL SERVER2005/2008, SQL Sudoku, SQLSERVER, SSRS Interview Questions, T SQL Puzzles, T-SQL Challenge, Tough SQL Challenges, Tough SQL Puzzles, TSQL, TSQL Challenge, TSQL Challenges, TSQL Interview questions, TSQL Queries
T-SQL Query | [ The All Products Puzzle ] – In this puzzle we have to two table i.e. SpecialProductMaster table – this table contains master information of the products and Sales Table- This table is used for capture product sales. Here we to find out customers who have purchase all the products mentioned in the SpecialProductMaster Table that appears in Sales table for a single customer. In the below example only customer 345 purchased Maggi and surf so we need 345 only as the output. Please check out the sample input and expected output for details.
Sample Input
SpecialProductMaster
ID | ProductName |
1 | Maggi |
2 | Surf |
Sales
SaleID | ProductName | CustID | SaleDATE |
101 | Maggi | 345 | 30-05-2014 |
201 | Surf | 345 | 30-05-2014 |
302 | Rin | 122 | 30-05-2014 |
303 | TV | 142 | 30-05-2014 |
109 | Parle | 345 | 30-05-2014 |
110 | GoodDay | 346 | 30-05-2014 |
112 | Surf | 346 | 30-05-2014 |
Expected Output
SaleID | ProductName | CustID | SaleDATE |
101 | Maggi | 345 | 30-05-2014 |
201 | Surf | 345 | 30-05-2014 |
Rules/Restrictions
- The solution should be should use “SELECT” statement or “CTE”.
- Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com
Script
Use the below script to generate the source table and fill them up with the sample data.
CREATE TABLE SpecialProductMaster ( ID INT ,ProductName VARCHAR(10) ) GO INSERT INTO SpecialProductMaster(Id,ProductName) VALUES (1,'Maggi'), (2,'Surf') CREATE TABLE Sales ( SaleID INT ,ProductName VARCHAR(10) ,CustID INT ,SaleDATE DATETIME DEFAULT GETDATE() ) GO INSERT INTO Sales(SaleID,ProductName,CustID) VALUES (112,'Surf',346) (109,'Parle',345), (101,'Maggi',345), (201,'Surf',345), (302,'Rin',122), (303,'TV',142) |
UPDATE – 20-Apr-2015 – Solution 1
-- --Solution 1 SELECT s.* FROM ( SELECT Ar.CustID , Ar.ProductCount ,ProductName FROM ( SELECT Ar.CustId , COUNT(*) OVER ( PARTITION BY Ar.CustID ORDER BY (SELECT NULL)) ProductCount , ProductName FROM ( SELECT m.ID,m.ProductName,s.CustID FROM Sales s INNER JOIN SpecialProductMaster m ON s.ProductName = m.ProductName ) Ar ) Ar WHERE ProductCount = ( SELECT COUNT(*) FROM SpecialProductMaster m ) ) Br INNER JOIN Sales s ON s.CustID = Br.CustID AND s.ProductName = Br.ProductName -- |
Add a comment if you have any other solution in mind. We all need to learn.
Keep Learning
Easy approach
WITH CTE_1
AS
(
SELECT SaleID,A.ProductName,CustID,SaleDATE
FROM Sales A INNER JOIN
SpecialProductMaster B ON
A.ProductName=B.ProductName
)
SELECT * FROM CTE_1 WHERE CustID IN
(
SELECT DISTINCT CUSTID FROM
(
SELECT *,
DENSE_RANK()OVER(PARTITION BY CustID ORDER BY ProductName) AS RNK
from Cte_1
) D
WHERE RNK>1
)
LikeLike
;with cte
as
(
select CustID from #SpecialProductMaster a
join #Sales b
on a.ProductName=b.ProductName
group by CustID
having COUNT(a.ProductName)=(select COUNT(*) as count1 from #SpecialProductMaster)
)
select * from cte a
join #Sales b
on a.CustID=b.CustID
where ProductName in (select ProductName from #SpecialProductMaster)
LikeLiked by 1 person
excellent Sanjay
LikeLiked by 1 person
;with cte
as
(
select * from #Sales a
where exists(select * from #SpecialProductMaster b where a.ProductName=b.ProductName)
)
select * from cte a
where exists( select * from cte b where a.CustID=b.CustID and ProductName =’maggi’ )
and exists (select * from cte b where a.CustID=b.CustID and ProductName=’surf’)
LikeLiked by 1 person
;WITH CTE (SaleID, ProductName, CustID, SaleDATE)
AS(
SELECT SaleID, P.ProductName, CustID, SaleDATE
FROM SpecialProductMaster P Join Sales R
ON P.ProductName = R.ProductName
)
SELECT * FROM CTE
WHERE CustID IN (SELECT CustID FROM Sales
WHERE ProductName IN (SELECT ProductName FROM SpecialProductMaster)
GROUP BY CustID
HAVING COUNT(CustID) = (SELECT COUNT(*) FROM SpecialProductMaster)
)
LikeLike