Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
KWMKWM
Starting Member
3 Posts |
Posted - 2010-11-05 : 15:34:31
|
I want to double count in a SQLSERVER 2008 query, hereis my sample example. TableA field itemid field itemgroupidTableB field Bomid I want the query to count using a whereclause on Table A select count(itemid) from TableA where itemgroupid in ('190','250','252','253','255')),and then give me a count of how manyBomid's in TableB are attached to allthe results from the select/where onTableA.The relationship of Table A to Table Bis one-to-many, Table A has unique entryswhile table B can have many connections toTableA, the key is itemid.So the result I am looking for wouldlook like this:TableAresult TableBResult60,000 180,000Thanks |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
TimSman
Posting Yak Master
127 Posts |
Posted - 2010-11-05 : 16:27:21
|
| DECLARE @t TABLE ( itmID int NOT NULL );INSERT INTO @t( itmID)SELECT itemidFROM TableAWHERE (itemgroupid IN ('190','250','252','253','255'));SELECT ( SELECT COUNT(itmID) FROM @t ) AS ACount,( SELECT COUNT(b.Bomid) FROM TableB AS b INNER JOIN @t AS t ON b.itemID = t.itmID) AS BCount;I think that works.... |
 |
|
|
KWMKWM
Starting Member
3 Posts |
Posted - 2010-11-05 : 16:32:26
|
| Hi, it worked as far as joining the 2 counts but there was no logic to it, I only want the query in TableB to count the resultset of TableA. So if there was 100 records in TableA and only 50 of them met the " where itemgroupid in ('190','250','252','253','255'))," criteria, AND 1000 records in Tbale B that only 500 matched the itemid in Table A, then I would like to seea count of 50 followed by a count of 500, not a toal count of 100 and 1000. Kevin |
 |
|
|
KWMKWM
Starting Member
3 Posts |
Posted - 2010-11-05 : 16:46:51
|
| Hi TimSMan,I changed the int to a char and it all worked, thanks! |
 |
|
|
TimSman
Posting Yak Master
127 Posts |
Posted - 2010-11-05 : 16:53:45
|
| Sweet. Welcome. |
 |
|
|
|
|
|
|
|