MsSql Query

HI,

I have the following tables,  category table ,Product Table and ActiveTable with following columns

category table

catId  Name
1              xx
2              yy
3              zz

Product Table

CatId  ProdId Name IsActive

1             1              x          1
1             2             y           0
1             3              z          1
2             4              b          1
2             5              a           0

ActiveTable

ActiveId      Name
1                  Active
2                  InActive


Now,i want the result as follows

catId  No.ofProducts  Active  InActive
1                3                     2                0
2                  2                   1                 1

could you please guide me how to get query.
                                                            Many thanks.
praveen1981Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramachandar NCommented:
Here you go.
create table category(catid int, Name char(10))
create table Product(catid int, Prodid int, Name char(10), Isactive int)
create table active(activeid int, Name char(10))

insert into category select 1, 'xx'
insert into category select 2, 'yy'
insert into category select 3, 'zz'

insert into Product select 1,1,'x',1
insert into Product select 1,2,'y',0
insert into Product select 1,3,'z',1
insert into Product select 2,4,'b',1
insert into Product select 2,5,'a',0

select c.catid, COUNT(c.catid) as numOfProducts, SUM(CASE WHEN Isactive = 1 then 1 else 0 end) as Active, SUM(CASE WHEN Isactive = 0 then 1 else 0 end) as InActive
from category c, Product p where c.catid=p.catid group by c.catid

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
You'll likely get a number of different answers on this one, but assuming IsActive is a bit and not char, give this a whirl..
SELECT CatId, NoOfProducts, Active, NoOfProducts - Active as Inactive
FROM (
SELECT cat.catId, 
  COUNT(pro.ProdId) as NoOfProducts, 
  SUM(pro.IsActive) as Active
FROM category cat
   JOIN Product pro ON cat.CatID = pro.ProdId
GROUP BY cat.CatID) a

Open in new window

0
Jeff DarlingDeveloper AnalystCommented:
Its interesting to see different solutions to the same problem.

SELECT CatID
	,count(CatID) AS NoOfProducts
	,Sum(Active) AS Active
	,Sum(Inactive) AS Inactive
FROM (
	SELECT C.catID
		,CASE 
			WHEN p.IsActive = 1 THEN 1 ELSE 0
			END AS Active 
		,CASE 
			WHEN p.IsActive = 0 THEN 1 ELSE 0
			END AS InActive
	FROM #category C
	JOIN #product P ON C.catID = P.catID
	) g
GROUP BY catid

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
To produce your desired result with the CatId, and not the CatName,  you need only the product table.  Personally I prefer not to assume no NULLs, so I used a condition that would include NULL IsActive values in the InActive category rather than ignoring them:

SELECT p.CatId, COUNT(DISTINCT p.ProdId) AS [No.OfProducts],
    SUM(CASE WHEN p.IsActive = '1' THEN 1 ELSE 0 END) AS Active,
    SUM(CASE WHEN p.IsActive = '1' THEN 0 ELSE 1 END) AS InActive
FROM Product p
GROUP BY p.CatId

If you want to list the cat name rather than the id, it's most efficient to do that as an outer query after the GROUPing has been done:

SELECT c.Name AS [CatName], p_totals.[No.OfProducts], p_totals.Active, p_totals.Inactive
FROM (
    SELECT p.CatId, COUNT(DISTINCT p.ProdId) AS [No.OfProducts],
        SUM(CASE WHEN p.IsActive = '1' THEN 1 ELSE 0 END) AS Active,
        SUM(CASE WHEN p.IsActive = '1' THEN 0 ELSE 1 END) AS InActive
    FROM Product p
    GROUP BY p.CatId
) AS p_totals
LEFT OUTER JOIN category c ON
    c.CatId = p_totals.CatId
--ORDER BY CatName
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
praveen1981, do you still need help with this question?
0
praveen1981Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.