Send Specific data to specific emails using Laravel8 - sql

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

Related

Returning rows that contain text from another table

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

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.

Noob needs advise on three tables in postgresql DB

I've very minor experience at working with databases( I only know the absolute basics). With that stated I guess my problem is rather easy to solve for more experienced minds.
My question is:
I need a way to be able to search for example "all types of Aluminium and all its sub_materials"?
I can do simple queries like
SELECT *
FROM sub_materials
WHERE category_id = 2;
So what I'm asking for is basically to be able to see the whole branch of Aluminium. I've looked at ltreeand Closure Tables But I'm to much of a Noob to figure it out by my self.
I believe I have to connect the tables somehow as "grandparent, parent, child" or something similar, but I've no idea if there is some other way or?
I'm not even sure if I'm doing this the right way.
Can someone advise me on this?
I've three tables in a database.(below are samples from the tables)
materials_category Holds all the categories for every material in the DB.
materials Holds the Category name and id, material_names and ids and the values for each material.
Some materials has sub_categories. not all but some.
3. sub_materials Holds the category info, material name and id, the sub_material name and id and the sub_material values.
1. `materials_category`
|category_id| category_name|
------------------------------
| 1 | Aggregate |
| 2 | Aluminium |
| 3 | Asphalt |
2. `materials`
|category_id |category_name |material_id |material_name|EE_1|EE_2|EC_1|EC_2
---------------------------------------------------------------------------
| 1 |Aggregate | 1 | General |3 |0 | 52 | 8
| 2 |Aluminium | 2 | General |55 |7 | 9 | 24
| 2 |Aluminium | 3 | Cast Pr |34 |30 | 22 | 28
| 2 |Aluminium | 4 | Extrud. |65 |16 | 8 | 74
| 2 |Aluminium | 5 | Rolled |15 | 0 | 9 | 61
| 3 |Asphalt | 6 |Asphalt, 4% |2 |22 | 6 | 54
| 3 |Asphalt | 7 |Asphalt, 5% |3 |91 | 1 | 4
3. `sub_materials`
|category_id|category_name|material_id|material_name|sub_mar_id|sub_mar_name|EE_1|EE_2|EC_1|EC_2|
|2 |Aluminium |2 |General |1 |Virgin |21 |5 |9 |60 |
|2 |Aluminium |2 |General |2 |Recycled |29 |3 |8 |9 |
Your tables are denormalised. This is usually not a good thing, unless this is a reporting only database.
Anyway, here's a start for what you're after. why don't you run it and give some feedback on whether it's what you want or not.
select *
from materials_category c
left outer join materials m
on c.category_id = m.category_id
left outer join sub_materials s
on s.material_id = m.material_id
where c.category_name = 'Aluminium'
Looking at your data, I'm guessing that 'recycled' could apply to any number of material categories, not just Aluminium? Would you have a whole bunch of 'Recycled' records in your sub_materials to allow for each material_category?

How to Merge 2 Rows into one by comma separate?

I need to merge this individual rows to one column, I now how to merge column by comma separated,
+---------------+-------+-------+
|CID |Flag |Value |
+---------------+-------+-------+
|1 |F |10 |
|1 |N |20 |
|2 |F |12 |
|2 |N |23 |
|2 |F |14 |
|3 |N |21 |
|3 |N |22 |
+---------------+-------+-------+
Desired Result can be anything,
+-----------+----------------------------+ +--------------------------+
|Part Number| Value | | Value |
+-----------+----------------------------+ +--------------------------+
| 1 | 1|F|10 ; 1|N|20 | Or | 1|F|10 ; 1|N|20 |
| 2 | 2|F|12 ; 2|N|23 ; 2|F|14 | | 2|F|12 ; 2|N|23 ; 2|F|14 |
| 3 | 3|N|21 ; 3|N|22 | | 3|N|21 ; 3|N|22 |
+-----------+----------------------------+ +--------------------------+
Note:
Any hint in right direction with small example is more than enough
EDIT :
I have massive data in tables like thousands of records where parent's and child relationship is present. I have to dump this into text files by comma separated values In single line as record. Think as primary record has relationship with so many other table then all this record has to be printed as a big line.
And I am trying to achieve by creating query so load can be distributed on database and only thing i have to worry about in business is just dumping logic into text files or whatever form we need in future.
You can try to use LISTAGG and your query will look like this:
select a.cid, a.cid || listagg(a.flag || '|' || a.value, ',')
from foo.dat a
group by a.cid
You can use different separators and of course play with how the result will be formatted.

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.