SQL中的MS Access First函数

时间:2017-01-11 18:24:59

标签: sql sql-server

我有简单的MS Access查询,我只是想弄清楚如何在SQL服务器中复制。

 SELECT tbl_XREF_MFG.[ITEM NUMBER], 
 Count(tbl_XREF_MFG.CROSS_REFERENCE) AS CountOfCROSS_REFERENCE, 
 First(tbl_XREF_MFG.CROSS_REFERENCE) AS FirstOfCROSS_REFERENCE
 FROM tbl_XREF_MFG
 GROUP BY tbl_XREF_MFG.[ITEM NUMBER];

我知道SQL Server没有First作为功能,但它有FIRST_VALUE,但我无法使其工作。这是我试过的:

SELECT  ([ITEM NUMBER]),
Count(CROSS_REFERENCE) as CountOFCROSS_REFERENCE,
FIRST_VALUE(CROSS_REFERENCE) OVER (ORDER BY CROSS_REFERENCE) as FirstOfCROSS_REFERENCE
INTO #tmp1
FROM tbl_XREF_MFG
GROUP BY ([ITEM NUMBER]),CROSS_REFERENCE

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以将其设为子查询。

SELECT tbl_XREF_MFG.*,
       FIRST_VALUE(CROSS_REFERENCE) OVER (PARTITION BY ITEM_NUMBER ORDER BY CROSS_REFERENCE) AS FIRST
FROM tbl_XREF_MFG