Table1
id REQNo Qty
D10 RD780 10
D1A RD786 20
D20 RD34 30
D2A RD34 40
有(1,2)(即)D1和D2的两个相等的子串
对于D1,如果RD780不等于RD786,那么数量将是相同的
对于D2,由于具有相同的RD34,因此sum(Qty)为70
必需的输出
id ReqNo Qty
D10 RD780 10
D1A RD786 20
D20 RD34 70
答案 0 :(得分:1)
create table #t(id varchar(10),reg varchar(10),qty int)
insert into #t values('D10','RD780',10),
('D1A','RD786',20),
('D20','RD34',30),
('D2A','RD34',40)
select min(id),reg,sum(qty) from #t group by reg
答案 1 :(得分:0)
这似乎有效:
declare @t table (id char(3),REQNo varchar(5),Qty int)
insert into @t(id,REQNo,Qty) values
('D10','RD780',10),
('D1A','RD786',20),
('D20','RD34', 30),
('D2A','RD34', 40)
select MIN(id) as id,REQNo,SUM(Qty) as Qty
from @t
group by SUBSTRING(REQNo,1,2),REQNo
对于最终只包含一行的组,MIN(id)
与id
相同,这是有用的。
id REQNo Qty
---- ----- -----------
D20 RD34 70
D10 RD780 10
D1A RD786 20
答案 2 :(得分:0)
SELECT COUNT (*) , First_name, Last_Name, Mobile, Mobile1, Company, Job_title, Tel, Fax, Email, Email1, Country
from dbo.Visitors
where (your where statement) GROUP BY First_name, Last_Name, Mobile, Mobile1, Company, Job_title, Website, Tel, Fax, Email, Email1, Country
HAVING COUNT (*)>1