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 |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-05-20 : 16:27:47
|
| Hello everybody,I have a data like below structure. I need to update Field3 and Field4.On Field3, no. of records where are same value in Field1 and Group by Field2.On field3, no. of records where are same value + Field2 value are different.declare @tbl1 table (ID INT,Field1 INT,Field2 varchar(10),Field3 int,Field4 int )INSERT INTO @tbl1SELECT 1, 1,'AAA',null,nullINSERT INTO @tbl1SELECT 2,1,'AAA', null,nullINSERT INTO @tbl1SELECT 3,1,'BBB',null,nullINSERT INTO @tbl1SELECT 4,1,null,null,nullINSERT INTO @tbl1SELECT 5, 5,'CCC',null,nullINSERT INTO @tbl1SELECT 6,5,'DDD', null,nullINSERT INTO @tbl1SELECT 7,5,'DDD',null,nullINSERT INTO @tbl1SELECT 8,5,'DDD',null,nullOutput Looks like this :1 1 AAA 2 43 1 BBB 1 35 5 CCC 1 46 5 DDD 3 4 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2010-05-20 : 17:58:32
|
| Your output for Field 4 makes no sense and you have no description what it is supposed to be.This gets you somewhat there:select MIN(t1.ID) AS ID, t1.Field1, t1.Field2, COUNT(*) AS Field3FROM @tbl1 AS t1WHERE t1.Field2IS NOT NULLGROUP BY t1.Field1, t1.Field2Output:ID Field1 Field2 Field31 1 AAA 23 1 BBB 15 5 CCC 16 5 DDD 3 |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-05-20 : 19:03:04
|
| Lazerath,Field3 calculation ok, but Field4 makes sense. Look at the row 4 where Field1=1 but Field2=null so, in Field4, it will be Field3's value plus this value.INSERT INTO @tbl1SELECT 4,1,null,null,null |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-05-21 : 07:23:23
|
| Anybody has idea? |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2010-05-21 : 13:38:29
|
| Tara and I are trying to help you but you are not listening. You need to describe in better detail exactly how to calculate Field4 because you have not already done so and your example output for Field4 is non-intuitive and makes no sense to us.For instance, why does the entry for 'BBB' have a Field4 value of 3 while every other entry has a Field4 value of 4? If you can describe that, it will go a long way. |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2010-05-21 : 15:38:20
|
| Sorry guys. I will try to explain what i want.I have a data like below eg. I have to count how many times repetated Field2's record (which is seperated by ; eg. Aaa, Bbb,Ccc ...) where Field1's value has same and Field2 is matching (if Field1 has same but Field2 does not have don't count)--This is for Field3And Field4, count all field1 matching but not matching Field2 vlaue.Also need to count if Field1 has same number but Field2 has null dada.ID Field1 Field2101,101, Aaa;Bbb;Ccc102,101, Xyz103,101,null104,102,Aaa;Xyz105,102,Mno106,102,nullOutputField2Aaa 2 3(in 101 and 104)Bbb 1 3Ccc 1 3Mno 1 3Xyz 2 3(in 102 and 104)In this case Field4 all are 3 because 101 and 102 are 3, 3 records. |
 |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2010-05-21 : 17:44:03
|
| Ok, first of all, why complicate matters by changing your sample data and data structure? Explaining things in terms of your original question will quicken a successful response.Secondly, if you can't even supply a correct example, how can we supply a solution? Your output doesn't match your input given what you say:And Field4, count all field1 matching but not matching Field2 vlaue.Also need to count if Field1 has same number but Field2 has null dada.If that was explicitly true, I'd expect the input would expand to this after you apply a Split or ParseValues function:ID Field1 Field2 101,101, Aaa101,101, Bbb101,101, Ccc102,101, Xyz103,101, null104,102, Aaa104,102, Xyz105,102, Mno106,102, nullWhich means your output should look like this if what you claim for Field4 is true:Field2 Field3 Field4 MyCommentAaa 2 7 (Field1 matches 4 other 101 rows, 3 other 102 rows)Bbb 1 4 (Field1 matches 4 other 101 rows)Ccc 1 4 (Field1 matches 4 other 101 rows)Mno 1 3 (Field1 matches 3 other 102 rows)Xyz 2 7 (Field1 matches 4 other 101 rows, 3 other 102 rows)If you didn't apply a Split or ParseValues function, I would expect this to be the result:Field2 Field3 Field4 MyCommentAaa;Bbb;Ccc 1 2Xyz 1 2Aaa;Xyz 1 2Mno 1 2Not trying to be mean, but I can spend no more time on this. Some would say I've spend more time than I should have already. I hope you can rephrase your question properly so someone here can help you further. |
 |
|
|
|
|
|
|
|