SQL Server 2008 R2 returning inconsistent results - performance

This query returns results that aren't grouped in the way the query expects it to. This started only happening recently and has been occurring at least twice a week.
SELECT
UserN, Program, CPair, PosCcy1, OpenPosOpt, CrossR,
OptPrem, DailyOptValue, OptPandL
FROM (
(SELECT
UserN, Program, CPair,PosCcy1,OpenPosOpt,RevalC1,
RevalC2, CrossR, OptPrem,DailyOptValue,OptValue, OptPandL
FROM
(SELECT
UserN, Program, CPair,
ROUND(SUM(Ccy1Amount),4) AS PosCcy1,
ROUND(SUM(OpenOptPos),4) AS OpenPosOpt,
MAX(RevalC1) as RevalC1,
MAX(RevalC2) as RevalC2,
case when MAX(RevalC2) = 0 and MAX(RevalC1) = 0 then 0
else MAX(RevalC2)/MAX(RevalC1) end as CrossR,
ROUND(SUM(OptPrem),2) AS OptPrem,
ROUND(SUM(DailyOp),4) AS DailyOptValue,
ROUND(SUM(OptValue),4) AS OptValue,
ROUND(SUM(OpPandL),4) AS OptPandL,
sum(Prem) as Prem
FROM
AReport rr
GROUP BY
UserN, Program, CPair) a
WHERE
not(PosCcy1= 0 and OpenPosOpt= 0 and Prem = 0)
)) results
The 'AReport' is a view.
As could be seen I expect the query to return the data grouped by User, Program and CPair. Almost always I get that result, but all of a sudden at times the results aren't grouped like that.
When this happens SQL Server returns the un-grouped results the whole day. I've run parts of the query on SSMS during then and the results I get are grouped as expected for the two inner nested selects. The moment I highlight the outer most select to be included the results set is un-grouped.
I've looked for errors in the error log and the event viewer which doesn't seem to suggest anything. This behaviour is automatically rectified by the following day. Could someone tell me if there's anything in the way the query is written that's causing this behaviour.

Read this
http://msdn.microsoft.com/en-gb/library/ms189822.aspx
and review your query

Finally found that the inconsistency was the data displayed was almost always ordered in the desired way . See Why does SQL Server 2008 order when using a GROUP BY and no order has been specified?
Adding an order by to the query fixed the problem.

Related

How to solve (data source adapter error: java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000) in cognos?

data source adapter error: java.sql.SQLSyntaxErrorException: ORA-01795: maximum number of expressions in a list is 1000
I am getting this error in cognos.Basically I am working on report enhancement in cognos.I already have searched for solution on internet but provided solution is for oracle, i want solution to be in cognos as i am unable to access database or framework manager. Please let me know what should be the right approch to resolve that error.
enter image description here
I already have searched for solution on internet but provided solution is for oracle. I want the solution to be in cognos as i am unable to access database or framework manager.
Error you got is indeed raised by Oracle, but the question is whether it should be fixed in Oracle or elsewhere (Cognos?).
Generally speaking, this is what happened:
select whatever from some_table
where id in (1, 2, 3) --> this is the IN clause. In Oracle, it is limited
to 1000 elements
I guess that nobody literally types 1000+ elements into the IN list, but some dynamic piece of code might, concatenating value after value until you reach and pass the limit.
I don't know Cognos, but - if that's what really happens, a simple and effective option is to store values which are being used in IN list into a table, and then either JOIN that table to other table(s), or use it as a subquery.
For example:
select whatever from some_table
where id in (select id from a_new_table)
or
select whatever
from some_table a join a_new_table b on a.id = b.id
Presumably the filter is coming from a prompt and someone is selecting everything in the list in the prompt or close to it.
It might be a good idea to try to figure out why there are so many things being chosen. This would require investigation about the business purpose of the prompt.
It might be that our someone needs to see everything so the prompt should be set as optional, which would not generate the where predicate in the SQL.
It might be that the prompt needs to be generated on an attribute (column) which is at a higher grain of dimensional abstraction (i.e. countries rather than cities).
Simply being told fix it in Cognos is not a very helpful instruction and you need to approach the problem with a better understanding of the purpose of the report.

