2012-08-26 71 views
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另一個表,與列isAssistent類型(bitSQL內加入以計數()和窗口函數

我試圖做內部聯接,但是這對我來說太複雜了,我得到一個嘗試與

tblCustomers where tblCustomers.isAssistent =1 

我會非常感激和高興適用的inner join過濾器知道如何編寫正確的語法時出現錯誤

重新編輯

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」無法綁定。 Msg 4104, Level 16,State 1,Line 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; 

的目的是爲了有tblProblems的選擇結果,只有當tblCustomers.isAssistent = 1

+0

你可以張貼錯誤? –

+0

用pk info redtiting這個帖子 – LoneXcoder

+0

外面的'SELECT'引用別名'tblProblems',但應該引用'n'。它也會從'tblCustomers'返回數據,但不是你期望的方式,因爲它是在'SELECT'外部加入'JOIN'。 – HABO

回答

1

你用錯了,它應該外面查詢be

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 
+0

現在它是好的沒有錯誤,但查理是比原來沒有內部聯接的其他方式,我需要的問題列,但現在它繪製tblCustomersrs字段insted tblProblems – LoneXcoder

+0

實際上,我現在看到,它也存在tblProblems。* (對不起tblCustomers有超過30列),但我不會在Gridview選擇命令中調用它們 – LoneXcoder

+0

檢查更新的答案 –