SQL查询,在同一个表中的一列上查找不同的值

时间:2015-11-06 15:10:32

标签: sql sql-server

我有下表Description字段scope = 10,我插入第一行Description = Cobra - Ni 但几天前我意识到我需要更大的扩展范围,适当的值应该是 Cobra - Nisyor

Column_ref     Description     Date          Money     Doc_nr
123            Cobra - Ni      06-11-2015    505.50    2000/10
123            Cobra - Toung   07-11-2015    505.50    2000/12
123            Cobra - Brain   07-11-2015    505.50    2000/25
123            Cobra - Nisyor  07-11-2015    505.50    2000/10

我需要编写查询,查找此示例表中的第一行和最后一行。

我试过这样的方式:

SELECT t1.*
FROM table as t1
WHERE t1.Description in
      (SELECT t2.Description
       FROM table as t2
       WHERE t1.Doc_nr = t2.Doc_nr
       AND t1.Description != t2.Description)

但它不起作用。

2 个答案:

答案 0 :(得分:1)

我假设“范围”你的意思是的宽度是10.因此你想要关联行,一个长度= 10,另一个以相同的字符串开头并且有长度> 10.我们可以使用LEN()函数来获取字符字段的长度,使用LEFT()来获取子字符串 - 后者我们可以使用它来比较“new”和“old”

例如:

with oldRows as (
    select *
    from myTable
    where LEN(Description) = 10
), newRows as (
    select *, LEFT(Description, 10) as oldKey
    from myTable
    where LEN(Description) > 10
)
select n.*, o.*
from oldRows o
join newRows n on o.Description = n.oldKey
-- Of course add any other comparisons you need to correlate rows:
--    and o.Column_ref = n.Column_ref
--    and o.[Date] = n.[Date]
--    and o.[Money] = n.[Money]
--    and o.Doc_nr = n.Doc_nr

为了将来参考,您可能不应该在意识到问题后在表中插入额外的新行,而应该使用更新。

答案 1 :(得分:0)

要找到您要查找的行,您需要在doc_nrself join进行SQL Fiddle,仅包括描述不匹配的行here

CREATE TABLE basic
(
  column_ref INT,
  description VARCHAR(30),
  dateField DATETIME,
  amount DECIMAL(12,2),
  doc_nr VARCHAR(30)
);

INSERT INTO basic (column_ref, description, dateField, amount, doc_nr)
VALUES (123, 'Cobra - Ni', '06/11/2015',505.50,'2000/10'),
       (123, 'Cobra - Toung', '07/11/2015',505.50,'2000/12'),
       (123, 'Cobra - Brain', '07/11/2015',505.50,'2000/25'),
       (123, 'Cobra - Nisyor', '07/11/2015',505.50,'2000/10');

SELECT *
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE b.description != q.description

╔════════════╦════════════════╦════════════════════════╦════════╦═════════╦════════════╦════════════════╦════════════════════════╦════════╦═════════╗
║ column_ref ║  description   ║       dateField        ║ amount ║ doc_nr  ║ column_ref ║  description   ║       dateField        ║ amount ║ doc_nr  ║
╠════════════╬════════════════╬════════════════════════╬════════╬═════════╬════════════╬════════════════╬════════════════════════╬════════╬═════════╣
║        123 ║ Cobra - Ni     ║ June, 11 2015 00:00:00 ║ 505.5  ║ 2000/10 ║        123 ║ Cobra - Nisyor ║ July, 11 2015 00:00:00 ║ 505.5  ║ 2000/10 ║
║        123 ║ Cobra - Nisyor ║ July, 11 2015 00:00:00 ║ 505.5  ║ 2000/10 ║        123 ║ Cobra - Ni     ║ June, 11 2015 00:00:00 ║ 505.5  ║ 2000/10 ║
╚════════════╩════════════════╩════════════════════════╩════════╩═════════╩════════════╩════════════════╩════════════════════════╩════════╩═════════╝

为了实际DELETE行,请使用以下内容替换上面的SELECT语句(如果您想要DELETE具有较短描述的行,则您的实际条件可能会有所不同)。

DELETE b 
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE LEN(b.description) < LEN(q.description);

归功于上述语法heap