Query on SNOWFLAKE.ACCOUNT_USAGE schema taking long time to run

I am trying to run a simple query against any of the tables contained in SNOWFLAKE.ACCOUNT_USAGE schema but for some reason it is taking up a long time to run even if I try to limit it to show only the first row, like the following example:
SELECT * FROM "SNOWFLAKE"."ACCOUNT_USAGE"."ACCESS_HISTORY" limit 1;
Is that a normal behavior? If not, can someone help me to figure out why this is happening?
Its always a good practice to add a WHERE condition so the optimizer can make use of query pruning.
If you know your objects were accessed within the past say 24 hours, can you add a date filter and see if that helps?
SELECT * FROM "SNOWFLAKE"."ACCOUNT_USAGE"."ACCESS_HISTORY"
WHERE QUERY_START_TIME > CURRENT_DATE() - 1
limit 1;
More info on mirco-partitions and query pruning: https://docs.snowflake.com/en/user-guide/tables-clustering-micropartitions.html#query-pruning

ORA-01861 literal does not match format error when running within larger query

I'm a newbie using Oracle SQL Developer, and running into some issues. When I run the following smaller sample query by itself, it runs fine:
SELECT ((SYSDATE - TO_DATE(table1.a_certain_date_field, 'DD-MON-RR')) / 365.0)
FROM &a_table
WHERE rownum < 5 AND
((SYSDATE - TO_DATE(table1.a_certain_date_field, 'DD-MON-RR')) > 365.0);
But once I embed this into a larger query, I get an ORA-01861 error: literal does not match format string. For example, when I include it with other WHERE conditions, such as
...AND NOT (adr.state = 'WA')
AND
((SYSDATE - TO_DATE(table1.a_certain_date_field, 'DD-MON-RR')) > 365.0)...[more conditions]
What are some reasons why this would occur? My guess is that the table field changed the way that dates were inputted (ie. from DD-MM-YYYY to DD-MON-RR) at some point and caused this error to be thrown, but wouldn't take likewise throw a similar error in the smaller sample code?
This is always (in my experience) a data issue. you can try to narrow it down by looking at various subsets of the data to find out which row/rows are causing it.

Is it possible to display the result of complex query in SSRS 2012?

I'm new in SSRS and I want to know if it's possible to display a result of a complex query (which run perfectly on SSMS) in my report?
The problem is that I have an Over statement in my Query:
LEAD(Flow.MeasurementDate) OVER (Partition by
ComponentId, NominationCycle, LocationType, Frequency, FlowTypeId, UnitofMeasurement
ORDER BY MeasurementDate) nextMeasurement,
MAX(Flow.MeasurementDate) OVER (PARTITION BY
ComponentId, NominationCycle, LocationType, Frequency, FlowTypeId, UnitofMeasurement
) maxMeasurement
The error message is:
The OVER SQL construct or statement is not supported.
If it's possible please give me few steps to achieve this.
The Over construct is not supported by the SSRS query designer but the report should still work when the SQL is sent to Sql Server for processing.
If it doesn't, check that your database isn't running in a compatibility level where the Over construct is unsupported (80 or earlier).
If all else fails, an alternative is to put your query in a stored procedure and call that from SSRS.
That's just what SSRS is used for in general.
The steps are the same, no matter if it's a simple or complex query.
You use your query to fill a dataset and display this in a table on your report.
What you need is just a basic SSRS tutorial as it can be found here:
http://msdn.microsoft.com/en-us/library/ms167305.aspx

Cache and SqlCacheDependency (ASP.NET MVC)

