SELECT distinct tblStock.*, FactNum*isnull(u.WholeMoney1,'0') as je FROM (select a.TypeId,s.SpecificationsId,s.AuditState,s.comodid, s.Specifications,s.SpecificationsOrder,s.UserName,s.usTime,a.ComoditiesType,b.Brand,s.modulbh, (SELECT COUNT(1)  FROM CS_Stock  WHERE CS_Stock.ModelId = s.SpecificationsId) AS countStock,ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock  WHERE CS_Stock.comodBarcode = s.Specifications), 0) AS ProcNum,ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.Specifications and isDel=0 ),0) as SalesNum,(ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock  WHERE CS_Stock.comodBarcode = s.Specifications), 0) -ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.Specifications and isDel=0),0)) as FactNum from ComoditiesType as a ,Brand as b,Specifications as s  where a.TypeId=s.TypeId and b.Brand=s.BrandId and s.Specifications in (select distinct(comodBarcode) from CS_Stock)) tblStock ,CS_Stock u where  tblStock.specifications=u.comodbarcode 
查询结果
1 15 1 NULL 高等数学 0 51aspx 2010-3-11 数学 高等数学 0 54157 36 54121 0
1 15 1 NULL 高等数学 0 51aspx 2010-3-11 数学 高等数学 0 54157 36 54121 2976655
1 16 1 NULL 初中数学 0 51aspx 2010-3-11 数学 初中数学 0 140540 52 140488 13486848
1 22 1 NULL 几何 0 51aspx 2011-9-7 数学 几何 0 74884 39 74845 6062445
2 17 1 NULL 雅思2010 0 51aspx 2010-3-11 英语 雅思2010 0 42176 20 42156 0
2 17 1 NULL 雅思2010 0 51aspx 2010-3-11 英语 雅思2010 0 42176 20 42156 3372480
4 20 1 NULL 科学技术 0 51aspx 2011-9-7 科教 科学技术 0 225565 43 225522 0
4 20 1 NULL 科学技术 0 51aspx 2011-9-7 科教 科学技术 0 225565 43 225522 21650112
5 23 1 NULL 前卫 0 51aspx 2011-9-13 文学 前卫 0 707125 61 707064 0
5 23 1 NULL 前卫 0 51aspx 2011-9-13 文学 前卫 0 707125 61 707064 37474392
8 25 1 NULL 永生 0 51aspx 2011-9-30 小说 永生 0 257999 30 257969 20637520
9 26 1 NULL 锋利的SQLSERVER 0 51aspx 2011-10-9 IT 锋利的SQLSERVER 0 241525 52146 189379 0
9 26 1 NULL 锋利的SQLSERVER 0 51aspx 2011-10-9 IT 锋利的SQLSERVER 0 241525 52146 189379 12688393
9 27 1 NULL ASP.NET.3.5.SP1高级编程(第6版)中文版 0 51aspx 2011-10-9 IT ASP.NET.3.5.SP1高级编程(第6版)中文版 0 442512 193012 249500 47155500
12 29 1 NULL 茅台 0 51aspx 2011-10-26 白酒 茅台 0 28 1 27 0
12 29 1 NULL 茅台 0 51aspx 2011-10-26 白酒 茅台 0 28 1 27 32400
12 30 1 NULL XO 0 51aspx 2011-10-26 白酒 XO 0 10 1 9 0
12 30 1 NULL XO 0 51aspx 2011-10-26 白酒 XO 0 10 1 9 13500

解决方案 »

  1.   

    去掉重复的可以用 distinct 或都group by 
      

  2.   

    ;with f as
    (
    SELECT distinct tblStock.*, FactNum*isnull(u.WholeMoney1,'0') as je FROM (select a.TypeId,s.SpecificationsId,s.AuditState,s.comodid, s.Specifications,s.SpecificationsOrder,s.UserName,s.usTime,a.ComoditiesType,b.Brand,s.modulbh, (SELECT COUNT(1) FROM CS_Stock WHERE CS_Stock.ModelId = s.SpecificationsId) AS countStock,ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock WHERE CS_Stock.comodBarcode = s.Specifications), 0) AS ProcNum,ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.Specifications and isDel=0 ),0) as SalesNum,(ISNULL ((SELECT SUM(ProcNum) FROM CS_Stock WHERE CS_Stock.comodBarcode = s.Specifications), 0) -ISNULL((SELECT sum(number) FROM CS_Sales WHERE comodBarcode = s.Specifications and isDel=0),0)) as FactNum from ComoditiesType as a ,Brand as b,Specifications as s where a.TypeId=s.TypeId and b.Brand=s.BrandId and s.Specifications in (select distinct(comodBarcode) from CS_Stock)) tblStock ,CS_Stock u where tblStock.specifications=u.comodbarcode  
    )
    select * from f t where FactNum=(select min(FactNum) from f where UserName=t.UserName)