英文:
Filtering SQL rows based on certain alphabets combination
问题
我有一个列存储来自前端网站的用户输入文本字段。用户可以在其中输入任何类型的文本,但他们还会输入特定的字母组合来表示工作类型 - 例如'dri'。举个例子:
行1:P49384; 打开车辆引擎盖-BO-dri 22/10
行2:P93818; 车辆排气-BO 10/20
行3:P1933; 电池 dri-pu-103/2
行4:P3193; 螺丝刀-pu 423
行5:X939; 座位 bo
行6:P9381-车辆-pu-bo dri
在这种情况下,我想要筛选出仅包含"dri"的行。从示例中可以看出,文本可以以任何顺序出现(用户的行为是他们可以随意输入,不遵循任何格式)。但恒定的是,对于特定的工作类型,他们会输入"dri"。
我知道我可以在SQL Server中简单地使用LIKE
来获取这些行。不幸的是,当我使用这个运算符时,第4行也被包括在内。这是因为"screwdriver"包含"dri"。
在SQL Server中是否有办法可以严格地只获取具有"dri"工作类型的行,同时排除像"screwdriver"这样的单词?
我尝试使用PATINDEX('%[d][r][i]%', column) > 0
,但失败了。
提前感谢。
英文:
I have a column that store user input text field from a frontend website. User can input any kind of text in it, but they will also put in a specific alphabets combination to represent a job type - for example 'dri'. As an example:
Row 1: P49384; Open vehicle bonnet-BO-dri 22/10
Row 2: P93818; Vehicle exhaust-BO 10/20
Row 3: P1933; battery dri-pu-103/2
Row 4: P3193; screwdriver-pu 423
Row 5: X939; seats bo
Row 6: P9381-vehicle-pu-bo dri
In this case, I will like to filter only rows that contain dri
. From the example, you can see the text can be in any order (user behaviour, they will key whatever they like without following any kind of format). But the constant is that for a particular job type, they will put in dri
.
I know that I can simply use LIKE
in SQL Server to get these rows. Unfortunately, row 4 is included inside when I use this operator. This is because screwdriver contains dri
.
Is there any way in SQL Server I can do to strictly only obtain rows that has dri
job type, while excluding words like screwdriver?
I tried to use PATINDEX
but it failed too - PATINDEX('%[d][r][i]%', column) > 0
Thanks in advance.
答案1
得分: 0
你的数据是问题的根本。不幸的是,即使对于非规范化的数据,它似乎也没有可靠/定义的格式,这使得在像T-SQL这样的语言中解析你的数据几乎是不可能的。存在哪些问题?根据原始示例数据,一瞥之下存在以下问题:
- 第一个数据值的分隔符不一致。第1至5行使用分号(
;
),但第6行使用连字符(-
)。 - 最后一个数据值的分隔符不一致。第1、2和4行使用空格(
-
)。 - 内部数据没有使用一致的分隔符。例如:
- 第1行的值为
Open vehicle bonnet-BO-dri
,看起来是值Open vehicle bonnet
,BO
和dri
;所以连字符(-
)是分隔符。 - 第5行有
seats bo
,看起来是值seats
和bo
,所以使用空格( - 第6行中
vehicle
作为自己的值(vehicle-pu-bo-dri
),然而,这意味着Open vehicle bonnet
和Vehicle Exhaust
(分别在第1行和第2行)可能实际上是值Open
,vehicle
和bonnet
,以及Vehicle
和Exhaust
。
- 第1行的值为
老实说,解决方案是修复你的设计。因此,你的表可能应该如下所示:
CREATE TABLE dbo.Job (JobID varchar(6) CONSTRAINT PK_JobID PRIMARY KEY NONCLUSTERED, --NONCLUSTERED 因为它不总是升序的
YourNumericalLikeValue varchar(5) NULL); --显然使用更好的名称
CREATE TABLE dbo.JobTypeCompleted(JobTypeID int IDENTITY (1,1) CONSTRAINT PK_JobTypeID PRIMARY KEY CLUSTERED,
JobID varchar(6) NOT NULL CONSTRAINT FK_JobType_Job FOREIGN KEY REFERENCES dbo.Job (JobID),
JobType varchar(30) NOT NULL); --很可能这实际上是对一个真正的工作类型表的外键
GO
然后,对于几行数据,可以这样插入数据:
INSERT INTO dbo.Job (JobID, YourNumericalLikeValue)
VALUES('P49384','22/10'),
('P9381',NULL);
GO
INSERT INTO dbo.JobTypeCompleted(JobID,JobType)
VALUES('P49384','Open vehicle bonnet'),
('P49384','BO'),
('P49384','dri'),
('P9381','vehicle'),
('P9381','pu'),
('P9381','bo'),
('P9381','dri');
然后,你可以使用简单的查询轻松获取你想要的工作:
SELECT J.JobID,
J.YourNumericalLikeValue
FROM dbo.Job J
WHERE EXISTS (SELECT 1
FROM dbo.JobTypeCompleted JTC
WHERE JTC.JobID = J.JobID
AND JTC.JobType = 'dri');
英文:
Your data is the problem here. Unfortunately even for denormalised data it doesn't appear to have a reliable/defined format, making parsing your data in a language like T-SQL next to impossible. What problems are there? Based on the original sample data, at a glance the following problems exist:
- The first data value's delimiter isn't consistent. Rows 1-5 use a semicolon (
;
), but row 6 uses a hyphen (-
) - The last data value's delimiter isn't consistent. Row 1, 2 & 4 use a space (
-
). - Internal data doesn't use a consistent delimiter. For example:
- Row 1 has a the value
Open vehicle bonnet-BO-dri
, which appears to be the valuesOpen vehicle bonnet
,BO
anddri
; so the hyphen(-
) is the delimiter. - Row 5 has
seats bo
, which appears to be the valuesseats
andbo
, so uses a space ( - The fact that row 6 has
vehicle
as its own value (vehicle-pu-bo-dri
), however, implies thatOpen vehicle bonnet
andVehicle Exhaust
(on rows 1 and 2 respectively) could actually be the valuesOpen
,vehicle
, &bonnet
andVehicle
&Exhaust
respectively.
- Row 1 has a the value
Honestly, the solution is to fix your design. As such, your tables should likely look something like this:
CREATE TABLE dbo.Job (JobID varchar(6) CONSTRAINT PK_JobID PRIMARY KEY NONCLUSTERED, --NONCLUSTERED Because it's not always ascending
YourNumericalLikeValue varchar(5) NULL); --Obviously use a better name
CREATE TABLE dbo.JobTypeCompleted(JobTypeID int IDENTITY (1,1) CONSTRAINT PK_JobTypeID PRIMARY KEY CLUSTERED,
JobID varchar(6) NOT NULL CONSTRAINT FK_JobType_Job FOREIGN KEY REFERENCES dbo.Job (JobID),
JobType varchar(30) NOT NULL); --Must likely this'll actually be a foreign key to an actual job type table
GO
Then, for a couple of your rows, the data would be inserted like so:
INSERT INTO dbo.Job (JobID, YourNumericalLikeValue)
VALUES('P49384','22/10'),
('P9381',NULL);
GO
INSERT INTO dbo.JobTypeCompleted(JobID,JobType)
VALUES('P49384','Open vehicle bonnet'),
('P49384','BO'),
('P49384','dri'),
('P9381','vehicle'),
('P9381','pu'),
('P9381','bo'),
('P9381','dri');
Then you can easily get the jobs you want with a simple query:
SELECT J.JobID,
J.YourNumericalLikeValue
FROM dbo.Job J
WHERE EXISTS (SELECT 1
FROM dbo.JobTypeCompleted JTC
WHERE JTC.JobID = J.JobID
AND JTC.JobType = 'dri');
答案2
得分: -1
你可以在查询中使用像操作符,如 column_name like '%-dri'。这意味着查找以"-dri"结尾的记录。
英文:
You can apply like operator in your query as column_name like '%-dri'. It means find out records that end with "-dri"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论