0

如何为 SQL Server 2008 R2 的选择列表中的几乎所有列重新构建以下 MS Access 查询?MS Access 查询返回 254 条记录,而我在没有 First() 函数的情况下构建的 SQL 查询返回 545 条记录,这显然没有返回所需的结果。任何帮助表示赞赏。提前致谢

SELECT 
First(Acct_ID) AS FirstOfAcct_ID, 
First(IMDX__ID) AS FirstOfIMDX_ID, 
First(Referral_ID) AS FirstOfReferral_ID, 
First(FIRSTNAME) AS FirstOfFIRSTNAME
First(LASTNAME) AS FirstOfLASTNAME, 
First(ADDRESSLINE1) AS FirstOfADDRESSLINE1, 
First(ADDRESSLINE2) AS FirstOfADDRESSLINE2, 
First(CITY) AS FirstOfCITY, 
First(STATE) AS FirstOfSTATE, 
First(ZIPCODE) AS FirstOfZIPCODE, 
First(Acct_Office_ID) AS FirstOfAcct_Office_ID, 
First(IMDX_Office_ID) AS FirstOfIMDX_Office_ID, 
FROM <<TableName>>
GROUP BY ZIPCODE,LASTNAME,FIRSTNAME,Acct_Office_ID,IMDX_Office_ID
HAVING (((First(posdesc)) Is Not Null) AND ((ACTIVE)<>0) AND ((CP_Active)<>0));
4

1 回答 1

0

为什么不只是:

;WITH x AS (SELECT Acct_ID, IMDX__ID, ... other columns ..., rn = ROW_NUMBER() OVER 
  (PARTITION BY ZIPCODE, LASTNAME, FIRSTNAME, Acct_Office_ID, IMDX_Office_ID
   ORDER BY ... whatever column(s) you think FIRST() means ...)
  FROM dbo.TableName
  WHERE posdesc IS NOT NULL AND ACTIVE <> 0 AND CP_Active <> 0
)
SELECT ... all columns except rn ... FROM x WHERE rn = 1;
于 2013-10-09T20:45:51.833 回答