Group By,Order by DateTime - sql

I have nearly 15000 data rows with the first column containing date in the format:
2012-05-10 09:00:00.000
I need this data to be sorted by year then month, then day, then hour so for example:
2012-05-10 09:00:00.000
2012-05-10 10:00:00.000
2012-05-10 11:00:00.000
2012-05-10 12:00:00.000
2012-05-11 09:00:00.000
2012-05-11 10:00:00.000
2012-05-11 11:00:00.000
2012-05-11 12:00:00.000
2012-06-01 02:00:00.000
2012-06-01 03:00:00.000
2012-06-01 04:00:00.000
2012-06-01 05:00:00.000
Current SQL Query to do this is below:
SELECT MIN(Datetime)
GROUP BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
HAVING MIN(jmusa_LOG1.DateTime) NOT IN(SELECT DateTime FROM AverageRawData)
ORDER BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)

You are describing a normal date sort, so you can just do:
select MyDate
from AverageRawData
order by MyDate
If you don't want duplicates, add DISTINCT like this:
select distinct MyDate
from AverageRawData
order by MyDate
If this does not meet your requirements, please provide sample data used to generate your output example.

Related

get time series in 8 hours of interval

I am generating one time-series from using the below query.
SELECT * from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::timestamp),
'2021-12-13 10:01:38'::timestamp,
concat(480, ' minutes')::interval) as t(time_ent)) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and it will give me output like below.
2021-11-13 18:00:00.000
2021-11-14 02:00:00.000
2021-11-14 10:00:00.000
2021-11-14 18:00:00.000
2021-11-15 02:00:00.000
but I need output like.
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
currently, the time series hours depend upon the timestamp that I pass. in above it gives me hours like 02,10,18...but I want the hours like 00,08,16...hours should not depend on the time I passed in query. I tried many things but not any success.
as your start of generate_series is set to 10:00:00, so your next step will be 18:00:00
you have to start your serie from 00:00:00 (cast to date) e.g.:
SELECT
time_ent::timestamp without time zone
from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::date),
'2021-12-13 10:01:38'::timestamp ,
concat(480, ' minutes')::interval) as t(time_ent)
) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and the result will be:
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
2021-11-15 08:00:00.000

Sum where values of a column matches without number of rows changing

I am trying to values of a column where values of another column matches. Below is a sample of my data.
DT No_of_records LD_VOY_N LD_VSL_M
2017-05-06 04:00:00.000 7 0002W pqo emzmnwp
2017-05-06 20:00:00.000 6 0002W pqo emzmnwp
2017-05-02 04:00:00.000 1 0007E omq ynzmeoyn
2017-05-01 08:00:00.000 2 0016W rmhp sunhpnw
2017-05-01 12:00:00.000 1 0016W rmhp sunhpnw
2017-05-05 12:00:00.000 2 0019N omq wqmsy
2017-05-06 04:00:00.000 12 0019N omq wqmsy
Below is my desired output
DT No_of_records LD_VOY_N LD_VSL_M Total_no_of_records
2017-05-06 04:00:00.000 7 0002W pqo emzmnwp 13
2017-05-06 20:00:00.000 6 0002W pqo emzmnwp 13
2017-05-02 04:00:00.000 1 0007E omq ynzmeoyn 1
2017-05-01 08:00:00.000 2 0016W rmhp sunhpnw 3
2017-05-01 12:00:00.000 1 0016W rmhp sunhpnw 3
2017-05-05 12:00:00.000 2 0019N omq wqmsy 14
2017-05-06 04:00:00.000 12 0019N omq wqmsy 14
I am trying to find the Total_no_of_records column. Do you have any ideas?
You seem to want a window function by LD_VOY_N:
select t.*,
sum(No_of_records) over (partition by LD_VOY_N) as Total_no_of_records
from t;
select DT,No_of_records,LD_VOY_N,LD_VSL_M ,COUNT(DISTINCT (DT,No_of_records,LD_VOY_N,LD_VSL_M )) as Total_no_of_records from tablename
group by DT,No_of_records,LD_VOY_N,LD_VSL_M

Count number of records within the same time period

