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

compare the values in the column Expand / Collapse
Author
Message
Posted Wednesday, October 27, 2010 11:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 25, 2011 11:46 AM
Points: 15, Visits: 49
Hi All,

I have to compare the values in the column of the table which is a resultset of two views.
The table is something like this-

field1 field2
position1 b
position1 b
position2 a
position2 a
position3 a
position3 b

The result should be-
( the logic is-
if position(field1) has all b's the flag should be 2
if position has all a's the flag should be 1
if position has the combination of a and b the flag should be 3

field1 field2 flag
position1 b 2
position1 b 2
position2 a 1
position2 a 1
position3 a 3
position3 b 3

I dont want to create one more view on top of the actual result set which is the combination of views( performance issue.. locking etc.)
I greatly appreciate the help.
Thank you,
Sravan
Post #1011793
Posted Wednesday, October 27, 2010 4:33 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,212, Visits: 3,232
Here is your test data in a readily consumable form, i.e. a CREATE TABLE statement and an INSERT statement. Supplying test data in this form will encourage people to assist you, and therefore tends to lead to a greater number of responses in a shorter time.

CREATE TABLE #t (
field1 varchar(20) NOT NULL,
field2 char(1) NOT NULL
)

INSERT INTO #t(field1, field2)
SELECT 'position1', 'b' UNION ALL
SELECT 'position1', 'b' UNION ALL
SELECT 'position2', 'a' UNION ALL
SELECT 'position2', 'a' UNION ALL
SELECT 'position3', 'a' UNION ALL
SELECT 'position3', 'b'

I'm not sure which version of SQL Server you are using, but the following queries should work on any version of SQL Server from 2000 or later. If the field2 column cannot possibly take any value other than 'a' or 'b', the query could be written as follows:

SELECT T.field1, T.field2, CASE 
WHEN (AGG.CountA = AGG.CountAll) THEN 1
WHEN (AGG.CountB = AGG.CountAll) THEN 2
ELSE 3 END AS Flag
FROM #t AS T INNER JOIN (
SELECT field1,
COUNT(1) AS CountAll,
COUNT(CASE WHEN field2 = 'a' THEN 1 END) AS CountA,
COUNT(CASE WHEN field2 = 'b' THEN 1 END) AS CountB
FROM #t
GROUP BY field1
) AGG ON (T.field1 = AGG.field1)

This second version of the query will return NULL for the Flag column if there is any value of the field2 column in a group (grouped by field1) that is neither 'a' nor 'b'.

SELECT T.field1, T.field2, CASE 
WHEN (AGG.CountA = AGG.CountAll) THEN 1
WHEN (AGG.CountB = AGG.CountAll) THEN 2
WHEN (AGG.CountA + AGG.CountB = AGG.CountAll) THEN 3
ELSE NULL END AS Flag
FROM #t AS T INNER JOIN (
SELECT field1,
COUNT(1) AS CountAll,
COUNT(CASE WHEN field2 = 'a' THEN 1 END) AS CountA,
COUNT(CASE WHEN field2 = 'b' THEN 1 END) AS CountB
FROM #t
GROUP BY field1
) AGG ON (T.field1 = AGG.field1)

Post #1011941
Posted Wednesday, October 27, 2010 6:52 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 25, 2011 11:46 AM
Points: 15, Visits: 49
Thank you very much smith..
It was a great help.
Post #1011962
Posted Thursday, October 28, 2010 12:49 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, March 25, 2011 11:46 AM
Points: 15, Visits: 49
I have one more question regarding this..

Can we store the result set in a view.
If yes how and when will the temp table is dropped.

Please help.

Thank you,
Sravan
Post #1012588
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse