WHERE子句中的逻辑锁

时间:2018-02-07 21:12:49

标签: sql-server tsql join union sql-insert

由于FOCCCODE在POSCODE和DEVCODE之间共享,因此我无法同时调出两者来消除空值以将数据插入名为JOINT的单独表中。 POSCODE和DEVCODE是FK。我知道必须有一个更简单的方法来做到这一点。我花了两周的时间试图制定一个解决方案...感觉我不明白一件事要做到这一点。任何建议都表示赞赏。

表格设置

FUNCCODE | POSCODE | DEVCODE

    11        1       NULL
    12       NULL      1
    13        2       NULL 
    14       NULL      2

该表需要重新排列,然后插入一个名为JOINT的单独表中,该表设置为:

POSCODE | POSFUNCCODE |DEVCODE | DEVFUNCCODE

   1          11          1         12
   2          13          2         14

我的一些尝试XD

每个联接只创建我需要的列中的两个

SELECT 
    dbo.POSITION.POSCODE AS POSCODE, 
    dbo.FUNC.FUNCCODE AS POSFUNCCODE
FROM FUNC
INNER JOIN POSITION ON dbo.POSITION.POSCODE = dbo.FUNC.POSCODE 

UNION ALL

SELECT 
    dbo.DEVICE.DEVCODE AS DEVCODE,
    dbo.FUNC.FUNCCODE AS DEVFUNCCODE
FROM FUNC
INNER JOIN DEVICE ON dbo.DEVICE.DEVCODE = dbo.FUNC.DEVCODE 
ORDER BY 1;

仅插入最后一行值

DECLARE @DATE DATETIME = GETDATE()

DECLARE @PC INT;

SELECT @PC = POSCODE 
FROM func
WHERE poscode != 0 
ORDER BY 1;

DECLARE @FCP INT;

SELECT @FCP = FUNCCODE 
FROM FUNC
WHERE POSCODE != 0 
ORDER BY 1;

DECLARE @DC INT;

SELECT @DC = devcode 
FROM func
WHERE devcode != 0
ORDER BY 1;

DECLARE @FCD INT

SELECT @FCD = FUNCCODE 
FROM FUNC
WHERE DEVCODE != 0
ORDER BY 1

INSERT INTO JOINT (POSCODE, POSFUNCCODE, DEVCODE, DEVFUNCCODE, JOINTTIME,             
JOINTSTATUS)
VALUES (@PC, @FCP, @DC, @FCD, @DATE, 1)

3 个答案:

答案 0 :(得分:0)

如果我对您的问题的理解是正确的,我认为以下查询将是一个解决方案。

INSERT INTO JOINT (POSTCODE, DEVCODE, POSFUNCCODE, DEVFUNCCODE)
SELECT
POSTCODE,
    DEVCODE,
    MAX(CASE
    WHEN POSCODE IS NOT NULL THEN FUNCCODE
    ELSE NULL
END) POSFUNCCODE,
MAX(CASE
    WHEN DEVCODE IS NOT NULL THEN FUNCCODE
    ELSE NULL
END) DEVFUNCCODE
FROM FUNC
GROUP BY POSTCODE,DEVCODE

更好理解后的第二版

INSERT INTO JOINT (POSTCODE, DEVCODE, POSFUNCCODE, DEVFUNCCODE)
SELECT
   t1.POSTCODE,
   t2.DEVCODE,
   t1.POSFUNCCODE,
   t2.DEVFUNCCODE
(SELECT
  POSTCODE,
  MAX(CASE
        WHEN POSCODE IS NOT NULL THEN FUNCCODE
        ELSE NULL
    END) POSFUNCCODE
FROM FUNC
GROUP BY POSTCODE) t1
INNER JOIN
(SELECT
  DEVCODE,
  MAX(CASE
        WHEN DEVCODE IS NOT NULL THEN FUNCCODE
        ELSE NULL
END) DEVFUNCCODE
FROM FUNC
GROUP BY DEVCODE) t2
ON t1.POSTCODE = t2.DEVCODE

答案 1 :(得分:0)

