I have a boxscore data set of a basketball team with mm:ss played (ex. 44:46) when I uploaded the csv to the table the values whose greater than 24 somehow became hh:mm (ex. 44:46 became 44:46:00) while the others just stayed the same (ex. 5:04), it is currently saved as a varchar data type.
How do I fix it so I can use the values to get for example the player with the highest time played in the team or the average time played for each player?
/*input*/
create table tab1( interval varchar(100));
insert into tab1 values( '44:46:00' );
insert into tab1 values( '5:04' );
/*Query*/
select case when charindex(':',interval,4) = 0 then interval
else left(interval,charindex(':',interval,4)-1) end from tab1
WITH formatted_time AS (
SELECT
player,
CAST(SUBSTRING(time_played, 1, 2) AS INT) * 60 + CAST(SUBSTRING(time_played, 4, 2) AS INT) AS time_in_seconds
FROM
your_table
)
SELECT
player,
time_in_seconds,
time_in_seconds / 60 || ':' || time_in_seconds % 60 AS time_played_formatted
FROM
formatted_time
Related
I have an input table of the login and logout hours of users in a table, I am required to split the output into hourly intervals.
Thanks in advance for any assistance
here's the snippet to create the table:
CREATE TABLE table_name (
userid varchar(255),
date_capture DATE,
login_hour int,
logout_hour int
);
here's the test data:
INSERT INTO table_name(userid,date_capture,login_hour,logout_hour)
VALUES ('Test1', '02-JUN-2020',6,9);
INSERT INTO table_name (userid,date_capture,login_hour,logout_hour)
VALUES ('Test2', '02-JUN-2020',3,4);
Here is the expected output:
You can use a cte to recursively add an hour until the target is reached:
with cte(userid,date_capture,login_hour,logout_hour,target) as (
select userid,
date_capture,
login_hour,
login_hour + 1,
logout_hour
from table_name
union all
select userid,
date_capture,
logout_hour,
logout_hour + 1,
target
from cte
where logout_hour + 1 <= target
)
select userid,
date_capture,
login_hour,
logout_hour
from cte
order by userid,
date_capture,
login_hour;
I'm using a PostgreSQL database and have one table "event_date" with "started_at" (DATE format, e.g. '2019-10-29') values which are also the PRIMARY KEY in this table.
Now I want to insert this "started_at" value in another table "event_days" but increasing the date by x days.
Can someone explain how to achieve this? Thanks a lot!
Use an INSERT with a SELECT:
insert into other_table (started_at, location, moderator)
select started_at + 42, 'Hall 5', 'Tom'
from event_days;
+ 42 add 42 days to the date value.
if you really need insert the related record you could use a insert select
insert into event_days(started_at, location , moderator)
select started_at + interval '10 day', 'hall 5' , 'Tom'
from event_date
I have this table:
I want to workout how many bus journeys per hour are operated by Bond Brothers
and the expected results is 7 per hour
My questions is how do I add up values that contain strings?
SELECT Operator, SUM(Frequency) AS “Total Frequency”
FROM Bus Routes
I don't think this would work would it?
Thanks
for that
declare #table as table (
group_id int,
content varchar(100)
)
insert into #table values (2, '2 per hour')
insert into #table values (1, '1 per hour')
insert into #table values (1, '6 per hour')
insert into #table values (1, '')
insert into #table values (3, '9 per hour')
insert into #table values (3, null)
select * from #table
do
select t.group_id, SUM(t.content) content_sum
from (select group_id, convert(int, REPLACE(content, ' per hour', ''))
content from #table) t
group by t.group_id
Borrowing from this How to get the numeric part from a string using T-SQL?, you could do something like this (note the cast to an integer):
SELECT Operator, SUM(CAST(left(Frequency, patindex('%[^0-9]%', Frequency+'.') - 1) AS INT))
FROM
(VALUES
('2 per hour','Ok Travel')
,('6 per hour','Diamond Buses')
,('1 per hour','Bond Brothers')
,('6 per hour','Bond Brothers')
,('','Diamond Buses')
) BusRulles(Frequency,Operator)
GROUP BY Operator
WITH CTE AS(
SELECT ts.Destination ,
CONVERT(INT,REPLACE(ts.Frequency, ' per hour','')) AS Frequency ,
ts.Operator ,
ts.[Route Number] ,
ts.Start
FROM dbo.temp_Sqlcheck ts
WHERE ts.Frequency IS NOT NULL OR ts.Frequency <>''
)
SELECT CTE.Operator, SUM(CTE.Frequency) AS Frequency from CTE
GROUP BY CTE.Operator
This will not count NULL or Blank values in Frequency.
I am stuck on converting a varchar column schedule containing the following data 0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19 to INT. I know, please don't ask why this schedule column was not created as INT initially, long story.
So I tried this, but it doesn't work. and give me an error:
select CAST(schedule AS int) from shift_test:
the query should check if the numbers representing days are found in the schedule filed using the sql code below
select empid, case when ((DateDiff(hour,'01-01-2014 07:00' , '01-01-2014 00:00')/ 24 )% 15) in ( CAST(schedule AS int))
then 'A' else '*' end as shift_A from Shift_test
After executing i get this error.
Conversion failed when converting the varchar value to int.
Any help will be appriciated
Use ISNUMERIC() test if you are using version 2008 or 2008R2. In SQL SERVER 2012 you can use TRY_CAST() function, which checks if the data conversion is allowed for given literal.
Mock up code for SQL Server 2008/R2:
Select col1, col2,
case
when <condition> and isnumeric(col2) then cast(col2 as int)
else <do whatever...>
end
as converted_col2
from <yourtable>;
For SQL Server 2012:
Select col1, col2,
case
when <condition> then try_cast(col2 as int)
else <do whatever...>
end
as converted_col2
from <yourtable>;
Example with SQl Server 2008
declare #T table (empid int, schedule varchar(2)) ;
insert into #T(empid, schedule) values (1, '1');
insert into #T(empid, schedule) values (2, '2');
insert into #T(empid, schedule) values (3, '03');
insert into #T(empid, schedule) values (4, '4');
insert into #T(empid, schedule) values (5, '05');
insert into #T(empid, schedule) values (6, 'A');
select empid,
case
when ISNUMERIC(schedule) = 1
and ((DateDiff(hour,'01-01-2014 07:00' , '10-01-2014 00:00')/ 24 )% 15)
in ( CAST(schedule AS int)) then 'A'
else '*'
end
as shift_A
from #T;
# Binaya Ive added my code for more help.
The schedule column contain (0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19) which is varchar.
i want to output A if after this calculation ((DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15) the result is found in the schedule column else if after the calculation and the result is not found output *
;with Shift_runover (shift_code,schedule,endd,startdate)
-- Start at the beginning of shift.
as
(select shift_code,schedule,Cast(end_date as DateTime) as endd,Cast(start_date as DateTime)as startdate from dbo.Shift_test
union all
-- Add hours up to the desired end date.
select shift_code,schedule,endd,DateAdd(hour, 1,startdate)from Shift_runover where startdate<=endd),
Extendedsamples as
(
-- Calculate the number of days since the beginning of the first shift on 1/1/2014.
select shift_code,schedule,startdate,DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 as Days from Shift_runover ),
Shifts as
(
-- the schedule column contain (0, 1, 2, 3,4,8,9,10,11,12,15,16,17,18,19) which is varchar.
-- i want to output A if ((DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15) is found in the schedule colume
select *,
case when (DateDiff(hour,'01-01-2014 07:00',startdate)/ 24 )% 15 in(schedule)
then 'A' else '*' end as shift_A
from ExtendedSamples
)
select *
from Shifts
option ( maxrecursion 0 )
I have a SELECT query and would like to place a negative sign in percentage value if it's less than 100. Here is my query:
SELECT produces, amount, percentage FROM inventory
I tried:
SELECT produces, amount, CASE WHEN percentage<100 THEN '-'+percentage ELSE percentage END FROM inventory
But it did not work.
If you want to invert the sign simply multipy it with -1.
Since the percentage-column is a numeric of some sort, not a string, you need to cast it to a varchar, something like this should work:
SELECT produces, amount, CASE WHEN percentage < 100 THEN '-' + CAST(percentage AS varchar(50)) ELSE CAST(percentage AS varchar(50)) END FROM inventory
Here's a working example:
CREATE TABLE inventory (produces int, amount int, percentage int)
INSERT INTO inventory values(1, 1, 110)
INSERT INTO inventory values(2, 2, 100)
INSERT INTO inventory values(3, 3, 90)
INSERT INTO inventory values(4, 4, 80)
SELECT
produces,
amount,
CASE
WHEN percentage < 100
THEN '-' + CAST(percentage AS varchar(50))
ELSE CAST(percentage AS varchar(50))
END
FROM
inventory