简体   繁体   中英

Query in SQL Server 2000

I have 2 SQL Server databases with same structure, first one in SQL Server 2008 and the other in SQL Server 2000.

I wrote a query in SQL Server 2008 like this :

SELECT
    sph.SiProforma, SuProforma, cps.Tp_FamilyOffice_Name,
    DsProforma, NaProformaFee, NqCount, 
    NaProformaFee * NqCount as jameradif,
    SUM(NaProformaFee * NqCount) OVER (PARTITION BY suProforma) AS ghabelepardakht 

and it works in SQL Server 2008, but when I run it in SQL Server 2000 I get this error :

Incorrect syntax near the keyword 'OVER'.

How can I fix it? Or replace code ?

SQL Server 2000 doesn't support OVER . Write a sub query that performs a SUM(..) GROUP BY suProforma and join the sub query to the main table on suProforma

I'd have written an example for you using your tables but for 2 reasons: one that, I'm posting from an iPhone 4 and reformatting your query (it's a bit of a mess- you will do yourself more favours keeping your code neater) is very very hard work and 2, it's not obvious where suProforma column is kept in which table

Here is a simpler example you can apply to your query:

SELECT
    t1.*,
    sq1.summed
FROM
    table1 t1
INNER JOIN
    (SELECT sum(a*b) as summed FROM table1 GROUP BY suProforma) sq2 ON T1.suProforma = sq2.suProforma

To apply this query to your situation replace table1 with the table that has suProforma and correct the SUM(a*b) with your actual columns. Then add your other joins in

If he columns you are grouping by and summing are in different tables, then you will have to write a query that links the two together and paste that in (surrounded by brackets) in place of table1

For example these two things are the same:

SELECT *
FROM table1

SELECT *
FROM (select * from table1) sq1

Get into the habit of looking at any entity in the FROM part of a query as "just a rectangular block of data from which I can select results"

This query will work in both SQL Server 2000 and 2008

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM