Last DateTime from JSON in Azure Stream Analytics - azure-stream-analytics

I am having an issue in ASA when trying to get the "Max" DateTime.
My code is:
LAST(Timestamp) OVER (PARTITION BY DeviceId LIMIT DURATION(minute, 5)) AS DateTime,
It is coming up with an error saying it is not in an aggregate function or in the group by. Im not sure what I am doing wrong here. Any help would be appreciated!

LAST is not an aggregate function and cannot be used in GROUP BY statements. It is most typically used in SELECT statements together with WHEN clause to "look back" and find event matching specific condition. Please check examples here
If what you want is to find biggest timestamp in the time window, you can do something like this:
SELECT MAX(CAST(Timestamp AS DateTime)) AS DateTime
FROM input
GROUP BY DeviceId, TumblingWindow(minute, 1)

Related

Access query expression returns incorrect

I'm working on a query that pulls a date from another query, I have my reasons for the nesting. The problem I'm facing is that there is a field that is called DueDate.
My SQL is
SELECT DueDate
FROM qryDueDates
WHERE DueDates <= DateAdd("d",60,Date())
The data causing the issue is when it equals something like "1/25/2019", "11/19/2019" or any date in 2019.
Goal
I need to limit the results to show dates that are expired or expiring within 60 days or less.
I'm trying to prepare the dataset for the conditional formatting.
if you can put your nested sub-query in your post that may give better picture, and if you can mention what is the error you are getting that may also help. Since you mentioned that you are getting error only when sub-query returns certain dates, I would suggest that cast your sub-query result to DATE if you have not already done.
Below is my attempt to help you with limited information I could extract from your post. I have used some of MS-SQL function below, please replace with your DB specific function.
SELECT myDates.* FROM (select COLUMN_NAME DueDates from TABLE_NAME) as myDates WHERE myDates.DueDates <= DateAdd("d",60, GETDATE())
Turns out that the original query was screwing it up. I moved the query into the main one and it worked.

Select and manipulate SQL data, DISTINCT and SUM?

Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'

Need help limiting results of a function at the end of my query

I have a function below that I'm using under my SELECT statement that I'm trying to limit the results of in my where statement but can't seem to figure out how to name it or tell it to reference that specific function. I'm thinking I can accomplish this by putting the function in its own WITH statement, but would rather just do it this way if possible.
SELECT
DAYS (CURRENT DATE) - DAYS (DATE(B.TSTAMP)) AS "TRANSIT"
From this function I want to put "WHERE TRANSIT >= '8'" When I do this now, I get the error "TRANSIT" is not valid in the context where it is used. Any suggestions?
You can't refer to something from the SELECT clause in your WHERE, it processes the WHERE first (see this article).
You can "fix" your problem by pushing your query into a sub-query, and then referring to the name:
SELECT *
FROM (
SELECT DAYS (CURRENT DATE) - DAYS (DATE(B.TSTAMP)) AS "TRANSIT"
FROM your_table
) A
WHERE TRANSIT >= 8

Need Alternative to SQL AVG() for Obvient BI tool

I am reading 2 fields from 1 table.
StartKey and Mins
Image below shows my current output result on left and what I need on right.
Here is my Query
Select
StartKey,
Duration as Mins
From TableA
Where Flag = 0
Order by StartKey
I know I can use avg(duration), but if I use that, Obvient, the software I am using to write and display the query, won't let me take the average of column Mins Avg itself.
This error I get after I manually insterted average code of column in CS file and then I try to edit column properties.
First, let me make sure I understand your problem.
You are using the SQL from your post while building something in Obvient which appears to be a Business Intelligence platform. The problem you are having is that you are unable to perform an average function in Obvient on the column of averages in your SQL query.
If that is correct, you should use your SQL query to create a view in the database which should appear to Obvient as a table and may allow you to perform the averaging function. I can't say for certain that this will solve your issue having never used Obvient, but give that a try and let us know how that works for you.
Seems like I'm missing something, but to get your desired results, this should work:
Select
StartKey,
AVG(Duration) as Mins
From TableA
Where Flag = 0
Group By StartKey
Order by StartKey
And the SQL Fiddle.
If your goal is to get the AVG(Mins) from the above query, you could use a subquery to return that:
Select AVG(Mins)
FROM (
SELECT
StartKey,
AVG(Duration) as Mins
From TableA
Group By StartKey
) t
Here is the Fiddle:
Good luck.

Grouping result by date in mysql

This SQL statement
SELECT `ip`, `when` FROM `metrics` WHERE `vidID` = '1' GROUP BY DATE('when')
returns one result, even though multiple are present in the table with different dates. I've tried using DATE_FORMAT as well. Am I doing something stupid?
When is a timestamp column with full timestamp, including hours, minutes and seconds. I'm trying to just group by results by day/month/year.
Looks like you're grouping by the constant string 'when' instead of the field when.
Use this instead:
GROUP BY DATE(`when`)
Sounds like you want to count the IP addresses for a given date:
SELECT COUNT(`ip`) AddressCount, `when`
FROM `metrics`
WHERE `vidID` = '1'
GROUP BY DATE(`when`)
Usually, GROUP BY is used in conjunction with some aggregate function (like SUM, say) to compute the result set by one or more columns. What are you trying to accomplish here? Do you mean to sort, or just get a collapsed list of which IPs have records on which dates?