SQL Server Select all except data where next row has datediff of less than 10 milliseconds - sql-server

I'm using SQL Server 2017 and i'm facing an issue.
I have the following data (sample seen below).
What i want is to select all the cardnumbers, except these that are followed by an event that contains the same cardnumber while the result value is not ok.
Their time difference between these two events is at most 200 milliseconds (so i believe in the where clause it must be datediff(ms, cardnumberofpreviousevent, cardnumberofnextevent) > 200.
So in this case, the desirable outcome should be all the lines displayed above (line 426 to 433), except the checked line 432.
Can anyone give me some help? It will be much appreciated. Thank you.

Your answer is in your question.
Did you try :
where datediff(ms, cardnumberofpreviousevent, cardnumberofnextevent) > 200
If it does not work, please include your whole query, then I will advise how to alter it...

Related

GQL error when formatting datetime query

I'm attempting to format a GQL query that pulls data between two dates. I've referred to several existing StackOverflow threads (GQL SELECT by date for example), and have tried following the formatting shown there, but for some reason when I test my query out it gives me an error.
Here is the query I'm attempting to use:
SELECT * FROM Packets WHERE timestamp > DATETIME(2017,12,23) AND timestamp < DATETIME(2017,12,29) LIMIT 10
It gives this error:
"GQL query error: Encountered "2017" at line 1, column 50. Was expecting one of: <SINGLE_QUOTE_STRING>, <DOUBLE_QUOTE_STRING>"
I've tried enclosing the dates in strings, I've tried using the DATE object, every format I can think of gives me some sort of error. What am I doing wrong?
The error is right, the DATETIME method needs a single string parameter.
According to the GQL reference, to instanciate a DATETIME in a query the format must be 'YYYY-MM-DDThh:mm:ss.SSSSSS+zz:ZZ':
DATETIME DATETIME() represents a timestamp. must be
in the time format specified in RFC 3339 section 5.6. (However, the
second precision is limited to microseconds and leap seconds are
omitted.) This standard format is: YYYY-MM-DDThh:mm:ss.SSSSSS+zz:ZZ
...
Your example working:
SELECT * FROM Packets WHERE timestamp > DATETIME('2013-09-20T09:30:20.00002-08:00') AND timestamp < DATETIME('2013-09-29T09:30:20.00002-08:00') LIMIT 10
You can check the complete article here :
https://cloud.google.com/datastore/docs/reference/gql_reference
Thanks for comment this problem.
With the answers of each one I can do a very easy (but almost impossible in GQL) Query.
Check this out, I hope it will help to someone:
SELECT * FROM Task WHERE recordDate >= DATETIME('2018-09-09T00:00:00.00000-03:00')
AND recordDate <= DATETIME('2018-09-20T23:59:59.99999-03:00')
Where "2018-09-09T00:00:00.00000-03:00" is the full datetime value and
it means:
2018-09-09 -> Date Indicator (YYYY-MM-DD in my case)
T -> Indicator that the next values are Time values
00:00:00.00000 -> Time Indicator (HH:mm:ss:[miliseconds])
-03:00 -> Time Zone indicator (Chile in my case)
I really hope this post will be useful to anyone that have the same trouble with dates using GQL

Different results with and without a dummy WITH clause

I'm trying to diagnose a bug in a calculated measure in a SSAS cube, and while debugging I get this weird behaviour ...
select
[Measures].[Net Lost Commitments] on columns
, {[Date Dimension].[Fiscal Year Hierarchy].[Fiscal Year].[2009\10],[Date Dimension].[Fiscal Year Hierarchy].[Fiscal Year].[2010\11]} on rows
from Compass3
...returns the following:
Net Lost Commitments
2009\10 (null)
2010\11 9,937
I'm trying to figure out why the value for 2009\10 is null and so I've been adding various other calculations via a WITH clause to help track back the problem. But any WITH clause I add changes the results, even if the calculation in the WITH clause is not used. For instance ...
with
member [Measures].[test] as null
select
[Measures].[Net Lost Commitments] on columns
, {[Date Dimension].[Fiscal Year Hierarchy].[Fiscal Year].[2009\10],[Date Dimension].[Fiscal Year Hierarchy].[Fiscal Year].[2010\11]} on rows
from Compass3
...returns the following:
Net Lost Commitments
2009\10 8,783
2010\11 9,937
Have you ever seen that before? What could possibly be the cause?
Maybe the presence of the calculated member is affecting the solve order for the [Date Dimension] aggregation? Is [Net Lost Commitments] a calculated measure? And if so, can you change its solve order to later in the query processing?
I have re-crafted the measure definitions into a simpler form and this problem seems to have gone away. I still have no idea how this seemingly random behaviour occurs but it is no longer an issue.
--Matt.

SQl Server I need to fix the error of "Timeout expired. The timeout period elapsed"

I know it's been asked a bunch of times but I can't seem to get the fixes I've read to solve my issue. I'm getting the SQL Server error that "the timeout period elapsed prior to completion of the operation or the server is not responding." I changed the setting under Tools>Options>Designers>"Override connection string time- out value" to 120 seconds as per this posting, but... it still times out after 30 seconds. I'm accessing the database from visual studio, working directly with it, and not with ado in client code. I'm open to suggestions... here's the query btw:
SELECT
Symbol
FROM
tblSymbolsMain
WHERE
((SELECT dbo.LatestDateInDailyPricingVolBySymbol(tblSymbolsMain.Symbol) AS Expr1) < dbo.RecentTradingDateByNumber(5))
In a nutshell, the goal is to return all stock symbols from a main symbols table that don't have a daily pricing data point in the pricing table for at least 5 trading days.
As always thanks in advance..
The code doesnt appear to be correct ... you have ...WHERE (SELECT...)
WHERE what?
Are you sure you are not after
SELECT MyCols FROM MyTable WHERE ID IN (...)
OR
SELECT MyCols FROM MyTable WHERE ID NOT IN (...)
Where (...) represents another select returning some sort of ID.
Otherwise of course you'd get a timeout. That select may return a count and WHERE 1 can go on and on and on...

Increment a row in SQL Server database after the LAST row within a range

I have tried looking for an answer for my question but due to lack of proper knowledge in databases, I thought some genius here could help me out.
I'm trying to add rows into a database table starting from the number 800 and onwards in a sequential order. The database currently has records from 1 up until 300.
This record in the table is not automatically incremented, but assigned manually.
I basically want to start another batch of numbers, but rather than incrementing them from where the program left off (i.e. from 300) I would like to start them from 800.
I tried different SQL statements but got nothing concrete to go with. I'm using PHP to enter these rows.
I feel like this shouldn't be so hard but I'm a novice and it kills!
I thought maybe adding one record that starts from 800 and then executing a statement like
SELECT LAST FROM A RANGE BETWEEN 800 AND 899
would be the way to go.
Any help, advice?
You can obtain the highest numbered record with the MAX aggregate function. The following will find the next highest number that is between 800 and 899.
SELECT MAX(my_assigned_number) + 1 as next_number
FROM my_table
WHERE my_assigned_number > 800 AND my_assigned_number < 899;

Query timeout, depending on dates

we have some user forms that include a 'period' menu, where the user can request the server to return data for a specific date range, such as "Purchase Orders issued between the 1st and the 10th of October 2008".
The logic is then to add "on the fly" the date range to the original sql query and requery the data. The syntax of the filter added to the query is:
WHERE myDate >=dateMin and myDate <= dateMax
or, if the original query already has a filter clause:
WHERE <original filter> AND (myDate >=dateMin and myDate <= dateMax)
With both dateMin and dateMax in the 'YYYYMMDD' format.
We began yesterday to get some query timeouts for our 'errors' form, which specifically requests our 'errors' table. After some tests, it appeared this timeout issue was happening only when data was requested from the Error table and for the month of October. The same query, with the very same syntax (only dateMin and dateMax values are changed) when sent with another range (september, august, or whatever) on the same table, did not time out! This happened either when sent from the app or directly from the Sql Server Management Studio.
We avoided the timeout problem by adding an index on the errorDate column of the Errors Table (we should have done this before, I know, but we forgot!). We still have a query delay which is 4 to 5 times longer than the standard one when requesting on October's dates!
We have been trying to query on smaller intervalls, like "first 15 days", "last 15 days". The "first 15 days" query takes longer than the last 15 days one, which is still significantly slower than queries on other periods.
I feel like the problem is only avoided, not really solved, and I still feel quite disturbed by this behaviour. Has anyone ever noticed such weird things, or does anyone have any idea about what is happening?
I recommend you update your statistics and rebuild the indexes.
Also, note that you can use BETWEEN, e.g.:
WHERE myDate BETWEEN dateMin and dateMax

Resources