I am trying to count the total number of records that have been added in at a specific time. Below is a sample of my data.
CNTR_N LOAD_VESSEL_M VOYAGE_OUT_N
HGTU 4615032 opgqqun 039E
TCNU 5590060 plq jpxxqyi 016E12
PCIU 1189368 iunpj igspnw 310N
CLHU 3193420 qpji oi 735S
RFSU 2000199 unqy ihpj 003NN
OOLU 1543519 mmaq ywclh 004E11
TFTU 8600600 epn vpu 490 W037
MSKU 5414708 syyhvmfyn 1708
SNAP_DT
2017-04-25 20:00:00.000
2017-04-25 20:00:00.000
2017-04-25 20:00:00.000
2017-04-25 20:00:00.000
2017-05-03 16:00:00.000
2017-05-03 16:00:00.000
2017-05-03 16:00:00.000
2017-05-03 16:00:00.000
Below is my desired output. I am trying to get the No_of_records column.
SNAP_DT No_of_records
2017-04-25 20:00:00.000 4
2017-05-03 16:00:00.000 4
Do any of you have ideas on how to get the above output? Would really appreciate your help.
You Can Use Group By clause with aggregate function Count.
Assuming your table name is table1, below is the query that will return your desired result.
SELECT snap_dt, Count(*)
FROM table1
GROUP BY snap_dt;
Try this:
SELECT
SNAP_DT
,COUNT(*)
FROM data
GROUP BY SNAP_DT

SQL Find Datetime outside Datetime range

I have 2 tables one called Production and the other called Schedule.
I am trying to find is there is some production outside the schedule.
So far I am getting duplicated value because the production could be inside one schedule but outside the other one.
So far I have no luck with this sql query I was wondering if someone can point me to the right direction.
thanks in advance.
SELECT TB1.*
FROM Production AS TB1
INNER JOIN Schedule AS TB2
ON TB1.ProduceDate < TB2.StartDate OR TB1.ProduceDate > tb2.EndDate
GROUP BY TB1.ID,TB1.ProduceDate
ORDER BY Tb1.ProduceDate
ID Produce Date
1 2017-02-03 09:00:00.000
2 2017-02-03 11:00:00.000
3 2017-02-03 13:00:00.000
4 2017-02-03 18:00:00.000
7 2017-02-03 19:00:00.000
5 2017-02-03 20:00:00.000
6 2017-02-03 23:00:00.000
Production Table Data
ID ProduceDate
1 2017-02-03 09:00:00.000
2 2017-02-03 11:00:00.000
3 2017-02-03 13:00:00.000
4 2017-02-03 18:00:00.000
5 2017-02-03 20:00:00.000
6 2017-02-03 23:00:00.000
7 2017-02-03 19:00:00.000
Schedule Table Data
ID StartDate EndDate
1 2017-02-03 10:00:00.000 2017-02-03 12:00:00.000
2 2017-02-03 15:00:00.000 2017-02-03 19:00:00.000
I think you just want not exists:
select p.*
from production p
where not exists (select 1
from schedule s
where p.producedate >= s.startdate and
p.producedate <= s.enddate
);
select Production.*
from Production
left join Schedule
on ProduceDate between StartDate and EndDate
where Schedule.id is null

Why the Lookup function in SSRS is failing

I have two tables in my database as follows:
SELECT TOP 12 [Date], [Amount]
FROM [Db].[dbo].[final]
UNION
SELECT MAX([F1]) 'Date', SUM([F2]) 'Amount' FROM (
SELECT TOP 5 [F1], [F2]
FROM [Db].[dbo].[origtable]
WHERE [F1] IS NOT NULL
ORDER BY [F1] DESC
) c
SELECT TOP 12 [date_period2] AS [Date], [trnchargeamt] AS [Amount]
FROM [Db].[dbo].[othertable]
ORDER BY [date_period2] DESC
Which displays the following (first query is the top and second query is the bottom):
Date Amount
2013-07-31 00:00:00.000 9658254
2013-08-31 00:00:00.000 6659659
2013-09-30 00:00:00.000 14256326
2013-10-31 00:00:00.000 8912215
2013-11-30 00:00:00.000 9326659
2013-12-31 00:00:00.000 10211985
2014-01-31 00:00:00.000 8652365
2014-02-28 00:00:00.000 16256326
2014-03-31 00:00:00.000 24454342
2014-04-30 00:00:00.000 16345908
2014-05-30 00:00:00.000 6976515.77
2014-05-30 00:00:00.000 23578640
Date Amount
201406 42492.78
201405 1846703.374
201405 44390961.65
201404 45413273.91
201403 46943807.39
201402 33744223.24
201401 41630266.94
201312 40672523.92
201311 42465408.47
201310 47878730.59
201309 39444986.25
201308 40554376.28
In my SSRS I have it like the following way to display the charts:
I am using the following lookup function to display the first table with the second table:
=Lookup(Fields!Date.Value,Fields!Date.Value, Fields!Amount.Value, "WMGDailyPaymentsRed")
I get the following warning and nothing is displayed for the second chart:
[rsRuntimeErrorInExpression] The Y expression for the chart ‘Chart8’ contains an error:
Exception of type 'Microsoft.ReportingServices.ReportProcessing.ReportProcessingException_ComparisonError' was thrown.`
As you can see from the results of your select statement the first SQL.. has an actual Date and the second sql has a date period with just year and month.. hence comparing both would not work. You would need to modify your first query to format the date into YYYYMM format and then it would work in the lookup.