ID  BarCode BoxNo(大箱条码) ProduceNo GoodsID(货号)Meter  UnitID(单位ID)
1   200001  200001          100000    001            34     1
2   200002  200001          100000    001            34     1
3   200003  200001          100001    002            6      1
4   200004  200002          100001    001            43     1上面这样一个表(一条记录就是一个条码,也就是一个货物), 
表结构为: 
ID:关键字段  
BarCode:唯一索引(小盒子条码)  
BoxNo:大箱条码(用来装小盒子的)
ProduceNo:生产制单号
GoodsID:货物ID
Meter:每一件货有多少米
现在我显示的时候:
select count(*) as [件数],  GoodsID as [货号], ProduceNo as [生产制单号],
sum(Meter) as [米数],  Max(TblUnit.UnitName) as [单位]
from Table1
left join TblUnit  on TblUnit.UnitID=Table1.UnitID
group by GoodsID, ProduceNo, ColorID,  UnitID, BoxNo现在是:
件数   货号   生产制单号   米数  颜色  单位多加一列,叫包装情况  (实际上BarCode就是一个小箱,包装的时候是N个小箱[就是N个条码BarCode放在大箱BoxNo里])目的是这样的:
盒数   货号   生产制单号   米数  颜色  单位  包装情况
3      xxx    xxx          xxx   xxx   xxx   2*32+44=108包装情况的意思就是根据group by GoodsID, ProduceNo, ColorID,  UnitID, BoxNo
这个分组的来计算  2是这一条记录有2盒为32M 44是另一盒为44米 加起来为1000米
全写出来就是:2盒*32米+1盒*44米=108米
最后这个包装情况要怎么写?

解决方案 »

  1.   

    ---trycreate function f1(@id varchar(10))
    as
    begin
        declare @cnt int
    select @cnt = 0
    select @cnt=@cnt+count(Meter)*Meter from TblUnit where GoodsID = @id group by Meter
    return @cnt
    endselect count(*) as [件数],  
    GoodsID as [货号], 
    ProduceNo as [生产制单号],
    sum(Meter) as [米数],  
    Max(TblUnit.UnitName) as [单位],
    [包装情况]=dbo.f1(GoodsID)
    from Table1
    left join TblUnit  on TblUnit.UnitID=Table1.UnitID
    group by GoodsID, ProduceNo, ColorID,  UnitID, BoxNo
      

  2.   

    create table Table1
    (
    ID int ,
    BarCode varchar(20),
    BoxNo varchar(20),
    ProduceNo int,
    GoodsID varchar(20),
    Meter int,
    UnitID int
    )
    go
    INSERT INTO Table1
    SELECT 1,'200001','200001',100000,'001',34,1 UNION ALL 
    SELECT 2,'200002','200001',100000,'001',34,1 UNION ALL
    SELECT 3,'200003','200001',100001,'002',6,1 UNION ALL 
    SELECT 4,'200004','200002',100001,'001',43,1 select count(*) as [件数],  GoodsID as [货号], ProduceNo as [生产制单号],
    sum(Meter) as [米数],  '' as [单位],cast(min(Meter) as varchar)+'*'+cast(count(1)as varchar) as 包装
    from Table1
    group by GoodsID, ProduceNo,   UnitID, BoxNo
    --结果
    件数          货号                   生产制单号       米数          单位   包装
    ----------- -------------------- ----------- ----------- ---- -------------------------------------------------------------
    2           001                  100000      68               34*2
    1           001                  100001      43               43*1
    1           002                  100001      6                6*1(3 行受影响)
      

  3.   

    ---下面是例子
    create table Table1
    (
    ID int ,
    BarCode varchar(20),
    BoxNo varchar(20),
    ProduceNo int,
    GoodsID varchar(20),
    Meter int,
    UnitID int
    )
    go
    INSERT INTO Table1
    SELECT 1,'200001','200001',100000,'001',34,1 UNION ALL 
    SELECT 2,'200002','200001',100000,'001',34,1 UNION ALL
    SELECT 3,'200003','200001',100001,'002',6,1 UNION ALL 
    SELECT 4,'200004','200002',100001,'001',43,1 
    create function f1(@id varchar(10))
    returns int
    as
    begin
        declare @cnt int
    select @cnt = 0
    select @cnt=@cnt+count(Meter)*Meter from Table1 where GoodsID = @id group by Meter
    return @cnt
    endselect count(GoodsID) as [件数],  
    GoodsID as [货号], 
    ProduceNo as [生产制单号],
    sum(Meter) as [米数],  
    [包装情况]=dbo.f1(GoodsID)
    from Table1
    group by GoodsID, ProduceNo
      

  4.   

    hrb2008() 的方法好象是错的  我要是有一条group by 后的记录里有大于2盒数量不一样的话 这样的方法应该不行  leo_lesley(leo)的思路可行~  不过函数里的where条件要根据group by后面的字段一样才行,
    还有 我需要的是2*32+44=108 这样的字符串, 都搞定了谢谢楼上所有XD~