T-SQL SUM manually defined values for X columns - tsql

I'm trying to INSERT a value into a column called Total for a number of hours spent throughout the week.
I have a form on a website that allows a user to input values for Mon, Tues, Weds, Thurs, Fri, Sat & Sun.
I can access the entered values via tokens for each field. IE: if I need the value for Mon, I can grab it using [Mon] and it will pull whatever number the user has entered into the column.
Using this I can build a query such as:
INSERT INTO MyTbl (Mon, Tues, Weds, Thurs, Fri, Sat, Sun,)
VALUES ('[Mon]', '[Tues]', '[Weds]', '[Thurs]', '[Fri]', '[Sat]', '[Sun]')
I'm having trouble modifing this query to SUM all of the day's values as my Total column insert value.
Something like this:
INSERT INTO MyTbl (Mon, Tues, Weds, Thurs, Fri, Sat, Sun, Total)
VALUES ('[Mon]', '[Tues]', '[Weds]', '[Thurs]', '[Fri]', '[Sat]', '[Sun]',
SUM('[Mon]', '[Tues]', '[Weds]', '[Thurs]', '[Fri]', '[Sat]', '[Sun]'))
Note that all columns are set to VARCHAR datatype in SQL Server.
Any help or examples appreciated.
IF Data looks like:
Mon, Tues, Weds, Thurs, Fri, Sat, Sun
1 2 3 4 5 6 7
I need a Total field that would sum all day values for in INSERT like:
Mon, Tues, Weds, Thurs, Fri, Sat, Sun, Total
1 2 3 4 5 6 7 28
Basically I need some query that can calculate the Total field in this example a value of 28 and INSERT that into the Total field in my table.

SUM() is for aggregation over a number of rows. You're in the scope of one single row. To add the fields together, you just need to convert them to an INT and add them.
Insert MyTbl
(Mon, Tue, Weds, Thurs, Fri, Sat, Sun, Total)
Values ('1', '2', '3', '4', '5', '6', '7',
Convert(Varchar, (1 + 2 + 3 + 4 + 5 + 6 + 7)));
Ideally, you should really be storing the values as their appropriate datatype (INT in this case).
A more ideal solution would be to create a computed column on the table, and have it be defined as the sum of the other fields:
E.g.:
Create Table MyTbl
(
Mon Int,
Tue Int,
Weds Int,
Thurs Int,
Fri Int,
Sat Int,
Sun Int,
Total As (Mon + Tue + Weds + Thurs + Fri + Sat + Sun)
);
Doing it that way, you won't have to worry about inserting the total, or keeping it up-to-date.

Related

Billing cycle, get a date every month (no such Feb 30)

I have a column called anchor which is a timestamp. I have a row with value of jan 30 2020. I want to compare this to feb 29 2020, and it should give me 1 month. Even though its not 30 days, but feb has no more days after 29. I am trying to bill every month.
Here is my sql fiddle - http://sqlfiddle.com/#!17/6906d/2
create table subscription (
id serial,
anchor timestamp
);
insert into subscription (anchor) values
('2020-01-30T00:00:00.0Z'),
('2019-01-30T00:00:00.0Z');
select id,
anchor,
AGE('2020-02-29T00:00:00.0Z', anchor) as "monthsToFeb29-2020",
AGE('2019-02-28T00:00:00.0Z', anchor) as "monthsToFeb28-2019"
from subscription;
Is it possible to get age in the way I am speaking?
My expected results:
For age from jan 30 2020 to feb 29 2020 i expect 1.0 month
For age from jan 30 2020 to feb 28 2019 i expect -11.0 month
For age from jan 30 2019 to feb 29 2020 i expect 13.0 month
For age from jan 30 2019 to feb 28 2019 i expect 1.0 month
(this is how momentjs library does it for those node/js guys out there):
const moment = require('moment');
moment('Jan 30 2019', 'MMM DD YYYY').diff(moment('Feb 29 2020', 'MMM DD YYYY'), 'months', true) === -13.0
moment('Jan 30 2019', 'MMM DD YYYY').diff(moment('Feb 28 2019', 'MMM DD YYYY'), 'months', true) === -1.0
How about:
select round(('2/29/2020'::date - '1/30/2020'::date) / 30.0);
round
-------
1
select round(('02/28/2019'::date - '1/30/2020'::date ) / 30.0);
round
-------
-11
select round(('2/29/2020'::date - '1/30/2019'::date) / 30.0);
round
-------
13
select round(('2/28/2019'::date - '01/30/2019'::date) / 30.0);
round
-------
1
The date subtraction gives you a integer value of days, then you divide by a 30 day month and round to nearest integer. You could put this in a function and use that.

