MsSql Query


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


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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Jim HornMicrosoft SQL Server Data DudeCommented:
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
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

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

	,count(CatID) AS NoOfProducts
	,Sum(Active) AS Active
	,Sum(Inactive) AS Inactive
			WHEN p.IsActive = 1 THEN 1 ELSE 0
			END AS Active 
			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

The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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

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
    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
    c.CatId = p_totals.CatId
--ORDER BY CatName

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?
praveen1981Author Commented:
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.