Alternative to "rows UNBOUNDED PRECEDING" in SQL Server 2005 - sql-server-2005

I am working on a conversion of a series of teradata statements to SQL Server 2005.
Everything is ok until we came across this :
select
CODE, _year, _month, _day, product,
sum(DAILY_AMMOUNT) over (partition by CODE, _year, _month, product
order by _day ROWS UNBOUNDED PRECEDING) as DAY_AMM
from ...
As you for sure know there is no ROWS in SQL Server 2005.
I know that in some cases I could replace it with RANGE, but I don't have access to the teradata database to check if this is indeed the case.
Any idea on how I can replace the sum to be sure that the result will be matching?
Thanks in advance

Related

How does the window SUM function work with OVER internally?

I'm trying to understand, how window function works internally.
ID,Amt
A,1
B,2
C,3
D,4
E,5
If I run this, will give sum of all amount in total column against every record.
Select ID, SUM (AMT) OVER () total from table
but when I run this, it will give me cumulative sum
Select ID, SUM (AMT) OVER (order by ID) total from table
Trying to understand what is happening when its OVER() and OVER(order by ID)
What I've understood is when no partition is defined in OVER, it considers everything as single partition. But not able to understand when we add order by Id within over(), how come it starts doing cumulative sum ?
Can anyone share what's happening behind the scenes for this ?
That is an interesting case, based on the documentation here is the explanation and example.
If PARTITION BY is not specified, the function treats all rows of the
query result set as a single partition. Function will be applied on
all rows in the partition if you don't specify ORDER BY clause.
So if you specifiey ORDER BY then
If it is specified, and a ROWS/RANGE is not specified, then default
RANGE UNBOUNDED PRECEDING AND CURRENT ROW is used as default for
window frame by the functions that can accept optional ROWS/RANGE
specification (for example min or max).
So technically these two commands are the same:
SELECT ID, SUM(AMT) OVER (ORDER BY ID) total FROM table
SELECT ID, SUM(AMT) OVER (ORDER BY ID RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) total FROM table
More about you can read in the documentation:https://learn.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-ver15
This is not related to Oracle itself, but it's part of the SQL Standard and behaves the same way in many databases including Oracle, DB2, PostgreSQL, SQL Server, MySQL, MariaDB, H2, etc.
By definition, when you include the ORDER BY clause the engine will produce "running values" (cumulative aggregation) inside each partition; without the ORDER BY clause it produces the same, single value that aggregates the whole partition.
Now, the partition itself is mainly defined by the PARTITION BY clause. In its absence, the whole result set is considered as a single partition.
Finally, as a more advanced topic, the partition can be further tweaked using a "frame" clause (ROWS and RANGE) and by a "frame exclusion" clause (EXCLUDE).

Count(*) with order by not working on PostgreSQL which works on Oracle

Below is the Sql query which works on oracle but not working on PostgreSQL.
select count(*) from users where id>1 order by username;
I know that order by has no meaning in this query but still why it's working on oracle. Below is error on PostgreSQL
ERROR: column "users.username" must appear in the GROUP BY clause or be used in an aggregate function
Position: 48
SQLState: 42803
PostgreSQL version 9.6.3
As seen by Oracle's execution plan, there is no sorting after the rows are aggregated, which suggests that the SQL engine Oracle has implemented ignores that phrase.
Why doesn't it work in PostgreSQL -- because the people running Postgres know what they're doing ;) Just kidding, but that question would be highly speculative for me, without seeing the Oracle vs MySQL source. The bigger questions is if Oracle and MySQL allow for this by coincidence, or because Oracle owns both.
Final note:
If you're going to ask why similar software applications behave differently, I think it's also important to include what version you're referring to. Even different versions of the same application may follow different instructions.
If you are looking for the count of all records only, then there is no need of order by clause because it has no meaning even in oracle also. In such case remove order by.
select count(*) from users where id>1
If you are looking for username wise count, then there is a meaning of sorting on username and in such case you can use following query.
select count(*) from users where id>1 group by username order by username;
Hope your doubt will be cleared.
You can use a with statement for doing things like MySQL in PostgreSQL.
with cnt (cnt1) AS ( select count(*) as cnt1 from sample )
select *, c.cnt1 as len from sample ,cnt as c;

SQL Server 2012 Management Studio not returning more than 10,000 rows

I am running a simple query like
SELECT *
FROM [LyncConversationDetail].[dbo].[MessageDetails]
WHERE ProjectId = '13'
but I am not getting more than 10,000 rows as a result, not sure what am I missing here as I don't have much knowledge in this. Is there any SQL Server setting for max row count? Or is it dependent on something else?
Try to check how many rows is in Your table.
SELECT count(*)
FROM [LyncConversationDetail].[dbo].[MessageDetails] where ProjectId='13'

Why ordering of rows in a sql table changes automatically

I am creating a table T1 in sql ordered by dates descending.
But when I SELECT * FROM T1, then the order changes.
PS: This didn't happen when I was working in SQL Server 2008 but now I am working on SQL Express 2012 and it's creating problems.
generally this changes should not effect by changing a version. Could you please check your Table level Index as well.
Thanks
NAdir

Query between SQL server and Client side

I create a query: Select * from HR_Tsalary where month='3' and year ='2010' the result is 473 records and I found 2 duplicate record, then I create another query to find duplicate record only: SELECT Emp_No, COUNT() FROM HR_Tsalary WHERE year = '10' AND month = '3'GROUP BY Emp_No HAVING COUNT() > 1 the result is zero record from client side (thru Visual Basic Adodb code). But when I use same query from server the result is 2 records. Is there any different when create a query between from server side and client side?
You could start SQL Server Profiler then run your VB code, and see the exact query that's hitting the database and make sure it is what you're expecting.
-Krip
SQL server profile is always open also I identified that from client side some functions are working such as select, order by but some functions are not working such as group by, sum, count, having