posgres query filter by date in format Mon Aug 24 14:16:00 GMT 2020

I have a table of logs where data is being saved with format Mon Aug 24 14:16:00 GMT 2020 and I need to query the data that is between a date range.
I need something like
SELECT * FROM logs
WHERE
log_date_time >= 'some-date-format'
AND log_date_time < 'some-date-format';
the log_date_time is saved as a string. I tried using
select * from logs
WHERE log_date_time BETWEEN 'Mon Aug 05 2019' AND 'Mon Aug 24 17:54:37 GMT 2020';
But it is not working correclty, brings only dates from day 17 and 24
How can I query this?
So something like:
SELECT
*
FROM
logs
WHERE
log_date_time::timestamptz
BETWEEN
'Mon Aug 05 2019 GMT'::timestamptz
AND
'Mon Aug 24 17:54:37 GMT 2020'::timestamptz;
Where you convert the log_date_time to a timestamp with time zone and then do the same for the string datetime values you want to use as the date range.

How to sort attendance date along with the month?

Attendance is sorting according to date, that is fine, but I want to sort date along with the month name January should come at the bottom, and December at the top.
Table
Attendance Date
---------------
26 Feb 2018
19 Dec 2018
18 Dec 2018
14 Dec 2018
12 June 2018
7 Dec 2018
5 Feb 2018
Query
select distinct
(select ARRAY_TO_STRING(ARRAY_AGG(ARRAY[to_char(t1.l_time,'HH12:mi AM')]::text), ',')
from
(select (al1.create_time AT TIME ZONE 'UTC+5:30')::time as l_time
from users.access_log as al1
where al1.user_id = al.user_id
and al1.login_status = 1
and al1.create_time::date = al.create_time::date
order by al1.create_time::time ASC
) as t1
) as login_time,
(select ARRAY_TO_STRING(ARRAY_AGG(ARRAY[to_char(t2.o_time,'HH:mi AM')]::text), ',')
from
(select (al2.create_time AT TIME ZONE 'UTC+5:30')::time as o_time
from users.access_log as al2
where al2.user_id = al.user_id
and al2.login_status = 0
and al2.create_time::date = al.create_time::date
order by al2.create_time::time ASC
) as t2
) as logout_time,
al.create_time::date
from users.access_log as al
where al.user_id = ?;
Attendance is sorting according to date, that is fine, but I want to sort date along with the month name January should come at the bottom, and December at the top.

What is the best way to get this TSQL Pivot to work [duplicate]

