在双内连接中选择double max

时间:2017-07-11 06:08:48

标签: sql

我有一个SQL Server 2012数据库,我需要查询数据库并获得与MAX(RecordID),MAX(SettingID)和MAX(RuleID)相关的结果

但所有结果都将到来。

USE CM_T01
SELECT D_PreReifiedValue,D_Setting_CI_ID,D_InstanceData,
MAX_D_RecordID,MAX_D_SettingID,MAX_D_RuleID,
B.rulename as B_rulename,B.criteria as B_criteria,A.CIVersion as A_CIVersion,A.CI_ID as A_CI_ID,b.CI_ID as B_CI_ID,
B.Rule_ID as B_Rule_ID,
A.ComplianceState as A_ComplianceState, A.LastComplianceMessageTime as A_LastComplianceMessageTime, B.rule_uniqueID as B_rule_uniqueID,
B.rulenameRID AS B_rulenameRID, B.ruledescriptionRID AS B_ruledescriptionRID,B.rowversion as B_Rowversion
FROM v_CI_CurrentComplianceStatus as A
INNER JOIN v_CIRules as B
ON B.CI_ID = A.CI_ID
INNER JOIN v_R_System  as C
ON C.ResourceID = A.ItemKey
INNER JOIN 
(SELECT PreReifiedValue as D_PreReifiedValue,Setting_CI_ID as D_Setting_CI_ID,instancedata as D_InstanceData,
MAX(RecordID) OVER(PARTITION BY PreReifiedValue) AS MAX_D_RecordID,
MAX(SettingID) OVER(PARTITION BY PreReifiedValue) AS MAX_D_SettingID,
MAX(RuleID) OVER(PARTITION BY PreReifiedValue) AS MAX_D_RuleID
FROM CI_CurrentRuleDetail
GROUP BY PreReifiedValue,Setting_CI_ID,instancedata,RecordID,SettingID,RuleID) AS D ON D.D_Setting_CI_ID = A.CI_ID

结果

D_PreReifiedValue   D_Setting_CI_ID D_InstanceData  MAX_D_RecordID  MAX_D_SettingID MAX_D_RuleID    B_rulename  B_criteria  A_CIVersion A_CI_ID B_CI_ID B_Rule_ID   A_ComplianceState   A_LastComplianceMessageTime B_rule_uniqueID B_rulenameRID   B_ruledescriptionRID    B_Rowversion
10  16803678    Location = [Is64Bit=true]:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon, Property = cachedlogonscount    72057594038117564   16780566    16780622    OS Hardening: Authentication Types - Network security: Minimum session security for NTLM SSP based (including secure RPC) clients   Equals 537395200    1015    16803678    16803678    16780621    2   2017-07-05 13:55:35.003 Rule_3bb06dda-bd4b-4c28-999c-3bae0818271d   ID-40797cd7-5677-459b-9191-cd751a6aaf77 ID-e2c1bdca-ea31-4ae7-a07d-d4ff351d6a96 0x000000000676D55D
0   16803678    Location = [Is64Bit=true]:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon, Property = scremoveoption   72057594038117565   16780570    16780620    OS Hardening: Authentication Types - Network security: Minimum session security for NTLM SSP based (including secure RPC) clients   Equals 537395200    1015    16803678    16803678    16780621    2   2017-07-05 13:55:35.003 Rule_3bb06dda-bd4b-4c28-999c-3bae0818271d   ID-40797cd7-5677-459b-9191-cd751a6aaf77 ID-e2c1bdca-ea31-4ae7-a07d-d4ff351d6a96 0x000000000676D55D
4   16803678    Location = [Is64Bit=true]:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa, Property = LmCompatibilityLevel  72057594038117567   16780563    16780614    OS Hardening: Authentication Types - Network security: Minimum session security for NTLM SSP based (including secure RPC) clients   Equals 537395200    1015    16803678    16803678    16780621    2   2017-07-05 13:55:35.003 Rule_3bb06dda-bd4b-4c28-999c-3bae0818271d   ID-40797cd7-5677-459b-9191-cd751a6aaf77 ID-e2c1bdca-ea31-4ae7-a07d-d4ff351d6a96 0x000000000676D55D
536870912   16803678    Location = [Is64Bit=true]:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\MSV1_0, Property = NTLMMinClientSec   72057594038117568   16780568    16780621    OS Hardening: Authentication Types - Network security: Minimum session security for NTLM SSP based (including secure RPC) clients   Equals 537395200    1015    16803678    16803678    16780621    2   2017-07-05 13:55:35.003 Rule_3bb06dda-bd4b-4c28-999c-3bae0818271d   ID-40797cd7-5677-459b-9191-cd751a6aaf77 ID-e2c1bdca-ea31-4ae7-a07d-d4ff351d6a96 0x000000000676D55D
536870912   16803678    Location = [Is64Bit=true]:HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\MSV1_0, Property = NTLMMinServerSec   72057594038117568   16780568    16780621    OS Hardening: Authentication Types - Network security: Minimum session security for NTLM SSP based (including secure RPC) clients   Equals 537395200    1015    16803678    16803678    16780621    2   2017-07-05 13:55:35.003 Rule_3bb06dda-bd4b-4c28-999c-3bae0818271d   ID-40797cd7-5677-459b-9191-cd751a6aaf77 ID-e2c1bdca-ea31-4ae7-a07d-d4ff351d6a96 0x000000000676D55D

1 个答案:

答案 0 :(得分:0)

如果您没有在源代码中分享示例,那么您不会理解这个概念,因为在您获得查询的另一个问题之前我会询问什么可能会很慢。

SELECT 
    Max_id, CIVersion, Netbios_Name0, 
    RuleName, RuleDescription, prereifiedvalue, complianceState 
FROM 
    v_CI_CurrentComplianceStatus 
INNER JOIN 
    (SELECT 
         CI_ID, Rule_ID, Rulename, RuleDescription, criteria, 
         MAX(Rule_ID) OVER(PARTITION BY Rulename) AS Max_id ,
         MAX(CIVersion) OVER(PARTITION BY Rulename) AS CIVersion,
     FROM 
         v_cirules 
     WHERE 
         rulename LIKE '%Account lockout duration%' 
         AND RuleDescription LIKE '%applicable%' 
     GROUP BY
         Rule_ID, RuleName, RuleDescription, CI_ID, Criteria) AS v_CIRules ON v_CIRules.CI_ID = v_CI_CurrentComplianceStatus.CI_ID 
INNER JOIN 
    v_R_System ON v_R_System.ResourceID = v_CI_CurrentComplianceStatus.ItemKey 
LEFT JOIN 
    v_CICurrentRuleDetail ON v_CICurrentRuleDetail.CurrentComplianceStatusID = v_CI_CurrentComplianceStatus.CI_CurrentComplianceStatusID 
WHERE
    netbios_name0 = 'TRISTSCCM-PS' 
GROUP BY 
    Netbios_Name0, RuleName, RuleDescription, prereifiedvalue, complianceState, v_cirules.CI_ID, v_CIRules.Max_id, Criteria