2015-11-06 136 views
-1

我有以下表,其中Description場有scope = 10我插入的第一排,Description = Cobra - Ni但前幾天我才意識到,我需要更大的範圍,其被延長並且適當的值應該是Cobra - NisyorSQL查詢其在同一個表上的一列中找到不同的值

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) 

,但它不工作。

+1

訂單欄在哪裏? –

+0

您專門搜索具有*不同*描述的項目,然後您希望它是相同的? – Luaan

+0

I.e.當同一日期有多行時,你如何選擇第一個/最後一個? – jarlh

回答

1

我假設「範圍」是指列的寬度是10.因此,您正在尋找關聯行,一個長度= 10,另一個以相同的字符串開始,長度> 10。可以使用LEN()函數來獲取字符字段的長度,並使用LEFT()來獲取子字符串 - 後者可用於比較「新」字和「舊」字。

例如:

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 

對於你可能不應該意識到這個問題後,插入額外的新行到表日後參考,並應該使用的更新替代。

0

要找到您要查找的行,您需要在doc_nr上執行self join,僅包括那些描述不匹配的行,SQL Fiddle

 
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行,用下面的(前提是你要DELETE具有較短的描述行,你的實際標準可能會有所不同)取代上述SELECT聲明。

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

對上述語法here的評價。

+0

你的答案肯定是正確的。我在SQL小提琴上檢查過它,它工作。我想我的數據有問題,但我知道你的解決方案也是正確的。 非常感謝SQL小提琴,我以前不知道這個! – Merix

相關問題