|
gfaryd
Starting Member
27 Posts |
Posted - 2011-04-07 : 05:42:31
|
| Dear All I have following table structure in Sql server 2000Create table test(pid varchar(10),sysid integer,remarks varchar(10),total integer)sample datainsert into test('A001',4,null,1)insert into test('A001',4,null,1)insert into test('A001',4,'abc',2)insert into test('A001',5,null,1)insert into test('B001',3,null,1)insert into test('B001',4,null,1)insert into test('B001',4,'abc',2)insert into test('B001',7,'pqr',3)insert into test('B001',7,'pqr',3)insert into test('C001',9,null,3)insert into test('C001',9,null,3)insert into test('C001',5,'abc',4)insert into test('C001',7,'pqr',3)insert into test('C001',7,'pqr',3)insert into test('C001',7,'xyz',3)insert into test('C001',7,null,2)following query give me resutselect pid, sum(count(*) from testgroup by 1A001 5B001 10C001 21however i want resultA001 4B001 7C001 15on following criteriasysid and remarks are related, if sysid and remarks are same thenonly one total should be added in sum otherwise both should be addede.g in insert into test('A001',4,null,1)insert into test('A001',4,null,1)insert into test('A001',4,'abc',2)sum should be 3 not 4 as sysid and remarks are same in row 1 and 2in insert into test('B001',7,'pqr',3)insert into test('B001',7,'pqr',3)only 3 should be added in total sum as sysid and remarks are samein insert into test('C001',7,'pqr',3)insert into test('C001',7,'pqr',3)insert into test('C001',7,'xyz',3)row 1 and 2 are same so total should be 6 not 9any help in query |
|