MariaDB Select Min-Date (earliest) from multiple columns - sql

I need to do a select within a MariaDB, where I have one row per customer and the earliest date out of three different tables with action datetime values.
Example Tables:
Main Table
customer-id
Column_X
First
Things
Second
Things
Table One
customer-id
Date
First
Date
Second
Date
Table Two
customer-id
Date
First
Date
Second
Earliest Date (Table2)
Table Three
customer-id
Date
First
Earliest Date (Table3)
Second
Date
My aim is to have the earliest date out of the three columns in the other tables in one column within the select.
What I tried to do is this:
SELECT main.customer-id , main.Column_X
(SELECT LEAST(C) FROM (VALUES ((table1.date) , (table2.date), (table3.date)) AS C) AS First_Action
FROM main_table main
LEFT JOIN table_one table1 ON table1.cutomer-id = main.customer-id
LEFT JOIN table_two table2 ON table2.cutomer-id = main.customer-id
LEFT JOIN table_three table3 ON table3.cutomer-id = main.customer-id
GROUP BY main.customer-id;
Unfortunatly, I don't get any results just an error message.
So the resulting table should look something like this:
Result
customer-id
Column_X
First_Action
First
Things
Earliest Date (Table 3)
Second
Things
Earliest Date (Table 2)
I just started working with SQL statments and therefore have basically no experience. Help would be much appreciated!
Many Greetings
Chris

It seems you simply need a LEAST function -
SELECT main.customer-id , main.Column_X,
LEAST(table1.date, table2.date, table3.date) AS First_Action
FROM main_table main
LEFT JOIN table_one table1 ON table1.cutomer-id = main.customer-id
LEFT JOIN table_two table2 ON table2.cutomer-id = main.customer-id
LEFT JOIN table_three table3 ON table3.cutomer-id = main.customer-id;

Related

How to create MIN and MAX date columns from an INNER JOIN across two DATE columns where each DATE column is from a separate table BigQuery

This is a problem that's driving me mad!
I want to create one MIN column and one MAX column for each unique person_id from an inner join across three tables that has two separate DATE columns - one in tbl1 and one in tbl2 (table 3 just joins based on the person_id). I'm sure it's just the order I'm writing my query but I cannot figure out where to place the DATE column from tbl2.
In short, whatever the MIN date is from the combined DATE columns from tbl1 and tbl2 will form start_date and whatever the MAX is will form end_date. Both are DATES, both in the format: YYYY-MM-DD, so no concerns there.
Here's an example query that works when I just want to find the MAX and MIN date for the tbl1 DATE column:
SELECT tbl1.person_id, MIN(tbl1.Date) AS start_date, MAX(tbl1.Date) AS end_date,
FROM `database_name.table_name1` tbl1
inner join `database_name.table_name2` tbl2
ON (
tbl1.person_id = tbl2.person_id
)
inner join `database_name.table_name3` tbl3
ON (
tbl1.person_id = tbl3.person_id
)
GROUP BY tbl1.person_id
Thank you in advance
You seem to want least() and greatest():
SELECT tbl1.person_id,
LEAST(MIN(tbl1.Date), MIN(tbl2.Date)) AS start_date,
GREATEST(MAX(tbl1.Date), MAX(tbl2.Date)) AS end_date,
FROM `database_name.table_name1` tbl1 JOIN
`database_name.table_name2` tbl2
ON tbl1.person_id = tbl2.person_id JOIN
`database_name.table_name3` tbl3
ON tbl1.person_id = tbl3.person_id
GROUP BY tbl1.person_id

Comparing base table value with second table's sum of value with group by

I have two tables:
One is base table and second is transaction table. I want to compare base table value with second table's sum of value with group by.
Table1(T1Id,Amount1,...)
Tabe2(T2Id,T1ID,Amount2)
I want those rows from table 1 WHere SUM of Table2's SUM( Amount2) is greater or equal table1's Amount1.
*T1ID is in relation with both tables
* The SQL query have many joins with other table for data retriving.
One approach uses a join:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.T1Id = t2.T1ID
GROUP BY
t1.T1Id, t1.Amount1
HAVING
SUM(t2.Amount2) >= t1.Amount1;
We can also try doing this via a correlated subquery:
SELECT t1.T1Id, t1.Amount1
FROM Table1 t1
WHERE t1.Amount1 <= (SELECT SUM(t2.Amount2) FROM Table2 t2
WHERE t1.T1Id = t2.T1ID);
I would use something similar to the query below:
SELECT
a.T1Id, a.Amount1, SUM(b.Amount2)
FROM Table1 a
INNER JOIN Table2 b on b.T1Id = a.T1Id
GROUP BY a.T1Id, a.Amount1
HAVING SUM(b.Amount2) >= a.Amount1;
Basically what the query above does is give you the ID, Amount from table 1 and the summed amount from table 2. The HAVING clause at the end of query filters out those records where the summed amount from the second table is smaller than the amount from the first one.
If you want to add further table joins to the query, you can do so by adding as many joins as you wish. I would recommend having a referenced ID for each table you are joining in the Table1 table.

matching value by non-unique id and minimum date difference

I'm using sqlite through the RSQLite package in R.
I have two tables:
Table 1 has important columns 'PERMCO' and 'Reporting_Period'.
('Reporting_Period' is an integer date)
Table 2 has important columns 'PERMCO' and 'date'.
('date' is an integer date)
I want to do a left join with table 1 as the left table.
Thing is that 'PERMCO' is not unique (row-wise, many duplicates) in the second table.
For a given row of table 1, I want the match from the second table to be the row from table 2 with matching PERMCO that is closest in absolute date to 'Reporting_Period' in the first table.
Not really sure how to do this...
Thank you
The Idea is a correlated subquery to get the closest day in table2 from Reporting_Period in table1
SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON t1.permco = t2.permco
WHERE ABS(t2."date" - t1.Reporting_Period) = (SELECT MIN(ABS("date" - t1.Reporting_Period) )
FROM table2
WHERE permco = t1.permco
)
OR t2.permco IS NULL --because you want a left join
;
I'm not familier with Sqlite, so you may need to change the query to subtract the two date.

How do I join my two tables?

I have two Tables and I am trying to do a join or union to bring over one column from the second table.
SELECT Services,Metric_Type,services_be,Services_Be_L2,Services_Be
FROM table1
JOIN table2
ON table1.SERVICES_BE = table2.SERVICES_BE
Metric_Type is all I need from table2
Potential joins
Table1
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR,
Table2
Services_Be,
Sales_L1 = SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR
I guess it's a column ambiguously defined error.
In the select statement, you list the field SERVICES_BE which belongs to both tables, and the sql parser don't know which table you meant.
try using table alias.
SELECT t1.Services,t2.Metric_Type,t1.services_be,t1.Services_Be_L2,t1.Services_Be
FROM table1 t1
Inner JOIN table2 t2 ON t1.SERVICES_BE = t2.SERVICES_BE
SELECT Services,Metric_Type,services_be,Services_Be_L2,Services_Be
FROM table1
JOIN table2
ON table1.SERVICES_BE = table2.SERVICES_BE
Looks like a valid join and that would allow you to use columns from both tables side by side. The one thing that you may want to use are table aliases to make it easier to reference the table and then specifically identify the table your column is from in the Select list something like:
SELECT t1.Services,t1.Metric_Type,t1.services_be,t2.Services_Be_L2,t2.Services_Be
FROM table1 t1
JOIN table2 t2
ON t1.SERVICES_BE = t2.SERVICES_BE
If you want to APPEND the records from one table to another you will want to use UNION or UNION ALL
SELECT
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR
FROM
Table11
UNION ALL
SELECT
Services_Be,
SALES_NODE_LEVEL1,
Fiscal QT,
Fiscal YR
FROM
Table12
When appending, the column definitions and data types need to be the same between the top table and bottom. UNION ALL will append every record from Table2, while UNION will only append records that are not already present in Table1 (Sort of like DISTINCT).

SQL EXCLUDE records with timestamps defined in the separate table

I need to write SQL that EXCLUDES records with date in a certain date range. The range is defined by another table.
First TABLE1 looks like this:
DATE VALUE
'1-jan-15 00:00'., 123
'1-jan-15 00:01'., 999
................., ...
'15-jan-15 16:42', ...
Second TABLE2 looks like this:
START_DATE END_DATE
'4-jan-15 12:05', '4-jan-15 12:17'
'7-jan-15 12:15', '4-jan-15 14:10'
etc
I need all the values from TABLE1 except the ones with the timestamp between TABLE2.START_DATE and TABLE2.END_DATE
Inner Join the two tables using Not Between Operator
SELECT datavalue
FROM table1 a
INNER JOIN table2 b
ON a.datavalue NOT BETWEEN b.start_date AND b.endate