在同一表中的一列上查找不同值的SQL查询
我有下表,其中在同一表中的一列上查找不同值的SQL查询,sql,sql-server,Sql,Sql Server,我有下表,其中Description字段有scope=10,我插入了Description=Cobra-Ni的第一行,但几天前我意识到我需要扩展更大的范围,正确的值应该是Cobra-Nisyor 我需要编写查询,从这个示例表中查找第一行和最后一行 我试着这样做: SELECT t1.* FROM table as t1 WHERE t1.Description in (SELECT t2.Description FROM table as t2 WHERE
Description
字段有scope=10
,我插入了Description
=Cobra-Ni
的第一行,但几天前我意识到我需要扩展更大的范围,正确的值应该是Cobra-Nisyor
我需要编写查询,从这个示例表中查找第一行和最后一行
我试着这样做:
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
为便于将来参考,您可能不应该在意识到问题后在表中插入额外的新行,而应该使用更新来代替。要查找您要查找的行,您需要在
文档编号
上执行一次操作,仅包括描述不匹配的行
上述语法的功劳。订购列在哪里?您专门搜索具有不同描述的项目,然后您希望它是相同的?即,当同一日期有多行时,您如何选择第一个/最后一个?您是否在寻找具有相同文档编号但不同描述的行?根据您的要求和提供的数据:
其中文档编号='2000/10'
您的答案肯定是正确的。我在SQLFiddle上检查了它,它正常工作了。我想我的数据有问题,但我知道你的解决方案也是正确的。非常感谢SQL Fiddle,我以前不知道这一点!
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
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 b
FROM basic b
JOIN basic q ON b.doc_nr = q.doc_nr
WHERE LEN(b.description) < LEN(q.description);