row_number()不起作用

时间:2018-07-12 13:56:23

标签: sql sql-server sql-server-2012

SELECT DISTINCT
    *
FROM 
    (SELECT
         *,
         ROW_NUMBER() OVER (PARTITION BY c.no_ ORDER BY app.beacon, l.beacon) AS seqnum 
     FROM
         loan l
     LEFT JOIN 
         cust c ON c.rowno = l.rowno_custloan_cust
     LEFT JOIN 
         Applicant a ON a.CIFId = c.rowno
     LEFT JOIN 
         Application app ON app.ApplicantId = a.ApplicantId 
    ) loan l
LEFT JOIN 
    cust c ON c.rowno = l.rowno_custloan_cust
LEFT JOIN 
    Applicant a ON a.CIFId = c.rowno
LEFT JOIN 
    Application app ON app.ApplicantId = a.ApplicantId
WHERE 
    l.code IN ('DM', 'MF', 'DV', 'MC', 'MO', 'MC','MF','HR','OF','ML','VR','RV','S1','11','y2','y3','y4','y5','y6','y7','y8','y9')
    AND seqnum = 1
ORDER BY
    l.rowno

运行此命令时出现错误

  

“ l”附近的语法错误

这是我第二个loan l中的FROM

我是row_number()的新手,我一定只是想念一些东西。如果您知道一种获取row_number()并为每个c.no_选择第1行的简便方法,这也将很有帮助

我正在使用SQL Server 2012

2 个答案:

答案 0 :(得分:0)

在查询中,您错误地使用了别名。第一个<content src="index.html" /> <preference name="BackupWebStorage" value="local" /> <feature name="CDVWKWebViewEngine"> <param name="ios-package" value="CDVWKWebViewEngine" /> </feature> <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine" /> <access origin="*" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <preference name="SplashScreen" value="screen" /> <preference name="SplashScreenDelay" value="3000" /> <preference name="KeepRunning" value="false" /> <preference name="Fullscreen" value="true" /> <platform name="android"> <allow-intent href="market:*" /> <icon density="ldpi" src="res/android/ldpi.png" /> <icon density="mdpi" src="res/android/mdpi.png" /> <icon density="hdpi" src="res/android/hdpi.png" /> <icon density="xhdpi" src="res/android/xhdpi.png" /> <icon density="xxhdpi" src="res/android/xxhdpi.png" /> <icon density="xxxhdpi" src="res/android/xxxhdpi.png" /> <splash density="land-hdpi" src="res/screen/android/drawable-land-hdpi.png" /> <splash density="land-ldpi" src="res/screen/android/drawable-land-ldpi.png" /> <splash density="land-mdpi" src="res/screen/android/drawable-land-mdpi.png" /> <splash density="land-xhdpi" src="res/screen/android/drawable-land-xhdpi.png" /> <splash density="land-xxhdpi" src="res/screen/android/drawable-land-xxhdpi.png" /> <splash density="land-xxxhdpi" src="res/screen/android/drawable-land-xxxhdpi.png" /> <splash density="port-hdpi" src="res/screen/android/drawable-port-hdpi.png" /> <splash density="port-ldpi" src="res/screen/android/drawable-port-ldpi.png" /> <splash density="port-mdpi" src="res/screen/android/drawable-port-mdpi.png" /> <splash density="port-xhdpi" src="res/screen/android/drawable-port-xhdpi.png" /> <splash density="port-xxhdpi" src="res/screen/android/drawable-port-xxhdpi.png" /> <splash density="port-xxxhdpi" src="res/screen/android/drawable-port-xxxhdpi.png" /> </platform> <platform name="ios"> <allow-intent href="itms:*" /> <allow-intent href="itms-apps:*" /> <feature name="StatusBar"> <param name="ios-package" onload="true" value="CDVStatusBar" /> </feature> <icon height="180" src="res/ios/icon-60@3x.png" width="180" /> <icon height="60" src="res/ios/icon-60.png" width="60" /> <icon height="120" src="res/ios/icon-60@2x.png" width="120" /> <icon height="76" src="res/ios/icon-76.png" width="76" /> <icon height="152" src="res/ios/icon-76@2x.png" width="152" /> <icon height="40" src="res/ios/icon-40.png" width="40" /> <icon height="80" src="res/ios/icon-40@2x.png" width="80" /> <icon height="57" src="res/ios/icon.png" width="57" /> <icon height="114" src="res/ios/icon@2x.png" width="114" /> <icon height="72" src="res/ios/icon-72.png" width="72" /> <icon height="144" src="res/ios/icon-72@2x.png" width="144" /> <icon height="167" src="res/ios/icon-167.png" width="167" /> <icon height="29" src="res/ios/icon-small.png" width="29" /> <icon height="58" src="res/ios/icon-small@2x.png" width="58" /> <icon height="50" src="res/ios/icon-50.png" width="50" /> <icon height="100" src="res/ios/icon-50@2x.png" width="100" /> <icon height="167" src="res/ios/icon-83.5@2x.png" width="167" /> <splash src="res/screen/ios/Default@2x~universal~anyany.png" /> </platform> <plugin name="cordova-plugin-file" spec="^5.0.0" /> <plugin name="cordova-plugin-ionic-webview" spec="^1.1.14" /> <plugin name="cordova-plugin-splashscreen" spec="^4.1.0" /> <plugin name="cordova-plugin-statusbar" spec="^2.2.2" /> <plugin name="cordova-plugin-whitelist" spec="^1.3.1" /> <plugin name="cordova-plugin-screen-orientation" spec="^3.0.1" /> <engine name="ios" spec="^4.4.0" /> 语句的别名用作SELECT,这里) loan l是别名,因此loan不能被识别并且会引发错误。

我已将l重命名为loan l并更改了其相关位置:

AliasLoan

答案 1 :(得分:0)

你只想要这个吗?

SELECT x.*
FROM (SELECT l.*, c.*, a.*, app.*
             ROW_NUMBER() OVER (PARTITION BY c.no_ ORDER BY app.beacon, l.beacon) AS seqnum 
      FROM loan l LEFT JOIN 
           cust c
           ON c.rowno = l.rowno_custloan_cust LEFT JOIN 
           Applicant a
           ON a.CIFId = c.rowno LEFT JOIN
           Application app
           ON app.ApplicantId = a.ApplicantId 
      WHERE l.code IN ('DM', 'MF', 'DV', 'MC', 'MO', 'MC','MF','HR','OF','ML','VR','RV','S1','11','y2','y3','y4','y5','y6','y7','y8','y9')
     ) x
WHERE seqnum = 1
ORDER BY seqnum;