反对我更好的判断,我创建了一个插入正确值的循环....我知道这不是解决这个问题的正确方法,请原谅我,因为我的SQL经验很少。但这会纠正客户端程序中的接口问题。请致电@ user4219031获取指导!

DECLARE @DATE DATETIME = GETDATE()

DECLARE @PC INT = 0;

DECLARE  @FCP INT = 12;

DECLARE @DC INT = 0;

DECLARE @FCD INT = 13

WHILE ( @PC < 739 )
BEGIN 
    INSERT INTO JOINT (POSCODE, POSFUNCCODE, DEVCODE, DEVFUNCCODE, JOINTTIME, JOINTSTATUS)
   VALUES( @PC, @FCP, @DC, @FCD, @DATE, 1)
    SET @PC = @PC + 1
    SET @FCP = @FCP +2 
    SET @DC = @DC + 1
    SET @FCD = @FCD + 2

END 

答案 2 :(得分:0)

编辑:我将查询更改为NULL POSCODEDEVCODE帐户。

SQL Fiddle

MS SQL Server 2017架构设置

CREATE TABLE t ( FUNCCODE int, POSCODE int,  DEVCODE int ) ;

INSERT INTO t (FUNCCODE, POSCODE, DEVCODE)
VALUES 
    ( 11, 1, NULL )
  , ( 12, NULL, 1 )
  , ( 13, 2, NULL )
  , ( 14, NULL, 2 )
  , ( 42, NULL, 1 )
  , ( 77, NULL, 7 )
  , ( 88, NULL, 8 )
  , ( 99, 9, NULL )
; 

创建新表并插入记录

CREATE TABLE ti ( POSCODE int, pos_FUNCCODE int, DEVCODE int, dev_FUNCCODE int ) ;

INSERT INTO ti ( POSCODE, pos_FUNCCODE, DEVCODE, dev_FUNCCODE)
SELECT t1.POSCODE
  , t1.FUNCCODE AS pos_FUNCODE
  , t2.DEVCODE
  , t2.FUNCCODE AS dev_FUNCCODE 
FROM t t1
FULL OUTER JOIN t t2 ON t1.POSCODE = t2.DEVCODE
WHERE t1.POSCODE IS NOT NULL OR t2.DEVCODE IS NOT NULL

;

新表中的内容是什么?

SELECT * FROM ti 

<强> Results

| POSCODE | pos_FUNCCODE | DEVCODE | dev_FUNCCODE |
|---------|--------------|---------|--------------|
|       1 |           11 |       1 |           12 |
|       1 |           11 |       1 |           42 |
|       2 |           13 |       2 |           14 |
|       9 |           99 |  (null) |       (null) |
|  (null) |       (null) |       7 |           77 |
|  (null) |       (null) |       8 |           88 |

<强> ========== ORIGINAL ==========

SQL Fiddle

MS SQL Server 2017架构设置

CREATE TABLE t ( FUNCCODE int, POSCODE int,  DEVCODE int ) ;

INSERT INTO t (FUNCCODE, POSCODE, DEVCODE)
VALUES 
    ( 11, 1, NULL )
  , ( 12, NULL, 1 )
  , ( 13, 2, NULL )
  , ( 14, NULL, 2 )
; 

创建新表并插入记录

CREATE TABLE ti ( POSCODE int, pos_FUNCCODE int, DEVCODE int, dev_FUNCCODE int ) ;

INSERT INTO ti ( POSCODE, pos_FUNCCODE, DEVCODE, dev_FUNCCODE)
SELECT t1.POSCODE
  , t1.FUNCCODE AS pos_FUNCODE
  , t2.DEVCODE
  , t2.FUNCCODE AS dev_FUNCCODE 
FROM t t1
INNER JOIN t t2 ON t1.POSCODE = t2.DEVCODE
WHERE t1.POSCODE IS NOT NULL
;

新表中的内容是什么?

SELECT * FROM ti 

<强> Results

| POSCODE | pos_FUNCCODE | DEVCODE | dev_FUNCCODE |
|---------|--------------|---------|--------------|
|       1 |           11 |       1 |           12 |
|       2 |           13 |       2 |           14 |