Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2011-03-21 : 11:15:01
|
| Hi I need to define the relationship field based on ages.1.Family = (2 age) cst.age >18 and 1-4 cst.age < 182.Single = 1 cst.age> 18 plus 1-4 cst.age< 183.Couple = 2 cst.age both > 184.everything else = Individualin my db, if there are 5 customers in a policy(for ex father/mother+ 3 children),then i have 5 rows with same policyid but they have diferent name and agesHere the main question is, 1.How to differntiate only 2 cst.age is 18 + and other 4 is less than 18,if i have a policy which has 5 cst.age 2.How to write a query based on all these condition to satisfied?Any help would be highly appreciated.Many Thanks.Regards,SG |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-03-21 : 11:20:56
|
| You're going to have to provide table structure (CREATE TABLE statement) along with some sample data and the expected output based on that sample. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
satheesh
Posting Yak Master
152 Posts |
Posted - 2011-03-21 : 11:50:50
|
| Hi All,I attched few sample data.Need to update the relationship field like i mentioned.ex dataPolicyidAge Relationship(expected outcome)1000 70 Individual1000 66 Individual1000 70 Individual2000 41 Single Family2000 11 Single Family2000 13 Single Family3000 20 Couple3000 53 CoupleLike this my table will be there.For ex:poliyid 1000 has 3 customer age all are above 18 so the relationship be individual.id -2000 has 1 age 18+ and other 2 age less than 18 so the relationship be single family.like this i need to validate the rule and need to update the relationship field.Hope you understand.ThanksRegards,SG. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-03-21 : 13:20:54
|
| [code]CREATE TABLE myTable99(Policyid int,Age int, Relationship varchar(50))GOINSERT INTO myTable99(Policyid, Age, Relationship) SELECT 1000, 70, 'Individual' UNION ALLSELECT 1000, 66, 'Individual' UNION ALLSELECT 1000, 70, 'Individual' UNION ALLSELECT 2000, 41, 'Single Family' UNION ALLSELECT 2000, 11, 'Single Family' UNION ALLSELECT 2000, 13, 'Single Family' UNION ALLSELECT 3000, 20, 'Couple' UNION ALLSELECT 3000, 53, 'Couple'GOSELECT DISTINCT PolicyId, 'Single Family' AS RelationshipFROM myTable99 oWHERE EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i1 WHERE Age < 18 AND o.PolicyId = i1.PolicyId GROUP BY PolicyId HAVING COUNT(*) <> 0) AND EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i2 WHERE Age > 18 AND o.PolicyId = i2.PolicyId GROUP BY PolicyId HAVING COUNT(*) > 0)UNION ALLSELECT DISTINCT PolicyId, 'Couple' AS RelationshipFROM myTable99 oWHERE NOT EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i1 WHERE Age < 18 AND o.PolicyId = i1.PolicyId GROUP BY PolicyId HAVING COUNT(*) <> 0) AND EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i2 WHERE Age > 18 AND o.PolicyId = i2.PolicyId GROUP BY PolicyId HAVING COUNT(*) = 2)UNION ALLSELECT DISTINCT PolicyId, 'Individual' AS RelationshipFROM myTable99 oWHERE NOT EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i1 WHERE Age < 18 AND o.PolicyId = i1.PolicyId GROUP BY PolicyId HAVING COUNT(*) <> 0) AND EXISTS (SELECT PolicyId, COUNT(*) FROM myTable99 i2 WHERE Age > 18 AND o.PolicyId = i2.PolicyId GROUP BY PolicyId HAVING COUNT(*) > 2)GO DROP TABLE myTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
|
|
|
|
|