ccleebelt
asked on
SQL Query Question
I have one table, dbo.pricing_levels with the following data:
id (pk) item pricing_level_id price
1000 XYZ 1 $10
1000 XYZ 2 0
I want to query to find every item where we have a positive value in with a pricing_leve_id = 1 and a zero value in a pricing_level=2
id (pk) item pricing_level_id price
1000 XYZ 1 $10
1000 XYZ 2 0
I want to query to find every item where we have a positive value in with a pricing_leve_id = 1 and a zero value in a pricing_level=2
1. SELECT DISTINCT A.[id(pk)]
,A.[item]
,A.[pricing_level_id]
,A.[price]
FROM [dbo].[pricing_levels] A CROSS JOIN [dbo].[pricing_levels] B
WHERE A.pricing_level_id < B.pricing_level_id AND A.[price] > 0;
Another Option without the DISTINCT Clause:
2. SELECT A.[id(pk)]
,A.[item]
,A.[pricing_level_id]
,A.[price]
FROM [dbo].[pricing_levels] A CROSS JOIN [dbo].[pricing_levels] B
WHERE A.pricing_level_id < B.pricing_level_id AND A.[price] > 0
GROUP BY A.[id(pk)]
,A.[item]
,A.[pricing_level_id]
,A.[price];
,A.[item]
,A.[pricing_level_id]
,A.[price]
FROM [dbo].[pricing_levels] A CROSS JOIN [dbo].[pricing_levels] B
WHERE A.pricing_level_id < B.pricing_level_id AND A.[price] > 0;
Another Option without the DISTINCT Clause:
2. SELECT A.[id(pk)]
,A.[item]
,A.[pricing_level_id]
,A.[price]
FROM [dbo].[pricing_levels] A CROSS JOIN [dbo].[pricing_levels] B
WHERE A.pricing_level_id < B.pricing_level_id AND A.[price] > 0
GROUP BY A.[id(pk)]
,A.[item]
,A.[pricing_level_id]
,A.[price];
You can do this with a self join.
Hope this helps,
Greg
SELECT *
FROM pricing_levels a INNER JOIN pricing_levels b ON a.[id(pk)] - b.[id(pk)]
WHERE a.pricing_level_id = 1 AND b.pricing_level_id = 2 AND a.price > 0 AND b.price = 0
Hope this helps,
Greg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just as curiosity: care to explain why have you selected this solution and not the others?
Open in new window