如何将表2中的每种产品不同单价的产量按表1某个操作工的某一产品的某一单价在同产品同价的比例分配,并按操作工分产品分单价汇总。表1
操作工 产品名称    工资单价 分单价统计产量
A    A1 1 300
A    A1 2 100
A    A1 3 200
A    A2 1 100
A    A3 1 300
B    A1 1 400
B    A2 2 200
B    A3 2 300
C    A1 3 100
C    A2 3 200表2产品名称     工资单价 分单价标准产量
A1         1                200
A1         2                200
A1         3                300
A2         1                300
A2         2                100
A2         3                200
A3         1                100
A3         2                200表3操作工 产品名称 工资单价 分单价统计产量 分单价标准产量 分配比率 计件工资
A1         1                200              xxx            XX%       $
A1         2                200              ...            ...     ...
A1         3                300
A2         1                300
A2         2                100
A2         3                200
A3         1                100
A3         2                200







解决方案 »

  1.   

    你的问题我还是没有看太明白
    我想你可能表连接不太熟
    使用内联接
    内联接是用比较运算符比较要联接列的值的联接。在 SQL-92 标准中,内联接可在 FROM 或 WHERE 子句中指定。这是 WHERE 子句中唯一一种 SQL-92 支持的联接类型。WHERE 子句中指定的内联接称为旧式内联接。下面的 Transact-SQL 查询是内联接的一个示例:USE pubs
    SELECT *
    FROM authors AS a INNER JOIN publishers AS p
       ON a.city = p.city
    ORDER BY a.au_lname DESC此内联接称为相等联接。它返回两个表中的所有列,但只返回在联接列中具有相等值的行。下面是结果集:au_id        au_lname  au_fname phone         address          city    
    -----------  --------  -------- ------------  ---------------  --------
    238-95-7766  Carson    Cheryl   415 548-7723  589 Darwin Ln.    Berkeley
    409-56-7008  Bennet    Abraham  415 658-9932  6223 Bateman St.  Berkeleystate zip   contract pub_id pub_name              city     state country
    ----- ----- -------- ------ --------------------- -------- ----- -------
    CA    94705 1        1389   Algodata Infosystems  Berkeley CA    USA    
    CA    94705 1        1389   Algodata Infosystems  Berkeley CA    USA    (2 row(s) affected)在结果集中,city 列出现两次。由于重复相同的信息没有意义,因此可以通过更改选择列表消除两个相同列中的一个。其结果称为自然联接。可以重新表述前面的 Transact-SQL 查询以形成自然联接。例如:USE pubs
    SELECT p.pub_id, p.pub_name, p.state, a.*
    FROM publishers p INNER JOIN authors a
       ON p.city = a.city
    ORDER BY a.au_lname ASC, a.au_fname ASC下面是结果集:pub_id pub_name              state    au_id        au_lname  au_fname
    ------ ---------------       -------- -----------  --------  -------- 1389   Algodata Infosystems  CA       409-56-7008  Bennet    Abraham
    1389   Algodata Infosystems  CA       238-95-7766  Carson    Cherylphone         address          city      state zip   contract
    ---------------  ------------- --------  ----- ----- ---------
    415 658-9932  6223 Bateman St. Berkeley  CA    94705 1
    415 548-7723  589 Darwin Ln.   Berkeley  CA    94705 1(2 row(s) affected)本示例中,publishers.city 没有出现在结果中。使用等号以外的运算符的联接
    也可以联接两个不相等的列中的值。用于内联接的运算符和谓词同样也可用于不相等联接。有关联接中可用的运算符和谓词的更多信息,请参见在表达式中使用运算符和 WHERE。 下面的 Transact-SQL 示例是一个大于 (>) 联接,可用于查找住在 Massachusetts 之后(按字母顺序排列)的州的 New Moon 作家,Massachusetts 是 New Moon Books 的所在地。USE pubs
    SELECT p.pub_name, p.state, a.au_lname, a.au_fname, a.state
    FROM publishers p INNER JOIN authors a
       ON a.state > p.state
    WHERE p.pub_name = 'New Moon Books'
    ORDER BY au_lname ASC, au_fname ASC下面是结果集:pub_name         state   au_lname             au_fname             state 
    ---------------- ------- -------------------- -------------------- ----- 
    New Moon Books   MA    Blotchet-Halls         Reginald             OR
    New Moon Books   MA    del Castillo           Innes                MI
    New Moon Books   MA    Greene                 Morningstar          TN
    New Moon Books   MA    Panteley               Sylvia               MD
    New Moon Books   MA    Ringer                 Albert               UT
    New Moon Books   MA    Ringer                 Anne                 UT(6 row(s) affected)使用不等运算符的联接
    很少使用不等联接 (< >)。通常不等联接只有与自联接同时使用才有意义。例如,可以使用下面的不等 Transact-SQL 联接和自联接查找包含不同价格的两本或多本廉价(低于 $15)书的类别:USE pubs
    SELECT DISTINCT t1.type, t1.price
    FROM titles t1 INNER JOIN titles t2 
       ON t1.type = t2.type
       AND t1.price <> t2.price
    WHERE t1.price < $15 AND t2.price < $15说明  表达式 NOT column_name = column_name 与表达式 column_name < > column_name 等效。
    下面的 Transact-SQL 示例中,使用不等联接和自联接的组合查找 titleauthor 表中的所有行,在该表中有两行或多行具有相同的 title_id 但 au_id 号不同(即一本书有多个作者):USE pubs
    SELECT DISTINCT t1.au_id, t1.title_id
    FROM titleauthor t1 INNER JOIN titleauthor t2 
       ON t1.title_id = t2.title_id
    WHERE t1.au_id <> t2.au_id
    ORDER BY t1.au_id下面是结果集:au_id            title_id
    -----------         --------
    213-46-8915         BU1032
    267-41-2394         BU1111
    267-41-2394         TC7777
    409-56-7008         BU1032
    427-17-2319         PC8888
    472-27-2349         TC7777
    672-71-3249         TC7777
    722-51-5454         MC3021
    724-80-9391         BU1111
    724-80-9391         PS1372
    756-30-7391         PS1372
    846-92-7186         PC8888
    899-46-2035         MC3021
    899-46-2035         PS2091
    998-72-3567         PS2091(15 row(s) affected)
      

  2.   

    使用外联接
    仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行。内联接消除与另一个表中的任何行不匹配的行。而外联接会返回 FROM 子句中提到的至少一个表或视图的所有行,只要这些行符合任何 WHERE 或 HAVING 搜索条件。将检索通过左向外联接引用的左表的所有行,以及通过右向外联接引用的右表的所有行。完整外部联接中两个表的所有行都将返回。Microsoft&reg; SQL Server&#8482; 2000 对在 FROM 子句中指定的外联接使用以下 SQL-92 关键字: LEFT OUTER JOIN 或 LEFT JOIN
    RIGHT OUTER JOIN 或 RIGHT JOIN
    FULL OUTER JOIN 或 FULL JOIN 
    SQL Server 支持 SQL-92 外联接语法,以及在 WHERE 子句中使用 *= 和 =* 运算符指定外联接的旧式语法。由于 SQL-92 语法不容易产生歧义,而旧式 Transact-SQL 外联接有时会产生歧义,因此建议使用 SQL-92 语法。使用左向外联接
    假设在 city 列上联接 authors 表和 publishers 表。结果只显示在出版商所在城市居住的作者(本例中为 Abraham Bennet 和 Cheryl Carson)。若要在结果中包括所有的作者,而不管出版商是否住在同一个城市,请使用 SQL-92 左向外联接。下面是 Transact-SQL 左向外联接的查询和结果:USE pubs
    SELECT a.au_fname, a.au_lname, p.pub_name
    FROM authors a LEFT OUTER JOIN publishers p
       ON a.city = p.city
    ORDER BY p.pub_name ASC, a.au_lname ASC, a.au_fname ASC下面是结果集:au_fname             au_lname                       pub_name          
    -------------------- ------------------------------ ----------------- 
    Reginald             Blotchet-Halls                 NULL
    Michel               DeFrance                       NULL
    Innes                del Castillo                   NULL
    Ann                  Dull                           NULL
    Marjorie             Green                          NULL
    Morningstar          Greene                         NULL
    Burt                 Gringlesby                     NULL
    Sheryl               Hunter                         NULL
    Livia                Karsen                         NULL
    Charlene             Locksley                       NULL
    Stearns              MacFeather                     NULL
    Heather              McBadden                       NULL
    Michael              O'Leary                        NULL
    Sylvia               Panteley                       NULL
    Albert               Ringer                         NULL
    Anne                 Ringer                         NULL
    Meander              Smith                          NULL
    Dean                 Straight                       NULL
    Dirk                 Stringer                       NULL
    Johnson              White                          NULL
    Akiko                Yokomoto                       NULL
    Abraham              Bennet                         Algodata Infosystems
    Cheryl               Carson                         Algodata Infosystems(23 row(s) affected)不管是否与 publishers 表中的 city 列匹配,LEFT OUTER JOIN 均会在结果中包含 authors 表的所有行。注意:结果中所列的大多数作者都没有相匹配的数据,因此,这些行的 pub_name 列包含空值。
      

  3.   

    declare @t1 table(操作工 varchar(4),产品名称 varchar(4),工资单价 int,分单价统计产量 int)
    insert into @t1 select 'A','A1',1,300
    insert into @t1 select 'A','A1',2,100
    insert into @t1 select 'A','A1',3,200
    insert into @t1 select 'A','A2',1,100
    insert into @t1 select 'A','A3',1,300
    insert into @t1 select 'B','A1',1,400
    insert into @t1 select 'B','A2',2,200
    insert into @t1 select 'B','A3',2,300
    insert into @t1 select 'C','A1',3,100
    insert into @t1 select 'C','A2',3,200declare @t2 table(产品名称 varchar(4),工资单价 int,分单价标准产量 int)
    insert into @t2 select 'A1',1,200
    insert into @t2 select 'A1',2,200
    insert into @t2 select 'A1',3,300
    insert into @t2 select 'A2',1,300
    insert into @t2 select 'A2',2,100
    insert into @t2 select 'A2',3,200
    insert into @t2 select 'A3',1,100
    insert into @t2 select 'A3',2,200select 
        a.操作工,
        a.产品名称,
        a.工资单价,
        a.分单价统计产量,
        b.分单价标准产量,
        rtrim(cast(a.分单价统计产量*100.0/b.分单价标准产量 as int))+'%' as 分配比率,
        a.工资单价*a.分单价统计产量 as 计件工资
    from 
        @t1 a,@t2 b 
    where 
        a.产品名称=b.产品名称 and a.工资单价=b.工资单价/*
    操作工  产品名称   工资单价   分单价统计产量   分单价标准产量   分配比率      计件工资        
    ------  --------   ---------  ---------------  ---------------  ------------  ----------- 
    A       A1         1          300              200              150%          300
    A       A1         2          100              200              50%           200
    A       A1         3          200              300              66%           600
    A       A2         1          100              300              33%           100
    A       A3         1          300              100              300%          300
    B       A1         1          400              200              200%          400
    B       A2         2          200              100              200%          400
    B       A3         2          300              200              150%          600
    C       A1         3          100              300              33%           300
    C       A2         3          200              200              100%          600
    */
      

  4.   

    谢谢各位的热心帮助,我先取下来试一下。不过libin_ftsafe(子陌红尘),分配率不应超过100% 咯。是不是我什么地方没有表述清楚。
      

  5.   

    例如:
     将表1中所有为A1且单价为1的产品合计,然后用A员工的A1产品且单价为1的与之相比,再用这个比率去与表2中产品名称为A1且单价为1的相乘,算出A员工A1产品且单位为1的数量作为“标准产量”再算工资。
      

  6.   

    declare @t1 table(操作工 varchar(4),产品名称 varchar(4),工资单价 int,分单价统计产量 int)
    insert into @t1 select 'A','A1',1,300
    insert into @t1 select 'A','A1',2,100
    insert into @t1 select 'A','A1',3,200
    insert into @t1 select 'A','A2',1,100
    insert into @t1 select 'A','A3',1,300
    insert into @t1 select 'B','A1',1,400
    insert into @t1 select 'B','A2',2,200
    insert into @t1 select 'B','A3',2,300
    insert into @t1 select 'C','A1',3,100
    insert into @t1 select 'C','A2',3,200declare @t2 table(产品名称 varchar(4),工资单价 int,分单价标准产量 int)
    insert into @t2 select 'A1',1,200
    insert into @t2 select 'A1',2,200
    insert into @t2 select 'A1',3,300
    insert into @t2 select 'A2',1,300
    insert into @t2 select 'A2',2,100
    insert into @t2 select 'A2',3,200
    insert into @t2 select 'A3',1,100
    insert into @t2 select 'A3',2,200select 
        a.操作工,
        a.产品名称,
        a.工资单价,
        a.分单价统计产量,
        b.分单价标准产量,
        rtrim(cast(a.分单价统计产量*100.0/(select sum(分单价统计产量) from @t1 where 产品名称=a.产品名称 and 工资单价=a.工资单价) as int))+'%' as 分配比率,
        cast(a.分单价统计产量*1.0/(select sum(分单价统计产量) from @t1 where 产品名称=a.产品名称 and 工资单价=a.工资单价) *a.工资单价*a.分单价统计产量 as int)as 计件工资
    from 
        @t1 a,@t2 b 
    where 
        a.产品名称=b.产品名称 and a.工资单价=b.工资单价/*
    操作工  产品名称   工资单价   分单价统计产量   分单价标准产量   分配比率      计件工资        
    ------  --------   ---------  ---------------  ---------------  ------------  ----------- 
    A       A1         1          300              200              42%           128
    A       A1         2          100              200              100%          200
    A       A1         3          200              300              66%           400
    A       A2         1          100              300              100%          100
    A       A3         1          300              100              100%          300
    B       A1         1          400              200              57%           228
    B       A2         2          200              100              100%          400
    B       A3         2          300              200              100%          600
    C       A1         3          100              300              33%           100
    C       A2         3          200              200              100%          600
    */