Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


find a grouping with at least one row within the group containing a certain value


find a grouping with at least one row within the group containing a certain value

Author
Message
Sha_
Sha_
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 411
Hi all,
How do you find a group where number of rows in the group are gtr than 1 and at least one of the non grouped columns contain at least one row with a certain value?

Example:

tableA has columns name, Type, price

example data:
nameA, typeA, 1.00
nameA, typeB, 2.75
nameB, typeA, 2.00
nameC, typeB, 3.00


In this example, I would need to identify a group on name where the grouping contains at least one row where Type = 'typeB'. The value in price is ultimately what I need when there is a group with count of rows > 1 AND Type = typeB, else the group should just use the price from typeA when there is no type B within a group with count of rows > 1.

--this group I need to identify since nameA contains at least one value of typeB in column Type. When typeB is present in the group, use the price from typeB
nameA, typeA, 1.00
nameA, typeB, 2.75

--this is not a group, and therefore only uses the price from typeA since there is no value of typeB in column Type.
nameB, typeA, 2.00

--this is not a group, and does have a typeB. Use this price.
nameC, typeB, 3.00


The returned result set would only include:
nameA, typeB, 2.75
nameB, typeA, 2.00
nameC, typeB, 3.00

Any help would be appreciated.

Thanks!
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4237 Visits: 6431
Something like this perhaps?


DECLARE @t TABLE (Name VARCHAR(10), [TYPE] VARCHAR(10), Price MONEY)

INSERT INTO @t
SELECT 'nameA', 'typeA', 1.00
UNION ALL SELECT 'nameA', 'typeB', 2.75
UNION ALL SELECT 'nameB', 'typeA', 2.00
UNION ALL SELECT 'nameC', 'typeB', 3.00

;WITH CTE AS (
SELECT Name, [TYPE], Price
,n=ROW_NUMBER() OVER (PARTITION BY Name ORDER BY CASE [TYPE] WHEN 'typeB' THEN 0 ELSE 1 END)
FROM @t
)
SELECT Name, [TYPE], Price
FROM CTE
WHERE n=1





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sha_
Sha_
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 411
Thank you so much!
Sha_
Sha_
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 411
For this,

SELECT retailer_name, blood_type, blood_price,
COUNT(*) OVER (PARTITION BY retailer_name)
AS inventory_cnt
FROM Vampire_Retailers AS VR
WHERE blood_type = 'B';


Wouldn't you instead get nameA, 'B', 2.75, 1 and not nameA, 'B', 2.75, 2 due to the where clause?
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8468 Visits: 18081
Indeed, the query is wrong and it won't take you near to the solution.
I suggest you to avoid that opinion and stay with Dwain's solution.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3937 Visits: 6661
Any of that seems like overkill to me for the specific problem stated. Why not just?:


SELECT
name,
ISNULL(MAX(CASE WHEN type = 'typeB' THEN 'typeB' END), MAX(CASE WHEN type <> 'typeB' THEN type END)) AS type,
ISNULL(MAX(CASE WHEN type = 'typeB' THEN price END), MAX(CASE WHEN type <> 'typeB' THEN price END)) AS price
FROM dbo.tablename
GROUP BY
name



SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4237 Visits: 6431
ScottPletcher (8/28/2012)
Any of that seems like overkill to me


Well, these are the Vampire_Retailers now aren't they?

Loved that one Joe!


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Sha_
Sha_
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 411
Thank you all for helping. The simple and direct answers gave me options for the solution. Thanks again!
Smash125
Smash125
SSC Veteran
SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)SSC Veteran (217 reputation)

Group: General Forum Members
Points: 217 Visits: 1381
--Check this

SELECT Name,TYPE,Price FROM
(SELECT Name, [TYPE], Price
,ROW_NUMBER() OVER (PARTITION BY NAME ORDER BY PRICE DESC) AS N FROM @t) AS A
WHERE N=1
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search