Returning rows that contain text from another table - sql

I have two tables Main and Lookup
Main has a column called [Promotion Codes List] that is a string of different promo codes comma separated.
Lookup has a column of all the individual promotion codes called [Promotion Codes].
I am trying to create a view of the Main table that will filter Main table if if Main.[Promotion Codes List] contains a code that is in the Lookup.[Promotion Codes] table.
Main Table sample data
|id|Promotion Codes List|
|--| ------------------ |
|1 | BR-Coll-210731-10903, BR-NFRD-210731-10124 |
|2 | BR-Coll-210731-10903, BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11037, BR-Deal-210731-11275 |
|4 | BR-Coll-210731-11037 |
Lookup Table sample data
|id|Promotion Codes|
|--| ------------- |
|1 | BR-NFRI-251231-04855 |
|2 | BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11402 |
|4 | BR-Deal-210731-11275 |
|5 | BR-Coll-210731-11037 |
Desired output
Main Table
|id|Promotion Codes List|
|--| ------------------ |
|2 | BR-Coll-210731-10903, BR-Deal-210731-11265 |
|3 | BR-Coll-210731-11037, BR-Deal-210731-11275 |
|4 | BR-Coll-210731-11037 |
I started off playing around with
SELECT Promotion Codes List, CASE WHEN EXISTS (SELECT *
FROM dbo.[Lookup] B
WHERE B.[Promotion Codes] = A.Promotion Codes List) THEN 'common' ELSE 'not common' END AS valid FROM dbo.Main AS A
and it works if there is are exact matches in row, but as you can see in the main table there can be multiple br codes in one record so I tried to add a like statement somewhere but couldn't figure it out.
Thanks !

Try below
SELECT Promotion Codes List, CASE WHEN EXISTS (SELECT 1
FROM dbo.[Lookup] B
WHERE A.[Promotion Codes List] like '%'+B.[Promotion Codes]+'%') THEN 'common' ELSE 'not common' END AS valid FROM dbo.Main AS A

Related

Count string occurrences within a list column SQL/Grafana

I have a table in the following format:
| id | tags |
|----|-------------------------|
|1 |['Car', 'Plane', 'Truck']|
|2 |['Plane', 'Truck'] |
|3 |['Car', 'Plane'] |
|4 |['Plane'] |
|5 |['Boat', 'Truck'] |
How can I create a table that gives me the total number of occurrences of each item in all cells of the "tags" column? Items ideally do not include single quotes, but may if necessary.
The resulting table would look like:
| tag | count |
|-------|-------|
| Car | 2 |
| Plane | 4 |
| Truck | 3 |
| Boat | 1 |
The following does not work because it only counts identical "tags" entries rather than comparing list contents.
SELECT u.id, count(u.tags) as cnt
FROM table u
group by 1
order by cnt desc;
I am aware of this near-identical question, but they are using Snowflake/SQL whereas I am using MySQL/Grafana so the accepted answer uses functions unavailable to me.

How to join two queries with count?

I have 3 tables in database:
Table 1: violation
| violation_id | violation_name |
|:-------------:|:--------------:|
|1 | No Parking |
|2 | Speed Contest |
|3 | No Helmet |
Table 2: violators
| violator_id | violation_id |
|:-------------:|:--------------:|
|1 |1 |
|2 |1 |
|3 |3 |
Table 2: previous_violator
| prev_violator_id| violation_id |
|:---------------:|:--------------:|
|1 |1 |
|2 |2 |
|3 |2 |
This view that I want:
| violation_name | Total |
|:-------------:|:--------------:|
|No Parking | 3 |
|Speed Contest | 2 |
|No Helmet | 1 |
I perform this code that joins the violator table and violation:
SELECT *,count(violators.violation_id) as vid
FROM violators
LEFT JOIN violation ON violation.violation_id = violators.violation_id
LEFT JOIN previous_violator ON previous_violator.violator_id = violators.violator_id
WHERE date_apphrehend BETWEEN '$from' AND '$to'
GROUP BY violators.violation_id
My problem is, I want to join the previous violator table that count to the total based on the violation_name.
You can first union all to get them into a single result and then count(*) it. Finally join with Violation to get names. ie:
select violation_name, count(*) as cnt
from (select violation_id from Violators
union all
select violation_id from previous_Violators) tmp
inner join Violation on tmp.violation_id = Violation.violation_id
group by Violation.violation_id, violation_name;
Sample DBFiddle demo.
PS: Sample is in postgreSQL but it would be the same for most backends. You didn't tag your backend.

Send Specific data to specific emails using Laravel8

