I have a table as follow:
Empl Dates Hour
Emp1 date1 8
Emp1 date2 5
Emp1 date3 4
Emp2 date1 1
Emp2 date2 3
Emp2 date3 5
....
How do I sum hours for employees and place the sum value on each rows of in MSAccess Query and not the SQL?
like
Empl Dates Hour Sum of Hours
Empl1 date1 8 17
Empl1 date2 5 17
Empl1 date3 4 17
Empl2 date1 1 9
Empl2 date2 3 9
Empl2 date3 5 9
and so on.
I don't have timestamp table. I just need to convert using name string,
SELECT day_of_week;
Output should look like
day_of_week sum
Friday 5
Monday 1
Saturday 6
Sunday 7
Thursday 4
Tuesday 2
Wednesday 3
If it were timestamp then you can use the isodow. See doc
isodow
The day of the week as Monday(1) to Sunday(7)
SELECT day_of_week, EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40') as sum FROM your_table;
But you don't have that column. So you can use CASE...WHEN like below-
SELECT day_of_week,
CASE WHEN day_of_week='Monday' THEN 1
WHEN day_of_week='Tuesday' THEN 2
WHEN day_of_week='Wednesday' THEN 3
WHEN day_of_week='Thursday' THEN 4
WHEN day_of_week='Friday' THEN 5
WHEN day_of_week='Saturday' THEN 6
ELSE 7
END as sum
FROM your_table;
See doc: https://www.postgresql.org/docs/8.4/functions-conditional.html
I have several measures names and 5 dates and I need to create a calculated field over the first 4 dates and use the result in news calculated fields with the 5 date
date1 date2 date3 date4 date5 AVGdate1-4
measure1
measure2
.
.
measureN
I want to get greater date from four date columns and fields can be NULL.
Please help me to write a query for this.
Example:
select max(date1, date2, date3, date4) from table A where tag_id='xxxxx'
You can work with unions which combine your query results and then get the max date of the combined columns
Here is the same question asked in another post
Hope that helps you
select max(max_date) from
(select date1 max_date from table A where tag_id='xxxxx'
union all
select date2 from table A where tag_id='xxxxx'
union all
select date3 from table A where tag_id='xxxxx'
union all
select date4 from table A where tag_id='xxxxx')
Here you have:
SELECT
CASE
WHEN date1 >= date2 AND date1 >= date3 THEN date1
WHEN date2 >= date1 AND date2 >= date3 THEN date2
WHEN date3 >= date1 AND date3 >= date2 THEN date3
ELSE date1
END AS RecentDate
FROM table A
WHERE A.tag_id='xxxxx'
Hope it helps,
Did you try your example query?
MAX() (and MIN() for that matter) are a bit unusual....they exists as both an aggregate function operating on multiple rows and a scalar function operating on multiple columns.
The works fine for me on DB2 for IBM i:
select max(dte1,dte2,dte3,dte4) from qtemp.test
I have this data in a table.
create table pick_max_date
(
student_id int
,date1 datetime
,date2 datetime
,date3 datetime
,date4 datetime
)
insert into pick_max_date
(student_id,date1,date2,date3,date4)values
(1,'2015-06-01','2016-01-01','2014-01-01','2017-01-01')
,(2,'2016-06-01','2017-08-01','2018-01-01','2017-05-06')
,(3,'2013-06-01','2019-08-01','2012-01-01','2012-05-06')
select * from pick_max_date
I need to select the max date for each student as below.
student_id max_date
---------- ----------
1 2017-01-01
2 2018-01-01
3 2019-08-01
what is the most optimised way to select as above. any help is appreciated.
Thanks in advance
Construct a derived table of the columns and select the max from that table,
select student_id,
(
select max(d.d)
from (values(date1),
(date2),
(date3),
(date4)
) as d(d)
) as max_date
from dbo.pick_max_date;
Sergey Gegoyan describes in the following link very efficient four different ways to solve this problem. I would prefer the first one the most:
LINK
Let me know if you found it useful.
SELECT student_id ,
(SELECT Max(v) FROM (VALUES (date1), (date2), (date3)) AS value(v)) as maxdate
FROM pick_max_Date
or
SELECT student_id,b.*
from
pick_max_Date
cross apply
(select max(d)
from
(values(date1),(date2),(date3),(date4))v(d)
)b(maxx)
You can do either the values tables already mentioned, or simply use a case if the columns are static:
select student_id
,(select max(d)
from (values(date1),(date2),(date3),(date4)) as tbl(d)) as MaxDate
,case when date1 >= date2 and date1 >= date3 and date1 >= date4 then date1
when date2 >= date1 and date2 >= date3 and date2 >= date4 then date2
when date3 >= date1 and date3 >= date2 and date3 >= date4 then date3
else date4
end as MaxDate2
from pick_max_date