I need to do the following transpose in MS SQL
from:
Day A B
---------
Mon 1 2
Tue 3 4
Wed 5 6
Thu 7 8
Fri 9 0
To the following:
Value Mon Tue Wed Thu Fri
--------------------------
A 1 3 5 7 9
B 2 4 6 8 0
I understand how to do it with PIVOT when there is only one column (A) but I can not figure out how to do it when there are multiple columns to transpose (A,B,...)
Example code to be transposed:
select LEFT(datename(dw,datetime),3) as DateWeek,
sum(ACalls) as A,
Sum(BCalls) as B
from DataTable
group by LEFT(datename(dw,datetime),3)
Table Structure:
Column DataType
DateTime Datetime
ACalls int
BCalls int
Any help will be much appreciated.
In order to transpose the data into the result that you want, you will need to use both the UNPIVOT and the PIVOT functions.
The UNPIVOT function takes the A and B columns and converts the results into rows. Then you will use the PIVOT function to transform the day values into columns:
select *
from
(
select day, col, value
from yourtable
unpivot
(
value
for col in (A, B)
) unpiv
) src
pivot
(
max(value)
for day in (Mon, Tue, Wed, Thu, Fri)
) piv
See SQL Fiddle with Demo.
If you are using SQL Server 2008+, then you can use CROSS APPLY with VALUES to unpivot the data. You code would be changed to the following:
select *
from
(
select day, col, value
from yourtable
cross apply
(
values ('A', A),('B', B)
) c (col, value)
) src
pivot
(
max(value)
for day in (Mon, Tue, Wed, Thu, Fri)
) piv
See SQL Fiddle with Demo.
Edit #1, applying your current query into the above solution you will use something similar to this:
select *
from
(
select LEFT(datename(dw,datetime),3) as DateWeek,
col,
value
from DataTable
cross apply
(
values ('A', ACalls), ('B', BCalls)
) c (col, value)
) src
pivot
(
sum(value)
for dateweek in (Mon, Tue, Wed, Thu, Fri)
) piv

PostgreSQL - GROUP subsequent rows

I have a table which contains some records ordered by date.
And I want to get start and end dates for each subsequent group (grouped by some criteria e.g.position).
Example:
create table tbl (id int, date timestamp without time zone,
position int);
insert into tbl values
( 1 , '2013-12-01', 1),
( 2 , '2013-12-02', 2),
( 3 , '2013-12-03', 2),
( 4 , '2013-12-04', 2),
( 5 , '2013-12-05', 3),
( 6 , '2013-12-06', 3),
( 7 , '2013-12-07', 2),
( 8 , '2013-12-08', 2)
Of course if I simply group by position I will get wrong result as positions could be the same for different groups:
SELECT POSITION, min(date) MIN, max(date) MAX
FROM tbl GROUP BY POSITION
I will get:
POSITION MIN MAX
1 December, 01 2013 00:00:00+0000 December, 01 2013 00:00:00+0000
3 December, 05 2013 00:00:00+0000 December, 06 2013 00:00:00+0000
2 December, 02 2013 00:00:00+0000 December, 08 2013 00:00:00+0000
But I want:
POSITION MIN MAX
1 December, 01 2013 00:00:00+0000 December, 01 2013 00:00:00+0000
2 December, 02 2013 00:00:00+0000 December, 04 2013 00:00:00+0000
3 December, 05 2013 00:00:00+0000 December, 06 2013 00:00:00+0000
2 December, 07 2013 00:00:00+0000 December, 08 2013 00:00:00+0000
I found a solution for MySql which uses variables and I could port it but I believe PostgreSQL can do it in some smarter way using its advanced features like window functions.
I'm using PostgreSQL 9.2
There is probably more elegant solution but try this:
WITH tmp_tbl AS (
SELECT *,
CASE WHEN lag(position,1) OVER(ORDER BY id)=position
THEN position
ELSE ROW_NUMBER() OVER(ORDER BY id)
END AS grouping_col
FROM tbl
)
, tmp_tbl2 AS(
SELECT position,date,
CASE WHEN lag(position,1)OVER(ORDER BY id)=position
THEN lag(grouping_col,1) OVER(ORDER BY id)
ELSE ROW_NUMBER() OVER(ORDER BY id)
END AS grouping_col
FROM tmp_tbl
)
SELECT POSITION, min(date) MIN, max(date) MAX
FROM tmp_tbl2 GROUP BY grouping_col,position
There are some complete answers on Stackoverflow for that, so I'll not repeat them in detail, but the principle of it is to group the records according to the difference between:
The row number when ordered by the date (via a window function)
The difference between the dates and a static date of reference.
So you have a series such as:
rownum datediff diff
1 1 0 ^
2 2 0 | first group
3 3 0 v
4 5 1 ^
5 6 1 | second group
6 7 1 v
7 9 2 ^
8 10 2 v third group