Query help - Group by problem

I have a table of data (table name = test):
item No    Sls           Cat
A                1               HI
B                2               HI
B                3               NI
C                4               HI

The reuslts I want are:

A               1
B               5

The results should contain all the items in which an HI appears in the group. The results are grouped by ItemNo.
A qualtifies becuase it only contains an HI
B qualifies because it contains at least one HI
C does not qualify because its group does not contain any HIs.

I have attached code to create the table and populate it.

Thanks in advance for your help.

pat
Create-and-Populate-table.txt
mpdillonAsked:
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.

Lee SavidgeCommented:
I may be missing something here but:

C                4               HI

C does not qualify because its group does not contain any HIs.

Unless I have misunderstood, C contains an HI?
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
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Use a subquery to pick off all the HI's, then do your sum.
SELECT t.itemno, Sum(t.Sales) as sum_of_sales
FROM test t
   JOIN (SELECT DISTINCT itemno FROM test WHERE cat='HI') hi oN t.itemno = hi.itemno
GROUP BY t.itemno
ORDER BY t.itemno

Open in new window

0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Using the T-SQL in the attachment..
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Test](
	[ItemNo] [char](1) NULL,
	[Sls] [int] NULL,
	[Cat] [char](2) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Insert into Test(ItemNo, Sls, Cat)
select 'A',1,'HI'
union all
select 'B',2,'HI'
union all
select 'B',3,'NI'
union all
select 'C',4,'NI'

SELECT t.ItemNo, Sum(t.Sls) as sum_of_sales
FROM test t
   JOIN (SELECT DISTINCT ItemNo FROM test WHERE cat='HI') hi oN t.ItemNo = hi.ItemNo
GROUP BY t.ItemNo
ORDER BY t.ItemNo 

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Lee SavidgeCommented:
Ahh, fair do. missed that. In that case yours works :)
0
PortletPaulfreelancerCommented:
This meets the requirement with a single pass of the table:
**Query 1**:

    SELECT
            ItemNo
          , sum(Sls) AS Sls
    FROM Test
    GROUP BY ItemNo
    HAVING SUM(case when Cat = 'HI' then 1 else 0 end) > 0
    

**[Results][2]**:
    
    | ITEMNO | SLS |
    |--------|-----|
    |      A |   1 |
    |      B |   5 |


  [1]: http://sqlfiddle.com/#!3/e42c5/10

  [2]: http://sqlfiddle.com/#!3/e42c5/10/0

Open in new window

0
mpdillonAuthor Commented:
yes. You are correct. i posted incorrectly. C should be NI (New Item). I will review the solutions and Accept in a few minutes. Sorry.
0
mpdillonAuthor Commented:
Thanks everyone. I really appreciate it.

pat
0
Lee SavidgeCommented:
Thanks but I'm not sure what help I was!
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
All

I gloriously ignored the discrepancy with C-HI, and I'm certain that if lsavidge did too he would have coughed up the same answer in about the same time.  I've done this countless times myself.

Thanks for the split.  Good luck with your project.  -Jim
0
PortletPaulfreelancerCommented:
Did mine not get tried? :( Oh well.
0
PortletPaulfreelancerCommented:
accepted solution explain planalternative solution explain planmerely recording this (instead of discarding it)
no action wanted
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 2008

From novice to tech pro — start learning today.