We need to return subset of records and for that we use the following command:
using (SqlCommand command = new SqlCommand(
"SELECT ID, Name, Flag, IsDefault FROM (SELECT ROW_NUMBER() OVER (ORDER BY #OrderBy DESC) as Row, ID, Name, Flag, IsDefault FROM dbo.Languages) results WHERE Row BETWEEN ((#Page - 1) * #ItemsPerPage + 1) AND (#Page * #ItemsPerPage)",
connection))
I set a SqlCacheDependency declared like this:
SqlCacheDependency cacheDependency = new SqlCacheDependency(command);
But immediately after I run the command.ExecuteReader() instruction, the hasChanged base property of the SqlCacheDependency object becomes true although I did not change the result of the query in any way! And, because of this, the result of this query is not kept in cache.
HttpRuntime.Cache.Insert( cacheKey, list, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(AppConfiguration.CacheExpiration.VeryLowActivity));
Is it because the command has 2 SELECT statements? Is it ROW_NUMBER()? If yes, is there any other way to paginate results?
Please help! After too many hours, a little will be greatly appreciated! Thank you
Running into the same issue and finding the same answers online without any help, I was reasearching the xml invalid subsicription response from profiler.
I found an example on msdn support site that had a slightly different order of code. When I tried it I realized the problem - Don't open your connection object until after you've created the command object and the cache dependency object. Here is the order you must follow and all will be good:
Be sure to enable notifications (SqlCahceDependencyAdmin) and run SqlDependency.Start first
Create the connection object
Create the command object and assign command text, type, and connection object (any combination of constructors, setting properties, or using CreateCommand).
Create the sql cache dependency object
Open the connection object
Execute the query
Add item to cache using dependency.
If you follow this order, and follow all other requirements on your select statement, don't have any permissions issues, this will work!
I believe the issue has to do with how the .NET framework manages the connection, specifically what settings are set. I tried overriding this in my sql command test but it never worked. This is only a guess - what I do know is changing the order immediately solved the issue.
I was able to piece it together from the following to msdn posts.
This post was one of the more common causes of the invalid subscription, and shows how the .Net client sets the properties that are in contrast to what notification requires.
https://social.msdn.microsoft.com/Forums/en-US/cf3853f3-0ea1-41b9-987e-9922e5766066/changing-default-set-options-forced-by-net?forum=adodotnetdataproviders
Then this post was from a user who, like me, had reduced his code to the simplest format. My original code pattern was similar to his.
https://social.technet.microsoft.com/Forums/windows/en-US/5a29d49b-8c2c-4fe8-b8de-d632a3f60f68/subscriptions-always-invalid-usual-suspects-checked-no-joy?forum=sqlservicebroker
Then I found this post, also a very simple reduction of the problem, only his was a simple issue - needing 2 part name for tables. In his case the suggestion resolved the issue. After looking at his code I noticed the main difference was waiting to open the connection object until AFTER the command object AND the dependency object were created. My only assumption is under the hood (I have not yet started reflector to check so only an assumption) the Connection object is opened differently, or order of events and command happen differently, because of this association.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bc9ca094-a989-4403-82c6-7f608ed462ce/sql-server-not-creating-subscription-for-simple-select-query-when-using-sqlcachedependency?forum=sqlservicebroker
I hope this helps someone else in a similar issue.
Just a guess, but could it be because your SELECT statement doesn't have an ORDER BY clause?
If you don't specify an explicit ordering then it's possible for the query to return the results in any order each time it is run. Maybe this is causing the SqlCacheDependency object to think that the results have changed.
Try adding an ORDER BY clause:
SELECT ID, Name, Flag, IsDefault
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY #OrderBy DESC) AS Row,
ID, Name, Flag, IsDefault
FROM dbo.Languages
) AS results
WHERE Row BETWEEN ((#Page - 1) * #ItemsPerPage + 1) AND (#Page * #ItemsPerPage)
ORDER BY Row
i'm no expert on SqlCacheDependency, in fact, i found this question whilst looking for answers to my own issues with it! However, i believe the reason your SqlCacheDependency is not working is because your SQL contains a nested sub query.
Take a look at the documentation which lists what you can/can not use in your SQL: Creating a Query for Notification
"....The statement must not contain subqueries, outer joins, or self-joins....."
I also found some invaluable troubleshooting info from a guy at Redgate here: Using and Monitoring SQL 2005 Query Notification that helped me solve my own problem: By using Sql Profiler to trace the QN events he suggests, i was able to spot my connection was incorrectly using the 'SET ARITHABORT OFF' option, causing my notifications to fail.

Resources