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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 count problem

Author  Topic 

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2003-03-13 : 04:07:31
I have a table called catlog the structure of the table is : catid int,catname
there is a secont table prod whose structure is : prodid int ,catid int ,prodname

now the records in table are like:
catid catlogname
1 A-abx
2 A-rtx
3 A-uyt
4 B-sddr
5 B-opt
6 B-tax

and the records in table products are as:
prodid catid prodid
1 1 prod1
2 1 prod2
3 2 prod3
4 4 prod4
5 3 prod5
6 5 prod6

I want to write a query which can give me the count of the number of products for each main catalog like count for A=4 ,B=2

any ideas?


regards,
Harshal.

Expect the UnExpected

Edited by - harshal_in on 03/13/2003 04:13:18

nr
SQLTeam MVY

12543 Posts

Posted - 2003-03-13 : 04:16:35
select left(c.catlogname,1), count(*)
from products p, catlog c
where p.catid = c.catid
group by left(c.catlogname,1)

if you may have more than one chanracter use charindex for the hyphon and maybe a derived table.
Note this is a very bad design try.

catid catlogname maincat
1 A-abx A

or
catid catlogname maincat
1 abx A



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

harshal_in
Aged Yak Warrior

633 Posts

Posted - 2003-03-13 : 05:57:55
quote:

select left(c.catlogname,1), count(*)
from products p, catlog c
where p.catid = c.catid
group by left(c.catlogname,1)

if you may have more than one chanracter use charindex for the hyphon and maybe a derived table.
Note this is a very bad design try.

catid catlogname maincat
1 A-abx A

or
catid catlogname maincat
1 abx A



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.


thanks nr the query works just as i wanted.I had spent a few hours for the results.Thankyou.
I have got u'r point regarding the design.
thanks for the suggestion.
regards,
Harshal.

Expect the UnExpected
Go to Top of Page
   

- Advertisement -