I have two tables as below:
tbl_test
OFID bigint
Offer_Text text
OFID Offer_Text
------- ----------
1014 Test1
1015 Test2
tbl_trans
OfferNo nvarchar
OfferNo
---------
1015
1015
1015
1014
Now, I want an o/p as below
OFID Offer_Text Counter
------- ------------- -------
1014 Test1 1
1015 Test2 3
That means count of each OFID with the Offer_Text also..
Is it possible..?
What will be the correct query for this?
My Existing query is this:
SELECT
offer_Text, OFID, COUNT(OFID) as Counter
FROM
tbl_Test as a, tbl_trans as b
WHERE
cast(a.OFID as nvarchar) = b.OfferNo
GROUP BY
a.OFID
But giving me the following error:
Column ‘tbl_Offer.Offer_Text’ is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY
clause.
Sure:
Gives you your desired output of:
Update: to have only those rows that exist in the
tbl_transsubtable (a new requirement – not present in the original question!), you need something like this: