Поиск соответствующей строки в группе по ключевому слову - PullRequest
2 голосов
/ 14 июля 2020

Сценарий: рассмотрим ресторан, в котором всякий раз, когда в меню должны быть добавлены новые элементы, они должны быть сначала одобрены или отклонены менеджером ресторана. Элементы, которые отклонены или имеют пустой ответ, хранятся в таблице под названием denials , которая показана ниже.

enter image description here

Upon further examination, it is observed that some entries have a blank response because, for some items, the manager leaves the response as blank and instead adds another row manually and then marks that row as denied.

For example, the manager left blank responses for strawberry cheesecake and raspberry cheesecake and added a new row with the item berry flavored cheesecake and marked it as denied. This manual entry accounts for denials of both strawberry and raspberry cheesecake. We see similar examples in other groups, for instance, double carrot cake and tomato cake are collectively marked as denied by creating a new manual entry row for veggie flavored cake.

The requirement is that for each row with a blank response, we need to find the equivalent item which has a denied response. In order to tackle this problem, we have already created a keyword lookup table called lookup that contains the keyword which needs to be searched for in the item column in order to find the matching item with a denied response.

enter image description here

The final desired output is shown below:

введите описание изображения здесь

Примечание : результирующий совпадающий элемент должен находиться в той же группе. Например, в группе D клюквенный чизкейк имеет пустой ответ. Ключевое слово для поиска клюквенного чизкейка - ягода . Поэтому, если мы проигнорируем столбец group , то соответствующий элемент будет чизкейк со вкусом ягод . Однако область поиска должна находиться в той же группе, т.е. группе D. Поскольку группа D не имеет ни одного элемента, который соответствует ключевому слову поиска ягода , ожидаемый результат будет нет ответа .

Я не могу понять, как получить желаемый результат. Мне нужна помощь с этим.

Ниже приведен сценарий SQL для создания схемы и данных для denails и таблиц поиска :

CREATE TABLE [denials](
    [group] [nvarchar](50) NOT NULL,
    [item] [nvarchar](255) NOT NULL,
    [manager_response] [nvarchar](255) NULL
)

CREATE TABLE [lookup](
    [item] [nvarchar](255) NOT NULL,
    [lookup_keyword] [nvarchar](255) NULL
)

INSERT [denials] ([group], [item], [manager_response]) VALUES ('A', 'lemon cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('A', 'strawberry cheesecake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('A', 'raspberry cheesecake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('A', 'berry flavored cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'apple cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'blueberry cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'orange cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'double carrot cake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'tomato cake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('B', 'veggie flavored cake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('C', 'red grapes cheesecake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('C', 'green grapes cheesecake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('C', 'grape flavored cheesecake', 'denied')
INSERT [denials] ([group], [item], [manager_response]) VALUES ('D', 'cranberry cheesecake', NULL)
INSERT [denials] ([group], [item], [manager_response]) VALUES ('D', 'cinnamon cheesecake', 'denied')

INSERT [lookup] ([item], [lookup_keyword]) VALUES ('strawberry cheesecake', 'berry')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('raspberry cheesecake  ', 'berry')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('double carrot cake', 'veggie')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('tomato cake', 'veggie')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('red grapes cheesecake', 'grape')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('green grapes cheesecake', 'grape')
INSERT [lookup] ([item], [lookup_keyword]) VALUES ('cranberry cheesecake', 'berry')

1 Ответ

1 голос
/ 14 июля 2020

Попробуйте следующее:

SELECT A.[group]
      ,A.[Item]
      ,A.[manager_response]
      ,C.[Item]
FROM [denials] A
LEFT JOIN [lookup] B
    ON A.[item] = B.[item]
LEFT JOIN [denials] C
    ON A.[group] = C.[group]
    AND C.[item] LIKE B.[lookup_keyword] + '%'
    AND B.[item] <> C.[item];

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...