根据表2中的数据更新表1中的数据

时间:2015-08-05 08:50:08

标签: sql sql-server sql-server-2008

表1:

CODE     KEY      GROUPBY     VALUE      <-- COLUMN

CAT       1       'NULL'       500       <-- DATA
DOG       2       'NULL'       400
RAT       3       'NULL'       300
SUM       4        1,3          
NUT       5       'NULL'       200
SUM2      6        2,5        

TABLE2:

CODE     GROUPBY
SUM        1
SUM        3
SUM2       2 
SUM2       5

我希望使用基于SUM的{​​{1}}来获取GROUPBY数据。

KEYTABLE2的拆分字符串,我将TABLE1值分开,因为我会将其基于groupby

TABLE1中的结果应如下所示:

KEY

3 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @DataSourceOne TABLE
(
    [CODE] VARCHAR(4)
   ,[KEY] TINYINT
   ,[GROUPBY] VARCHAR(8)
   ,[VALUE] SMALLINT
);

INSERT INTO @DataSourceOne ([CODE], [KEY], [GROUPBY], [VALUE])
VALUES ('CAT', '1', NULL, '500')
      ,('DOG', '2', NULL, '400')
      ,('RAT', '3', NULL, '300')
      ,('SUM', '4', '1,3', '')
      ,('NUT', '5', NULL, '200')
      ,('SUM2', '6', '2,5', '');

DECLARE @DataSourceTwo TABLE
(
    [CODE] VARCHAR(4)
   ,[GROUPBY] VARCHAR(8)
);

INSERT INTO @DataSourceTwo ([CODE], [GROUPBY])
VALUES ('SUM', 1)
      ,('SUM', 3)
      ,('SUM2', 2)
      ,('SUM2', 5);

WITH DataSource ([CODE], [VALUE]) AS
(
    SELECT DST.[CODE]
          ,SUM([VALUE])
    FROM @DataSourceTwo DST
    INNER JOIN @DataSourceOne DSO
        ON DSO.[KEY] = DST.[GROUPBY]
    GROUP BY DST.[CODE]
)
SELECT DSO.[CODE]
      ,DSO.[KEY]
      ,DSO.[GROUPBY]
      ,ISNULL(DS.[VALUE], DSO.[VALUE]) AS [VALUE]
FROM @DataSourceOne DSO
LEFT JOIN DataSource DS
    ON DSO.[CODE] = DS.[CODE];

enter image description here

我的想法是计算SUM中的CTE,然后将结果加入第一个表格。

答案 1 :(得分:0)

SQL Fiddle

架构详细信息

create table table1
(code varchar(20),
 codekey integer,
 groupby varchar(20),
 value integer);

 create table table2
 (code varchar(20),
  groupby integer);

  insert into table1 values('CAT',1,NULL,500);
  insert into table1 values('DOG',2,NULL,400);
  insert into table1 values('RAT',3,NULL,300);
  insert into table1 values('SUM',4,'1,3',NULL);
  insert into table1 values('NUT',5,NULL,200);
  insert into table1 values('SUM2',6,'2,5',NULL);

  insert into table2 values('SUM',1);
  insert into table2 values('SUM',3);
  insert into table2 values('SUM2',2);
  insert into table2 values('SUM2',5);

更新查询

update table1
set table1.value=tsum.codesum
from table1 t1a,
(select t2.code, sum(t1b.value) codesum from table1 t1b, table2 t2 where t1b.codekey=t2.groupby group by t2.code) tsum
where t1a.groupby is not null
and t1a.code=tsum.code

<强>输出

| code | codekey | groupby | value |
|------|---------|---------|-------|
|  CAT |       1 |  (null) |   500 |
|  DOG |       2 |  (null) |   400 |
|  RAT |       3 |  (null) |   300 |
|  SUM |       4 |     1,3 |   800 |
|  NUT |       5 |  (null) |   200 |
| SUM2 |       6 |     2,5 |   600 |

虽然通过选择查询很容易只显示所需的输出,但编写更新查询并不容易。 SQL Server中的更新查询不允许两次指定Table1。

答案 2 :(得分:0)

试试这个,

SELECT    A.CODE,
          A.CODEKEY,
          A.GROUPBY,
          CASE
            WHEN A.VALUE IS NULL THEN B.VALUE
            ELSE A.VALUE
          END AS VALUE
FROM      #YOUR_TABLE_1 A
LEFT JOIN (SELECT A.CODE,
                  Sum(B.VALUE) AS VALUE
           FROM   #YOUR_TABLE_2 A
           JOIN   #YOUR_TABLE_1 B ON A.GROUPBY = B.CODEKEY
           GROUP  BY A.CODE) B ON A.CODE = B.CODE