简体   繁体   English

SQL Server 2000中的查询

[英]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. 我有2个具有相同结构的SQL Server数据库,第一个在SQL Server 2008中,另一个在SQL Server 2000中。

I wrote a query in SQL Server 2008 like this : 我在SQL Server 2008中这样写了一个查询:

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 : 它可以在SQL Server 2008中运行,但是当我在SQL Server 2000中运行它时,出现此错误:

Incorrect syntax near the keyword 'OVER'. 关键字“ OVER”附近的语法错误。

How can I fix it? 我该如何解决? Or replace code ? 还是替换代码?

SQL Server 2000 doesn't support OVER . SQL Server 2000不支持OVER Write a sub query that performs a SUM(..) GROUP BY suProforma and join the sub query to the main table on suProforma 编写一个执行SUM(..) GROUP BY suProforma的子查询,并将该子查询加入SUM(..) GROUP BY 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 我已经为您编写了一个使用表的示例,但是有两个原因:一个,我从iPhone 4发布并重新格式化您的查询(这有点混乱-您会更加努力地保持代码整洁)是非常非常艰苦的工作,而且2,将suProforma列保存在哪个表中并不明显

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. 将此查询应用于您的情况,将table1替换为具有suProforma的表,并使用实际列更正SUM(a * b)。 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 如果您要按其分组和求和的列位于不同的表中,那么您将必须编写一个将两者链接在一起的查询并将其粘贴在(用括号括起来)代替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" 养成在查询的FROM部分中将任何实体视为“只是可以选择结果的矩形数据块”的习惯

This query will work in both SQL Server 2000 and 2008 此查询将在SQL Server 2000和2008中均适用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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