Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
ID Time Status
----------- ----------------------- --------------------------------------------------
1 2013-12-24 00:00:00 on
2 2013-12-25 00:00:00 on
3 2013-12-26 00:00:00 on
4 2013-12-27 00:00:00 on
5 2013-12-28 00:00:00 on
6 2013-12-29 00:00:00 on
7 2013-12-30 00:00:00 on
8 2013-12-31 00:00:00 on
9 2013-12-24 00:00:00 off
10 2013-12-25 00:00:00 off
11 2013-12-27 00:00:00 off
12 2013-12-27 00:00:00 on
13 2013-12-27 00:00:00 off
14 2013-12-27 00:00:00 on
15 2013-12-27 00:00:00 off
16 2013-12-28 00:00:00 on
17 2013-12-28 00:00:00 off
18 2013-12-28 00:00:00 on
19 2013-12-29 00:00:00 off
20 2013-12-29 00:00:00 on
21 2013-12-30 00:00:00 off
22 2013-12-30 00:00:00 on
23 2013-12-30 00:00:00 off
24 2013-12-30 00:00:00 on
25 2013-12-30 00:00:00 off
26 2013-12-31 00:00:00 on
27 2013-12-31 00:00:00 off
28 2013-12-31 00:00:00 on
29 2013-12-31 00:00:00 off
30 2013-12-31 00:00:00 on
31 2013-12-31 00:00:00 off
I have this Data , I want a query that categorized them by Date and by on and Off
with column names Date , On , Off , I am using Microsoft SQL server 2008
Lets say the column containing date was named yourDateColumn and the column containing status was named statusColumn, you could do:
SELECT yourDateColumn,
SUM(statusColumn="On") as On,
SUM(statusColumn="Off") as Off
FROM yourTable
GROUP BY yourDateColumn
ORDER BY yourDateColumn
I have no idea what the name or structure of your table but from the data i was able to figure out this should work.
SELECT Date, SUM(status="On") as On_count, SUM(status="Off") as Off_count
FROM Table
GROUP BY Date
Order by Date;
SELECT t.Time, Count(t_on.id),Count(t_off.id) FROM Table1 t
LEFT JOIN Table1 t_on
ON t.id= t_on.id and t_on.Status = 'on'
LEFT JOIN Table1 t_off
ON t.id= t_off.id and t_off.Status = 'off'
GROUP BY t.Time Order by Time;
Fiddle
Related
I have clickhouse table with some rows like that
id
created_at
6962098097124188161
2022-07-01 00:00:00
6968111372399976448
2022-07-02 00:00:00
6968111483775524864
2022-07-03 00:00:00
6968465518567268352
2022-07-04 00:00:00
6968952917160271872
2022-07-07 00:00:00
6968952924479332352
2022-07-09 00:00:00
I need to resample time-series and get count by date like this
created_at
count
2022-07-01 00:00:00
1
2022-07-02 00:00:00
2
2022-07-03 00:00:00
3
2022-07-04 00:00:00
4
2022-07-05 00:00:00
4
2022-07-06 00:00:00
4
2022-07-07 00:00:00
5
2022-07-08 00:00:00
5
2022-07-09 00:00:00
6
I've tried this
SELECT
arrayJoin(
timeSlots(
MIN(created_at),
toUInt32(24 * 3600 * 10),
24 * 3600
)
) as ts,
SUM(
COUNT(*)
) OVER (
ORDER BY
ts
)
FROM
table
but it counts all rows.
How can I get expected result?
why not use group by created_at
like
select count(*) from table_name group by toDate(created_at)
I have a table that has the following values (reduced for brevity)
Period
Periodfrom
Periodto
Glperiodoracle
Glperiodcalendar
88
2022-01-01 00:00:00
2022-01-28 00:00:00
JAN-FY2022
JAN-2022
89
2022-01-29 00:00:00
2022-02-25 00:00:00
FEB-FY2022
FEB-2022
90
2022-02-26 00:00:00
2022-04-01 00:00:00
MAR-FY2022
MAR-2022
91
2022-04-02 00:00:00
2022-04-29 00:00:00
APR-FY2022
APR-2022
92
2022-04-30 00:00:00
2022-05-27 00:00:00
MAY-FY2022
MAY-2022
93
2022-05-28 00:00:00
2022-07-01 00:00:00
JUN-FY2022
JUN-2022
94
2022-07-02 00:00:00
2022-07-29 00:00:00
JUL-FY2022
JUL-2022
95
2022-07-30 00:00:00
2022-08-26 00:00:00
AUG-FY2022
AUG-2022
96
2022-08-27 00:00:00
2022-09-30 00:00:00
SEP-FY2022
SEP-2022
97
2022-10-01 00:00:00
2022-10-28 00:00:00
OCT-FY2023
OCT-2022
I want to make a stored procedure that when executed (without receiving parameters) will return the single row corresponding to the date between PeriodFrom and PeriodTo based on execution date.
I have something like this:
Select top 1 Period,
Periodfrom,
Periodto,
Glperiodoracle,
Glperiodcalendar
From Calendar_Period
Where Periodfrom <= getdate()
And Periodto >= getdate()
I understand that using BETWEEN could lead to errors, but would this work in the edge cases taking in account seconds, right?
Looks like (i) your end date is inclusive (ii) the time portion is always 00:00. So the correct and most performant query would be:
where cast(getdate() as date) between Periodfrom and Periodto
It will, for example, return the first row when the current time is 2022-01-28 23:59:59.999.
I have the following table in HANA :
vehicle_id time roaming_time parking_time
1 Sep 01,2016 3:09:03 AM 3 9
2 Sep 01,2016 3:12:03 AM 6 8
1 Sep 01,2016 9:10:03 AM 10 6
4 Sep 01,2016 10:09:03 AM 9 3
1 Sep 01,2016 10:10:03 AM 10 10
4 Sep 01,2016 12:09:03 AM 3 9
from these information I wanted to know that what is the sum of roaming_time and sum of parking_time for each hour from all the vehicles and want the output in the format:
time roaming_time parking_time
____ _____________ ____________
2016-09-01 00:00:00 3 9
2016-09-01 01:00:00 6 8
2016-09-01 02:00:00 9 6
2016-09-01 03:00:00 3 6
2016-09-01 04:00:00 12 3
2016-09-01 05:00:00 15 8
2016-09-01 06:00:00 18 4
2016-09-01 07:00:00 8 3
2016-09-01 08:00:00 9 4
2016-09-01 09:00:00 6 6
2016-09-01 10:00:00 6 9
........
2016-09-01 23:00:00 3 12
I need to group the following query which gives all the sum by hour wise and get the expected result:
select sum(roaming_time) as roaming_time,sum(parking_time) as parking_time
from time>='2016-09-01 00:00:00'
time>='2016-09-01 23:59:59'
I do not know how to do the grouping by hour in HANA. Any help is appreciated
Here is one method . . . it converts the time to a date and hour format:
select to_varchar(time, 'YYYY-MM-DD'), hour(time),
sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t
group by date(time), hour(time)
order by to_varchar(time, 'YYYY-MM-DD'), hour(time);
Use a group by clause with SERIES_ROUND(). Avoid date() and hour() and similar data/time functions on large data sets as they tend to be slower.
select SERIES_ROUND(time, 'INTERVAL 1 HOUR') as time,
sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t
group by SERIES_ROUND(time, 'INTERVAL 1 HOUR')
order by SERIES_ROUND(time, 'INTERVAL 1 HOUR');
Another approach is to convert it to a string, especially if no further time calculations are required.
This could look like this:
select to_varchar(time, 'DD.MM.YYYY HH24') as parking_hour ,
sum(roaming_time) as roaming_time, sum(parking_time) as parking_time from t
group by to_varchar(time, 'DD.MM.YYYY HH24') as parking_hour
order byto_varchar(time, 'DD.MM.YYYY HH24') as parking_hour;
I have a table that looks more less like that:
K_PKEY D_FROM D_TO PERC
============ ==================== ==================== ===========
0013 01-JAN-2009 00:00:00 31-JUL-2011 00:00:00
0013 01-AUG-2011 00:00:00 31-DEC-2011 00:00:00
0013 01-JAN-2012 00:00:00 31-MAR-2012 00:00:00
0013 01-APR-2012 00:00:00 31-DEC-2012 00:00:00 75.000000
0013 01-JAN-2013 00:00:00 31-JAN-2013 00:00:00 50.000000
0013 01-FEB-2013 00:00:00 28-FEB-2013 00:00:00 50.000000
0013 01-MAR-2013 00:00:00 31-AUG-2013 00:00:00 75.000000
0013 01-SEP-2013 00:00:00 31-MAY-2015 00:00:00 75.000000
0013 01-JUN-2015 00:00:00 31-DEC-2100 00:00:00
I'm trying to build a DISTICT query that takes specific date ranges into consideration.
This is what I came up with:
SELECT DISTINCT k_pkey, MIN(d_from), MAX(d_to), perc FROM my_table GROUP BY k_pkey
It doesn't work the way I want and I understand why.
MIN() and MAX() combined with DISTINCT work globally, which is natural for that type of query. This results in:
K_PKEY D_FROM D_TO PERC
============ ==================== ==================== ===========
0013 01-JAN-2009 00:00:00 31-DEC-2100 00:00:00
0013 01-APR-2012 00:00:00 31-MAY-2015 00:00:00 75.000000
0013 01-JAN-2013 00:00:00 28-FEB-2013 00:00:00 50.000000
What I want to achieve is to keep chronological order and combine only ranges that are (so to say) next to each other.
K_PKEY D_FROM D_TO PERC
============ ==================== ==================== ===========
0013 01-JAN-2009 00:00:00 31-MAR-2012 00:00:00
0013 01-APR-2012 00:00:00 31-DEC-2012 00:00:00 75.000000
0013 01-JAN-2013 00:00:00 28-FEB-2013 00:00:00 50.000000
0013 01-MAR-2013 00:00:00 31-MAY-2015 00:00:00 75.000000
0013 01-JUN-2015 00:00:00 31-DEC-2100 00:00:00
Is it possible with one sql query (i don't want to use sql procedure if possible)? Any suggestions?
You are trying to combine adjacent rows together, based on the date and on PERC being the same. The idea is to use a left join to determine which values start a new range. Then, use a cumulative sum to count the number of starts up to each row. This latter value can be used for grouping.
In SQL Server 2012+, the cumulative sum can be done directly. In earlier versions, you would use outer apply.
The resulting query looks like this:
select k_pkey, min(d_from) as d_from, max(d_to) as d_to, perc
from (select t.*,
sum(IsGroupStart) over (partition by k_pkey, perc order by d_from) as grp
from (select t.*,
(case when t_prev.k_pkey is null then 1 else 0 end) as IsGroupStart
from t left join
t tprev
on tprev.k_pkey = t.k_pkey and
(tprev.perc = t.perc or tprev.perc is null and t.perc is null) and
tprev.d_to = dateadd(day, -1, t.d_from)
) t
) t
group by grp, k_pkey, perc;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have the following requirement,
Sample data:
Numberid startDate, Enddate
---------------------------------------------------
1900415115 2012-09-22 00:39:00 2013-10-25 00:00:00
2429398311 2008-05-22 16:57:00 2013-07-30 00:00:00
4337719455 2008-05-22 16:57:00 2009-06-12 00:00:00
6644946399 2008-05-22 16:57:00 2009-01-16 00:00:00
9740698857 2008-05-22 16:57:00 2008-09-26 00:00:00
3928192597 2011-08-24 12:14:00 2012-09-24 00:00:00
1233655116 2013-08-14 00:39:00 2013-12-09 00:00:00
1780419233 2008-10-22 00:08:00 2014-08-24 00:00:00
1912939738 2011-08-26 01:32:00 2014-06-20 00:00:00
3810216146 2008-05-22 16:57:00 2009-01-16 00:00:00
5851814815 2009-10-07 01:00:00 2010-01-25 00:00:00
3692916726 2008-05-22 16:57:00 2008-10-02 00:00:00
3069490750 2008-05-22 16:57:00 2009-08-14 00:00:00
I want to check if the 'numberid' exists in date range and want to group by
01/01/2008 - 01/01/2009 as 0809
01/01/2009 - 01/01/2010 as 0910
01/01/2010 - 01/01/2011 as 1011
01/01/2011 - 01/01/2012 as 1112
Appreciate your quick help!!
Thanks,
can you try with the following data as im not sure it works for enddate null values
Numberid startDate, Enddate
---------------------------------------------------
4405598510 2011-08-06 00:00:00 NULL
2418680054 2011-08-06 00:00:00 2011-12-28 00:00:00
4405598510 2011-08-06 00:00:00 NULL
1810168034 2011-08-06 00:00:00 NULL
6849266569 2011-08-06 00:00:00 2014-09-02 00:00:00
2682265222 2011-08-09 00:58:00 2012-09-20 00:00:00
6253123963 2011-08-09 00:00:00 2011-07-01 00:00:00
8276745680 2011-08-10 00:00:00 2014-06-27 00:00:00
3873103800 2011-08-10 00:00:00 2013-07-16 00:00:00
3703761027 2011-08-06 00:00:00 NULL
1810168034 2011-08-06 00:00:00 NULL
9888909217 2011-08-08 00:00:00 2013-06-30 00:00:00
3034945061 2011-08-09 00:59:00 NULL
4822850747 2011-08-10 00:00:00 2012-08-21 00:00:00
5849710101 2011-08-10 00:00:00 NULL
and also its not yearly 2008, 2009.. 2012..
i need specific dates please.
thanks
Try this
with myTable (
numberid,
startDate,
endDate
) as(
select
numberid,
CONVERT(DATETIME,startDate),
CONVERT(DATETIME,endDate)
from (
values
(4405598510,'2011-08-06 00:00:00',NULL),
(2418680054,'2011-08-06 00:00:00','2011-12-28 00:00:00'),
(4405598510,'2011-08-06 00:00:00',NULL),
(1810168034,'2011-08-06 00:00:00',NULL),
(6849266569,'2011-08-06 00:00:00','2014-09-02 00:00:00'),
(2682265222,'2011-08-09 00:58:00','2012-09-20 00:00:00'),
(6253123963,'2011-08-09 00:00:00','2011-07-01 00:00:00'),
(8276745680,'2011-08-10 00:00:00','2014-06-27 00:00:00'),
(3873103800,'2011-08-10 00:00:00','2013-07-16 00:00:00'),
(3703761027,'2011-08-06 00:00:00',NULL),
(1810168034,'2011-08-06 00:00:00',NULL)
) [ ] (numberid,startDate,endDate)
)
select
Numberid,
startDate,
endDate,
case when 2009 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [0809],
case when 2010 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [0910],
case when 2011 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1011],
case when 2012 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1112],
case when 2013 between year(startDate) and ISNULL(year(endDate),year(startDate)) then 'y' else 'n' end [1213]
from myTable
Edit:
you can do, instead of:
2009 between year(startDate) and ISNULL(year(endDate),year(startDate))
So you can setup the interval, this:
case when startDate <= '2012-01-01' and ISNULL(endDate,startDate) >= '2011-01-01' then 'y' else 'n' end [1011],