Avatar of dlavar
dlavar
 asked on

Stored Procedure not giving correct data.

The query below is resulting in the following data which is not what i want..

vchRecordID       vchManufacturer              vchCertificationType    Number of Brands  Total Amount Owed
T-110057      COMMONWEALTH BRANDS, INC      Initial      6      6000


what it really should be populating is 3 for number of distinct brands and 3000 for total amount owed.

What wrong with my query?  I know its populating the following because in the tblCLBrandFamily the recordid is in there 3 different times which is possible. I've tried adding distinct to the query but it doesn't work. how can i get it to work?
SELECT  t.vchRecordID, t.vchManufacturer, a.vchCertificationType, 
    COUNT(b.vchBrandFamily)as 'Number of Brands', 'Total Amount Owed'=
        (Case
    when vchCertificationType = 'Initial' then '1000'
    when vchCertificationType = '3 Year Renewal' then '1000'
    when vchCertificationType ='Corrective Filing' then '0'
    end) * COUNT(b.vchBrandFamily)
    from tblCLTestLab t
    inner join tblCLApplicants a on t.vchManufacturer = a.vchManufacturer
    Inner join  tblCLBrandFamily b on t.vchRecordID = b.vchRecordID
    where t.vchRecordID = 'T-110057'
    group by t.vchRecordID, t.vchManufacturer,  a.vchCertificationType

Open in new window

Microsoft SQL Server 2008

Avatar of undefined
Last Comment
dlavar

8/22/2022 - Mon
BourbonKid

Just try the query below. It is likely that you got six rows (each duplicated two times) :

SELECT  t.vchRecordID, t.vchManufacturer, a.vchCertificationType, 
    b.vchBrandFamily as 'Number of Brands'
from tblCLTestLab t
inner join tblCLApplicants a on t.vchManufacturer = a.vchManufacturer
Inner join  tblCLBrandFamily b on t.vchRecordID = b.vchRecordID
where t.vchRecordID = 'T-110057'
order by t.vchRecordID, t.vchManufacturer, a.vchCertificationType, b.vchBrandFamily

Open in new window

SOLUTION
ralmada

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Lowfatspread

dlavar

ASKER
Sorry it took so long for me to respond.  For some reason it would let me pull up the answers you all were posting.  
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes