2012-03-02 110 views
0

tbl_Offer具有由組子查詢和計數

OFID bigint 
Offer_Text text 

OFID Offer_Text 
------- ----------  
1014 Test1 
1015 Test2 

tbl_TransactionDishout

offerNo TerminalID  Created 
--------------------------------- 
1014  170924690436418 2010-05-25 12:51:59.547 

tblVTSettings

gid  mid  tid 
----------------------- 
50  153  119600317313328 
104  158  160064024922223 
76  162  256674529511898 
1111  148  123909123909123 

這些都是THRE表格。

現在我想了解由學校分開的所有優惠(優惠)的信息(看看gid TerminalID在(50,76,104))。

這是三個學校:(50,76,104)

的O/P應該具備以下字段:
OFFERID(歐佩克國際發展基金),學校要約的,OFFER_TEXT,時間數的報價。

查詢可能會以某種方式是這樣的:

SELECT OFID, Offer_Text, 
    Counter = 
    (
    SELECT COUNT(*) FROM dbo.tbl_TransactionDishout t 
    WHERE t.OfferNo = CAST(OFID AS NVARCHAR(30)) 
    and t.TerminalID in 
    (select TID from tblVTSettings where gid in (50,76,104)) 
    ) 
    FROM dbo.tbl_Offer 
    Where EXISTS (SELECT * FROM dbo.tbl_TransactionDishout 
      WHERE OfferNo = CAST(OFID AS NVARCHAR(30))) 
+0

請告訴我你的問題? – Arion 2012-03-02 08:58:09

+0

我得到OFID,Offer_Text和計數器與上述查詢,但沒有得到它從屬於哪個學校.. – 2012-03-02 08:59:31

+0

@marc_s你能給我正確的輸出..? – 2012-03-02 09:03:21

回答

0

請試試這個。

SELECT to.OFID 
     ,ts.gid AS 'School the offer is for' 
     ,to.Offer_Text 
     ,COUNT(to.OFID) AS 'Number of time the offer is' 
    FROM tbl_Offer to 
    JOIN tbl_TransactionDishout tt 
    ON to.OFID = tt.offerNo 
    JOIN tblVTSettings ts 
    ON ts.tid = tt.TerminalID 
+0

不工作...錯誤執行.. – 2012-03-02 10:18:29

+0

PLZ發表您的錯誤。我們無法測試查詢,因爲我們沒有模式腳本。 – Rajan 2012-03-02 10:20:51

0

嘗試:

SELECT o.OFID, 
     s.gid, 
     o.Offer_Text, 
     count(*) over (partition by o.OFID) number_schools, 
     count(*) over (partition by s.gid) number_offers 
    FROM tbl_Offer o 
    JOIN tbl_TransactionDishout d ON o.OFID = d.offerNo 
    JOIN tblVTSettings s ON s.tid = d.TerminalID 
+0

Msg 156,Level 15,State 1,Line 4 關鍵字'over'附近的語法不正確。 – 2012-03-02 11:17:32

+0

@VishalSuthar:由於'o.Offer_Text'後面缺少一個逗號,我現在已經糾正 - 再試一次。 – 2012-03-02 11:28:10

+0

同樣的錯誤...我已經糾正了逗號.. – 2012-03-02 11:54:44