Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

best optimized query for the requirement Expand / Collapse
Author
Message
Posted Wednesday, March 20, 2013 6:21 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: 2 days ago @ 7:27 AM
Points: 35,769, Visits: 32,437
foxxo (3/20/2013)
SELECT DISTINCT product_id 
FROM fact_sales
WHERE (agent_id=1 AND product_code in (1))
OR (agent_id=2 AND product_code in (3))
OR (agent_id=3 AND product_code in (2))
OR (agent_id=4 AND product_code in (1))

AND & OR operators
http://www.w3schools.com/sql/sql_and_or.asp


I'm pretty sure the OP wants a return of only those things that meet all 4 pairs of conditions. By itself, OR just isn't going to do it.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1433165
Posted Wednesday, March 20, 2013 6:34 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: 2 days ago @ 7:27 AM
Points: 35,769, Visits: 32,437
Shanmuga Raj (3/20/2013)
Thanks.. its working !
can you let me know why you have used
HAVING COUNT(*) = 4


That's to make it so that only those product_ids that have all 4 distinct product condition pairs are returned.

So far, there are some good answers to this problem, especially Chris' most recent post. My question is, what are you going to do when the conditions change where you might need to meet fewer or more than 4 pairs of conditions? Is this a one off or something where the conditions need to be passed by a GUI or passed by something else?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1433176
Posted Wednesday, March 20, 2013 7:22 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, December 18, 2014 1:43 AM
Points: 6,890, Visits: 14,254
Jeff Moden (3/20/2013)
Shanmuga Raj (3/20/2013)
Thanks.. its working !
can you let me know why you have used
HAVING COUNT(*) = 4


That's to make it so that only those product_ids that have all 4 distinct product condition pairs are returned.

So far, there are some good answers to this problem, especially Chris' most recent post. My question is, what are you going to do when the conditions change where you might need to meet fewer or more than 4 pairs of conditions? Is this a one off or something where the conditions need to be passed by a GUI or passed by something else?


Thanks for picking this up, Jeff.
There's a solution to more than 4 pairs of conditions - depending on how the conditions are raised, as you suggest. Here it is;
;WITH IncludedAgentProducts AS (
SELECT DISTINCT *
FROM (VALUES
(1,'1'),
(2,'3'),
(3,'2'),
(4,'1')
) d (agent_id, product_code)
)
SELECT Product_ID
FROM (
SELECT f.Product_ID, f.agent_id, f.product_code
FROM #fact_sales f
INNER JOIN IncludedAgentProducts d
ON d.agent_id = f.agent_id
AND d.product_code = f.product_code
GROUP BY f.Product_ID, f.agent_id, f.product_code
) d
GROUP BY Product_ID
HAVING COUNT(*) = (SELECT COUNT(*) FROM IncludedAgentProducts)



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1433193
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse