[英]sql query to convert one column into two based on three different column values in a same table
[英]SQL query which find different values on one column in the same table
but a few days ago I realized that I need larger scope which was extended and proper value should be .我有下表,其中Description
字段的scope = 10
並且我插入了第一行Description
= 但幾天前我意識到我需要更大的范圍,並且適當的值應該是 。
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)
但它不起作用。
我假設“范圍”是指列的寬度為 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
為了將來參考,您可能不應該在意識到問題后在表中插入額外的新行,而應該使用更新。
要查找您要查找的行,您需要對doc_nr
進行自doc_nr
僅包括那些描述不匹配的行, 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
行,請將上面的SELECT
語句替換為以下內容(如果您要DELETE
描述較短的行,您的實際條件可能會有所不同)。
DELETE b
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE LEN(b.description) < LEN(q.description);
歸功於此處的上述語法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.