I Have the following form of data :
Table1
-----------------+----------------+-------------------+----------------+
| id_contrat | numero_contrat | contrat |id_filiale |
-----------------+----------------+-------------------+----------------+
|1 | 20210001 | xxxxxx |2 |
|2 | 20210002 | yyyyyy |1 |
|3 | 20210003 | zzzzzz |2 |
-----------------+-----------------------------------------------------+
Table 2 :
+----------------+-----------------------------------------------------------+
| id_filiale | adm_emails |
+----------------+-----------------------------------------------------------+
|1 | ["email1#gmail.com","email2#gmail.com"] |
|2 | ["email3#gmail.com","email2#gmail.com","email2#gmail.com"]
+----------------------------------------------------------------------------+
Please, if you can help me with a Query, I've been stuck since yesterday, so I'm building a program that sends emails to users, so I'm stuck in the fact that for example the contracts 1 and 3 must be sent to ["email3#gmail.com","email2#gmail.com","email2#gmail.com"] and the contracts 2 must be sent to ["email1#gmail.com","email2#gmail.com"] .
I'm really stuck to how to do that using query builder in Laravel.
I should choose witch contracts to send to which emails according to the id_filiale key.
I did a join with the two tables but how can I choose a specific data to a send to a specific emails.
If you have any idea please help

Select all data with same ID in a table

I just want to know the correct syntax because what I've done is not what I wanted. So here is the sample table on database,
|ID |BOOKS |COURSE
+------+--------+------+
|1 |web book| A |
|1 |java | A |
|2 |php | B |
|2 |html | B |
In my UI, I have this table. I have a button in the column of the table that will trigger a modal to show the borrowed books of the student in that row.
|ID |COURSE|ACTION|
+------+------+------+
|1 |A |SHOW |
|2 |B |SHOW |
Whenever I want to show the borrowed book of ID = 1, the output on modals should be like
|BOOKS |
+--------+
|web book|
|java |
because that's what ID=1 borrowed. Thanks in advance.
My current query:
SELECT book
FROM borrowing_tbl
where ID in (SELECT ID
FROM borrowing_tbl
GROUP BY ID
HAVING COUNT(*) > 1)
it keeps showing all data with duplicated ID
Select all books with ID 1
Select ID
From BOOKS
Where BOOKS.ID = 1
I'm sure you could lay your tables out better
Edit:
SELECT BOOKS.name
FROM BOOKS
JOIN students on students.id = BOOKS.id
WHERE students.courses = 'A';

select multi row inside of a table (not same condition)

I try to explain an issue I have faced nowadays.
Actually I have designed a table in order to track the changes applying by users inside of a depot of NLP engine.
I have two table named Token And Lexeme. each token has an id that directly connect to a row of lexeme table. and always I can find the latest and updated lexemes by looking up to token table.
here is their scheme:
Token Table:
+-----+----------+----------+
| Id | token |LexemeId* |
+-----+----------+----------+
LexemeId refers to a row inside of lexeme table.
Lexeme Table:
+-----+---------------------+-------------+
| Id | some information |UpdatedFrom* |
+-----+---------------------+-------------+
* UpdatedFrom field refers another row inside of Lexeme Table.
Null means there is no more rows related to this token(lexeme).
an example:
Token Table:
+-----+----------+----------+
| 0 | A |4 |
| 1 | B |1 |
+-----+----------+----------+
Lexeme Table:
+-----+----------------------+-------------+
| 0 | A information#1 |NULL |
| 1 | B information |NULL |
| 2 | A information#2 |0 |
| 3 | A information#3 |2 |
| 4 | A information#4 |3 |
+-----+----------------------+-------------+
I hope I could clear the air.
I want to write a store procedure to collect all records related to each token. for example for token 'A', I'm expected to have an array (or data table) looks like this:
+-----+----------------------+-------------+
| id | informations | updated from|
+-----+----------------------+-------------+
| 0 | A information#1 |NULL |
| 2 | A information#2 |0 |
| 3 | A information#3 |2 |
| 4 | A information#4 |3 |
+-----+----------------------+-------------+
anybody has any idea to help me....
my knowledge on sql transcript is summarized to Update, Insert and select statements, not more!
thanks in advanced...
Assuming this is in an RDBMS that supports recursive CTEs, try:
with cte as
(select t.id TokenId, t.token, l.Id, l.SomeInformation, l.UpdatedFrom
from Token t
join Lexeme l on t.LexemeId = l.id
union all
select t.TokenId, t.token, l.Id, l.SomeInformation, l.UpdatedFrom
from cte t
join Lexeme l on t.UpdatedFrom = l.id)
select Id, SomeInformation, UpdatedFrom
from cte
where TokenId=0 /* token = 'A' */
SQLFiddle here.