0
;WITH n AS  
( 
  SELECT problemID, StationName, problemCode, ProblemCreateDate, probCount, 
    c = COUNT(*) OVER (PARTITION BY StationName, problemCode), 
    rn = ROW_NUMBER() OVER  
    ( 
      PARTITION BY StationName, problemCode ORDER BY ProblemCreateDate DESC, problemID DESC 
    ) 
  FROM dbo.tblProblems
) 
 SELECT problemID, StationName, problemCode, ProblemCreateDate, c 
 FROM n WHERE rn = 1; 

还有一个名为 的表tblCustomers,其中有一列isAssistentType( bit)

我尝试进行内部联接,但这对我来说太复杂了,并且在尝试应用 with 的过滤器时出现inner join错误

tblCustomers where tblCustomers.isAssistent =1

我将非常感激并很高兴知道如何编写正确的语法

重新编辑

inner join tblCustomers on tblProblems.CustID = tblCustomers.custID

错误,我最后一次尝试

消息 4104,级别 16,状态 1,行 14 无法绑定多部分标识符“tblProblems.CustID”。消息 4104,级别 16,状态 1,行 12 无法绑定多部分标识符“tblproblems.problemID”。消息 4104,级别 16,状态 1,第 12 行无法绑定多部分标识符“tblproblems.custID”。消息 4104,级别 16,状态 1,第 12 行无法绑定多部分标识符“tblproblems.StationName”。消息 4104,级别 16,状态 1,第 12 行 无法绑定多部分标识符“tblproblems.problemCode”。消息 4104,级别 16,状态 1,第 12 行无法绑定多部分标识符“tblproblems.ProblemCreateDate”。

这是我的疯狂猜测:

;WITH n AS  
( 
  SELECT tblCustomers.*,tblproblems.problemID, tblproblems.StationName, tblproblems.problemCode, tblproblems.ProblemCreateDate, tblproblems.probCount, 
    c = COUNT(*) OVER (PARTITION BY tblproblems.StationName, tblproblems.problemCode), 
    rn = ROW_NUMBER() OVER  
    ( 
      PARTITION BY tblproblems.StationName, tblproblems.problemCode ORDER BY tblproblems.ProblemCreateDate DESC, tblproblems.problemID DESC 
    ) 
  FROM dbo.tblProblems
  inner join tblCustomers on tblProblems.CustID = tblCustomers.custID
) 
SELECT tblCustomers.*, tblproblems.problemID, tblproblems.custID, tblproblems.StationName, tblproblems.problemCode, tblproblems.ProblemCreateDate, c 
FROM n 
inner join tblCustomers on tblProblems.CustID = tblCustomers.custID
WHERE rn = 1; 

目的是仅在 tblCustomers.isAssistent=1 时选择 tblProblems 的结果

4

1 回答 1

1

您在外面的查询是错误的,应该是

SELECT tblCustomers.*, n.problemID, n.custID, 
           n.StationName, n.problemCode, n.ProblemCreateDate, c 
FROM n 
inner join tblCustomers on n.CustID = tblCustomers.custID
WHERE rn = 1; 

你为什么要再次加入 tblcustomers?

你可以这样做:

;WITH n AS  

( 
  SELECT tblCustomers.*,tblproblems.problemID, tblproblems.StationName, 
         tblproblems.problemCode, tblproblems.ProblemCreateDate,
         tblproblems.probCount, 
    c = COUNT(*) OVER 
        (PARTITION BY tblproblems.StationName, tblproblems.problemCode), 
    rn = ROW_NUMBER() OVER  
    ( 
      PARTITION BY tblproblems.StationName, tblproblems.problemCode
      ORDER BY tblproblems.ProblemCreateDate DESC, tblproblems.problemID DESC 
    ) 
  FROM dbo.tblProblems
  inner join tblCustomers on tblProblems.CustID = tblCustomers.custID
  WHERE tblCustomers.isAssistent =1

) 
SELECT n.* FROM n where rn = 1

如果您只需要问题数据

;WITH n AS  

( 
  SELECT tblproblems.problemID, tblproblems.StationName, 
         tblproblems.problemCode, tblproblems.ProblemCreateDate,
         tblproblems.probCount, 
    c = COUNT(*) OVER 
        (PARTITION BY tblproblems.StationName, tblproblems.problemCode), 
    rn = ROW_NUMBER() OVER  
    ( 
      PARTITION BY tblproblems.StationName, tblproblems.problemCode
      ORDER BY tblproblems.ProblemCreateDate DESC, tblproblems.problemID DESC 
    ) 
  FROM dbo.tblProblems
  inner join tblCustomers on tblProblems.CustID = tblCustomers.custID
  WHERE tblCustomers.isAssistent =1

) 
SELECT n.* FROM n where rn = 1
于 2012-08-26T13:18:05.847 回答