加入查询以获取计数

时间:2012-03-05 07:12:38

标签: sql-server sql-server-2008

tbl_TransactionDishout

Trnx_id   offerNo   TerminalID      Created                  VirtualCard
-------------------------------------------------------------------
1         1014      170924690436418 2010-05-25 12:51:59.547  1234567890
2         1014      123924690436418 2010-05-23 12:51:59.547  1234567890
3         1015      144524690436418 2010-05-23 12:51:59.547  1234567890

tbl_Offer

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

现在我希望输出像这样:

OFID    Offer_Text     Counter
------------------------------
1014    Test1          2
1015    Test2          1

是否可以使用单个查询..?

5 个答案:

答案 0 :(得分:1)

试试这个:

select t2.OFID, cast(t2.Offer_Text as varchar(max)), Count(*)
from tbl_TransactionDishout t
inner join tbl_Offer t2 on cast(t.offerNo as varchar(max)) = cast(t2. OFID as varchar(max))
group by t2.OFID, cast(t2.Offer_Text as varchar(max))

答案 1 :(得分:1)

你可以这样写

SELECT     O.OFID, O.Offer_Text, COUNT(t.offerNo) AS Counter FROM  tbl_Offer AS O 
INNER JOIN  tbl_TransactionDishout AS t ON O.OFID = t.offerNo GROUP BY O.OFID, O.Offer_Text

编辑:

以下是我在系统中尝试时输出的屏幕截图。

enter image description here

答案 2 :(得分:0)

试试这个,它会起作用

 select t1.OFID,t1.Offer_text, count(t2.offerNo)
 from tbl_Offer t1 inner join tbl_TransactionDishout t2 
 on t1.OFID= t2.offerNo
 group by t1.OFID,t1.Offer_text 

答案 3 :(得分:0)

select a.offerNo,Offer_Text,COUNT(a.TerminalID) as Counter
from 
tbl_TransactionDishout a,
tbl_Offer b
where a.OFID = b.OFID
group by OFID,Offer_Text;

答案 4 :(得分:0)

尝试此查询

select OFID, Offer_Text, Trnx_id as Counter from tbl_offer left join tbl_TransactionDishout  on transactionDishout.offerNo = tbl_Offer.OfferText ordered by Trnx_id desc