如何编写SQL脚本来更新条件数据和约束?

时间:2018-02-27 10:44:45

标签: sql sql-server syntax sql-update

我刚接触过SQL并且没有解决方案来执行此操作。 我有2个表A和B:

A                B 
ID (char)        Year (char)
Zone (char)      Code (char)
ZCode (char)     

首先,表B将完全为空。表A的Ex数据:

A                
01  A  2013/AA   
02  A  2018/KK
03  A  null
04  B  
05  B  2016/HH

我想将表A中的数据更新到表B,前提是只有ZCode的Zone有最新年份,而ZCode将用“/”分隔。这是我想要的结果:

B
2018  KK
2016  HH

期待有人给我解决方案来做到这一点。

5 个答案:

答案 0 :(得分:1)

试试这个

foreach($image_definitions as $name => $keys) {
  // Generate the conversion
  $conversion = $this->addMediaConversion($name);
  // Loop through and define the attributes as they are in the config, things like ->width(), ->height()
  foreach($keys as $key => $value) {
    if(is_array($value))
    {
      // LOOKING FOR A MORE ELEGANT WAY TO DO THE BELOW
      switch(count($value)) 
      {
        case 2:
          $conversion->$key($value[0], $value[1]);
          break;
        case 3:
          $conversion->$key($value[0], $value[1], $value[2]);
          break;
        case 4:
          $conversion->$key($value[0], $value[1], $value[2], $value[3]);
          break;
        case 5:
          $conversion->$key($value[0], $value[1], $value[2], $value[3], $value[4]);
          break;
        case 6:
          $conversion->$key($value[0], $value[1], $value[2], $value[3], $value[4], $value[5]);
          break;
      }
    } else 
    {
      $conversion->$key($value);
    }                
  }
}

结果

IF OBJECT_ID('dbo.TableA')IS NOT NULL
  DROP TABLE TableA
IF OBJECT_ID('dbo.TableB')IS NOT NULL
  DROP TABLE TableB
CREATE TABLE TableA (Id INT,Zone VARCHAR(2) ,ZCode VARCHAR(20))
CREATE TABLE TableB ([Year] INT,Code VARCHAR(20))   
GO

INSERT INTO TableA(Id,Zone,ZCode)
SELECT 01,'A','2013/AA' UNION ALL   
SELECT 02,'B','2016/HH' UNION ALL
SELECT 03,'A','2018/KK' 
GO


INSERT INTO TableB

SELECT   [Year]
        ,[Code] 
FROM
(
    SELECT   SUBSTRING(ZCode,0,CHARINDEX('/',ZCode)) As [Year]
            ,SUBSTRING(ZCode,CHARINDEX('/',ZCode)+1,LEN(ZCode)) AS Code 
    FROM TableA
)dt


SELECT * FROM TableB  ORDER BY [Year] DESC

答案 1 :(得分:1)

下面的代码段会提供您想要的输出,现在根据您的要求,您可以INSERT进入tableB或执行UPDATE

DECLARE @A TABLE(ID CHAR(10), ZONE CHAR(10), ZCODE CHAR(20))
INSERT INTO @A VALUES
('01',  'A',  '2013/AA'),   
('02',  'B',  '2016/HH'),
('03',  'A',  '2018/KK')

SELECT Year,Code FROM(
SELECT Year,Code,ROW_NUMBER() OVER (PARTITION BY ZONE ORDER BY Year DESC) rn FROM
(SELECT cast(concat('<x>', REPLACE(ZCODE, '/', '</x><x>'), '</x>') as xml).value('/x[1]','varchar(100)') AS Year,
       cast(concat('<x>', REPLACE(ZCODE, '/', '</x><x>'), '</x>') as xml).value('/x[2]','varchar(100)') AS Code,*
FROM @A WHERE ZCODE IS NOT NULL) t1) t2
WHERE rn = 1;

答案 2 :(得分:1)

如果您的数据一致,这是一个非常简单的解决方案。这仅适用于您的数据始终具有完整年份,例如左侧为2018,右侧仅为2个字符。这更多是对列长度的硬编码,不能看到你不能使用它的原因。 使用Max将按代码选择最新年份

Insert into tableB (Year,Code)
select Max(Left(Columnname,4)) year, 
       Right (columnname,2) Code  from TableA
       where Right (columnname,2) is not null or Right (columnname,2)<> ''
       group by Right (columnname,2)

答案 3 :(得分:1)

当数据完全为空时,您可以使用此查询将数据插入表B

INSERT INTO B ([YEAR], [MONTH])
select
Substring(ZCode,0,charindex('/',ZCode)) BYEAR,
Substring(ZCode,charindex('/',ZCode)+1,LEN(ZCode)-charindex('/',ZCode)) BCode
from A

否则使用可以更新此查询以基于BCode更新表B的记录。

针对NOT NULL和GROUP条件编辑的查询

select MAX(v.BYEAR), v.BCode from
(select
 Substring(ZCode,0,charindex('/',ZCode)) BYEAR
,Substring(ZCode,charindex('/',ZCode)+1,LEN(ZCode)-charindex('/',ZCode)) BCode
from B ) v
Where v.BCODE IS NOT NULL 
Group by v.BCODE

答案 4 :(得分:1)

要在表格B中UPDATE,您需要JOIN Year / Code列上的表格WITH CTE AS ( SELECT left(a.zcode, 4) year, substring(a.zcode, charindex('/', a.zcode)+1, len(a.zcode)) code FROM tableA a INNER JOIN ( select Zone, max(left(zcode, 4)) year FROM tableA GROUP BY Zone )b ON a.Zone = b.zone and b.year = left(a.zcode, 4) ) SELECT * FROM CTE

{{1}}