sql inner使用Count()和窗口函数进行连接

时间:2012-08-26 12:49:22

标签: sql-server-2008 count inner-join window-functions

;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的表,其中列isAssistent类型(bit

我尝试进行内连接,但对我来说太复杂了,在尝试应用inner join的过滤器时遇到错误

tblCustomers where tblCustomers.isAssistent =1

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

重新编辑

inner join tblCustomers on tblProblems.CustID = tblCustomers.custID

错误,我最后一次尝试

  

消息4104,级别16,状态1,行14多部分标识符   " tblProblems.CustID"无法受约束。 Msg 4104,Level 16,State 1,   第12行多部分标识符" tblproblems.problemID"不可能   界。消息4104,级别16,状态1,行12多部分标识符   " tblproblems.custID"无法受约束。 Msg 4104,Level 16,State 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的结果

1 个答案:

答案 0 :(得分: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