Get MAX() on column in two tables - sql

I have two tables which both have the column DateTime.
How can I get the MAX() DateTime?
The shorter/simpler the better, because this is just part of a larger query.

You could use the GREATEST function:
SELECT GREATEST((SELECT MAX(column)
FROM TABLE_1),
(SELECT MAX(column)
FROM TABLE_2))
Using UNIONs:
SELECT MAX(col)
FROM (SELECT col FROM TABLE_1
UNION ALL
SELECT col FROM TABLE_2)
Use UNION ALL for this - it's faster because it doesn't remove duplicates, and it doesn't matter if duplicates are returned by the subquery in this example.

SELECT MAX(thedate) FROM (
SELECT mydate as thedate FROM TABLE1
UNION
SELECT anotherdate as thedate FROM TABLE2
) as tablealias

Related

I want to retrieve max value from two column - SQL table

Please find attached image for table structure
You could try using UNION ALL for build a unique column result and select the max from this
select max(col)
from (
select col1 col
from trans_punch
union all
select col2
from trans_punch) t
You can use a common table expression to union the columns, then select the max.
;with cteUnionPunch(Emp_id, both_punch) AS
(
SELECT Emp_id, In_Punch FROM trans_punch
UNION ALL
SELECT Emp_id, Out_Punch FROM trans_punch
)
SELECT Emp_id, max(both_punch) FROM cteUnionPunch GROUP BY Emp_id
You can use apply :
select tp.Emp_id, max(tpp.Punchs)
from trans_punch as tp cross apply
( values (In_Punch), (Out_Punch) ) tpp(Punchs)
group by tp.Emp_id;

Find Max value of column from all tables with that column in SQL

I am trying to show the MAX of a date field, that several of my tables have in my database.
select max(col)
from (select max(col) from t1
union all
select max(col) from t2
union all
...
select max(col) from tn)

It is possible to do a SELECT MIN(DATE) searching in 2 or more columns?

I have a table which have 2 columns of dates, and I want to look for the closest date in the future (Select min()?) searching in both columns, is that possible?
For example, if I have column1=23/11/2014 and column2=22/11/2014, in the same row, I want get 22/11/2014
I hope it is clear enough, ask me if it doesn't.
Greetings.
In a single table use CASE
SELECT CASE column1 < column2
THEN column1
ELSE column2
END mindate
FROM yourtable
If you have the date column in multiple tables, just replace yourtable with your tables JOINed together
This is what the least() function is for:
select least(column_1, column_2)
from your_table;
min() is an aggregate function that operates on a single column but multiple rows.
try something like this:
select min(date) from(
select a.date date from table1 a
union
select b.date2 date from table1 b
)

SQL SUM function inquiry

I'm having a hard time summing up a column on two tables. The scenario is something like this (refer to the image below)
Table 1 may have a lot of rows per Date. But Table 2 may only consists of two rows of data per Date. What I wanted to do is to sum up all Item/Price (Table1) according to their Date and ADD them with another SUM of Item/Price of Table2. The category of SUM is by Date.
I tried any joins statement (left, right or inner) but it does not produce the result that I am expecting to. My expected result is the Result table. But on my query, it produces a very high value.
Thanks.
Use a UNION clause like this:
WITH t(d, p) AS (
SELECT [Date], Price FROM Table1
UNION ALL
SELECT [Date], Price FROM Table2
)
SELECT d, SUM(p) FROM t GROUP BY d
You can do this with UNION ALL in either a subquery or a cte, cte shown here:
;WITH cte AS (SELECT [Date], Price
FROM Table1
UNION ALL
SELECT [Date], Price
FROM Table2
)
SELECT [Date], SUM(Price) AS Total_Price
FROM cte
GROUP BY [Date]
Demo: SQL Fiddle
Try This,
with cte (C_Date,C_Price)
as
(
SELECT date,SUM(price) FROM table_1
group by date
union
SELECT date,SUM(price) FROM table_2
group by date
)
select c_date,SUM(c_price) from cte
group by C_Date
Try this
Select t.date,P1+P2
from(
Select Date,sum(Price) P1
from table1 t
group by Date
) t
left join
(
Select Date,sum(Price) P2
from table t2
group by date
) t1 on t.date = t1.date
group by date

Union all on the same table

There has to be a better way of doing this any one have an idea. there is one table and i have 8 columns i need select all of the columns one on top of each other and for each selected colum i need to count the number of items that are the same
SELECT Col1, count(*) 'Selected'
FROM [Table]
group by temp_id,Col1
UNION ALL
SELECT Col2,count(*)
FROM [Table]
group by temp_id,Col2
having len(ltrim(rtrim(Col2)))<>0
UNION ALL
SELECT Col3,count(*)
FROM [Table]
group by temp_id,Col3
having len(ltrim(rtrim(Col3)))<>0
union all
SELECT Col4,count(*)
FROM [Table]
group by temp_id,Col4
union all
SELECT Col5,count(*)
FROM [Table]
group by temp_id,Col5
having len(ltrim(rtrim(Col5)))<>0
union all
SELECT Col6,count(*)
FROM [Table]
group by temp_id,Col6
having len(ltrim(rtrim(Col6)))<>0
union all
SELECT Col7,count(*)
FROM [Table]
group by temp_id,Col7
having len(ltrim(rtrim(Col7)))<>0
union all
SELECT Col8,count(*)
FROM [Table]
group by temp_id,Col8
having len(ltrim(rtrim(Col8)))<>0
There is. It is called grouping sets (and it is documented here). In your case, you can do something like this (example for first three columns):
select coalesce(col1, col2, col3), count(*) as Selected
from [table]
group by grouping sets ((temp_id, col1), (temp_id, col2), (temp_id, col3));
Your condition using length() and trim() on each column -- you should probably handle that using a having clause.