SQL Server Sum two count in case - sql-server

I'm trying to sum the two count case and ran into some errors says:
Column 'tmp.Col1' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
I've tried to add group by Col1, but it can't recognized Col1 as column.
SELECT
Col1,
Col2,
SUM(Col3 + Col4) AS 'In_Progress'
FROM
(
SELECT
COUNT(case when IO_Account_Handler_Contact IS NULL THEN 1 ELSE 0 END) AS 'Col1',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status IN ('OPEN-RELEASED','OPEN-CORRECTED') THEN 1 ELSE 0 END) AS 'Col2',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'For Issuance' THEN 1 ELSE 0 END) AS 'Col3',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'OPEN-ACCEPTED' AND Completed_Quality_Review IS NOT NULL THEN 1 ELSE 0 END) AS 'Col4'
FROM Sales_Market
) tmp

You will need to add:
Group by Col1,Col2
to make it work.
Not sure if that is what you want tough.
when using any aggregation(sum,max,min etc) you will always need to group anything not aggregated. So in your case the two columns not just the 1.
Or you could also sum(col1) and sum(col2).
So Either:
SELECT
sum(Col1),
sum(Col2),
SUM(Col3 + Col4) AS 'In_Progress'
FROM
(
SELECT
COUNT(case when IO_Account_Handler_Contact IS NULL THEN 1 ELSE 0 END) AS 'Col1',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status IN ('OPEN-RELEASED','OPEN-CORRECTED') THEN 1 ELSE 0 END) AS 'Col2',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'For Issuance' THEN 1 ELSE 0 END) AS 'Col3',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'OPEN-ACCEPTED' AND Completed_Quality_Review IS NOT NULL THEN 1 ELSE 0 END) AS 'Col4'
FROM Sales_Market
) tmp
or
SELECT
Col1,
Col2,
SUM(Col3 + Col4) AS 'In_Progress'
FROM
(
SELECT
COUNT(case when IO_Account_Handler_Contact IS NULL THEN 1 ELSE 0 END) AS 'Col1',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status IN ('OPEN-RELEASED','OPEN-CORRECTED') THEN 1 ELSE 0 END) AS 'Col2',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'For Issuance' THEN 1 ELSE 0 END) AS 'Col3',
COUNT(case when Issuing_Country_Name ='U.S.A' AND Internal_status = 'OPEN-ACCEPTED' AND Completed_Quality_Review IS NOT NULL THEN 1 ELSE 0 END) AS 'Col4'
FROM Sales_Market
) tmp
group by Col1,Col2

Related

Better performance for running this query

I want to improve the performance for running the following SQL.
Apparently its taking up 3.28 minutes to run 784 for the following query,
is there a better practice, or better solution to improve the performance of the code? Thanks so much
I've tried with Sum, Count, pivot and looks like Count is faster
with Accounts as
(select ID,displayName AccountName from u3_system.dbo.account with(nolock)
where
ID = '0439b9d9-f2c9-43dc-aa2d-08cdea94db21'
or ID = 'f815d2dd-e118-4930-a67d-08cf28b410e2'
or ID = 'f606fcab-dcbd-4d02-8161-08d635cd8971'
or ID = '7d8f44d4-0f17-41f1-87e2-08cf28b4343b'),
Campaign as
(select C.ID, Accounts.AccountName,C.displayName [Mailout Folder] from Accounts
left join u3_mail.dbo.Campaign C with(nolock)
on C.listID = Accounts.ID),
mo as
(select
campaign.AccountName
,campaign.[Mailout Folder]
,mo.ID [MailOutID]
,mo.displayName [Mailout Name]
,[date] [Send Date]
,Details.value('(//content)[1]/subject[1]', 'varchar(200)') AS [Subject Line]
from campaign
inner join [u3_mail].[dbo].[mailout] mo WITH(NOLOCK)
on mo.campaignID = campaign.ID),
ML as
(select mailoutID,
count(mailoutId) [Total Messages],
count(case when status = 'Delivered' then 1 else null end) Delivered,
count(case when status <> 'Delivered' then 1 else null end) Undelivered,
count(case when ReadDate is not null then 1 else null end) [Read],
count(case when HasClicked = '1' then 1 else null end) [Unique Clicks],
count(case when BounceDate is not null then 1 else null end) [Bounced],
count(case when OptOutDate is not null then 1 else null end) [Opted Out]
from
[u3_data].[data].[MailLog_0439b9d9f2c943dcaa2d08cdea94db21] ML with(nolock)
group by MailoutID
union
select mailoutID,
count(mailoutId) [Total Messages],
count(case when status = 'Delivered' then 1 else null end) Delivered,
count(case when status <> 'Delivered' then 1 else null end) Undelivered,
count(case when ReadDate is not null then 1 else null end) [Read],
count(case when HasClicked = '1' then 1 else null end) [Unique Clicks],
count(case when BounceDate is not null then 1 else null end) [Bounced],
count(case when OptOutDate is not null then 1 else null end) [Opted Out]
from
[u3_data].[data].[MailLog_f815d2dde1184930a67d08cf28b410e2] ML with(nolock)
group by MailoutID
union
select mailoutID,
count(mailoutId) [Total Messages],
count(case when status = 'Delivered' then 1 else null end) Delivered,
count(case when status <> 'Delivered' then 1 else null end) Undelivered,
count(case when ReadDate is not null then 1 else null end) [Read],
count(case when HasClicked = '1' then 1 else null end) [Unique Clicks],
count(case when BounceDate is not null then 1 else null end) [Bounced],
count(case when OptOutDate is not null then 1 else null end) [Opted Out]
from
[u3_data].[data].[MailLog_f606fcabdcbd4d02816108d635cd8971] ML with(nolock)
group by MailoutID
union
select mailoutID,
count(mailoutId) [Total Messages],
count(case when status = 'Delivered' then 1 else null end) Delivered,
count(case when status <> 'Delivered' then 1 else null end) Undelivered,
count(case when ReadDate is not null then 1 else null end) [Read],
count(case when HasClicked = '1' then 1 else null end) [Unique Clicks],
count(case when BounceDate is not null then 1 else null end) [Bounced],
count(case when OptOutDate is not null then 1 else null end) [Opted Out]
from
[u3_data].[data].[MailLog_7d8f44d40f1741f187e208cf28b4343b] ML with(nolock)
group by MailoutID
),
ME as
(select MailoutID,
count(case when [Event] = 'LinkClicked' then 1 else null end) [Total Clicks],
count(case when [Event] = 'AbuseReport' then 1 else null end) [Mark as Spam]
from
[u3_data].[data].[MailEvent_0439b9d9f2c943dcaa2d08cdea94db21] ME with(nolock)
inner join [u3_data].[data].[MailLog_0439b9d9f2c943dcaa2d08cdea94db21] ML with(nolock)
on ML.ID = ME.messageID
where [Event] in ('LinkClicked','AbuseReport')
group by MailoutID
union
select MailoutID,
count(case when [Event] = 'LinkClicked' then 1 else null end) [Total Clicks],
count(case when [Event] = 'AbuseReport' then 1 else null end) [Mark as Spam]
from
[u3_data].[data].[MailEvent_f815d2dde1184930a67d08cf28b410e2] ME with(nolock)
inner join [u3_data].[data].[MailLog_f815d2dde1184930a67d08cf28b410e2] ML with(nolock)
on ML.ID = ME.messageID
where [Event] in ('LinkClicked','AbuseReport')
group by MailoutID
union
select MailoutID,
count(case when [Event] = 'LinkClicked' then 1 else null end) [Total Clicks],
count(case when [Event] = 'AbuseReport' then 1 else null end) [Mark as Spam]
from
[u3_data].[data].[MailEvent_f606fcabdcbd4d02816108d635cd8971] ME with(nolock)
inner join [u3_data].[data].[MailLog_f606fcabdcbd4d02816108d635cd8971] ML with(nolock)
on ML.ID = ME.messageID
where [Event] in ('LinkClicked','AbuseReport')
group by MailoutID
union
select MailoutID,
count(case when [Event] = 'LinkClicked' then 1 else null end) [Total Clicks],
count(case when [Event] = 'AbuseReport' then 1 else null end) [Mark as Spam]
from
[u3_data].[data].[MailEvent_7d8f44d40f1741f187e208cf28b4343b] ME with(nolock)
inner join [u3_data].[data].[MailLog_7d8f44d40f1741f187e208cf28b4343b] ML with(nolock)
on ML.ID = ME.messageID
where [Event] in ('LinkClicked','AbuseReport')
group by MailoutID)
select
mo.AccountName [Account Name]
,mo.[Mailout Folder]
,mo.[Mailout Name]
,mo.[Send Date]
,mo.[Subject line]
,ml.[Total Messages]
,ml.Delivered
,ml.Undelivered
,ml.[Read]
,ml.Delivered - ml.[Read] [Unread]
,ml.[Unique Clicks]
,me.[Total Clicks]
,ml.[Bounced]
,ml.[Opted Out]
,me.[Mark as Spam]
from mo
left join ml
on ml.mailoutID = mo.mailoutID
left join me
on me.mailoutID = ml.mailoutID

unpivot one row of aggregates to 1 column

I have this query that returns 1 row of aggregate sums
DECLARE #Income9 int
SELECT #Income9 = IncomeLevel FROM PovertyLevels WHERE HouseholdNumber = 9
;WITH CTE
AS
(
SELECT PatientProfileID, CASE WHEN v.FamilyMembersinHousehold > 8
THEN ROUND((CAST(AnnualIncome as float)/(CAST(#Income9 as float) +((V.FamilyMembersinHousehold-8)* CAST(#Income9 as Float)))*100.00), 5)
WHEN ((v.FamilyMembersinHousehold IS NULL) OR (AnnualIncome IS NULL)) THEN NULL
ELSE ROUND(((CAST(AnnualIncome AS Float)/CAST(pl.IncomeLevel as Float)) * 100.00), 5) END AS PercentOfPoverty
FROM vPatientDemographics v
LEFT OUTER JOIN PovertyLevels pl ON v.FamilyMembersinHousehold = pl.HouseholdNumber
)
SELECT SUM(CASE WHEN PercentOfPoverty <= 100 THEN 1 ELSE 0 END) AS NumOfPatientsBelow100,
SUM(CASE WHEN PercentOfPoverty BETWEEN 101 AND 150 THEN 1 ELSE 0 END) AS NumOfPatientsBetween101And150,
SUM(CASE WHEN PercentOfPoverty BETWEEN 151 AND 200 THEN 1 ELSE 0 END) AS NumOfPatientsBetween151And200,
SUM(CASE WHEN PercentOfPoverty > 200 THEN 1 ELSE 0 END) AS NumOfPatientsOver200,
SUM(CASE WHEN PercentOfPoverty IS NULL THEN 1 ELSE 0 END) AS NumOfPatientsUnknown
FROM CTE
I would like to have the sum data to be in rows not columns.
I tried adding this UNPIVOT but it does not recognize the column names.
UNPIVOT
(
Levels for PovertyLevels in (NumOfPatientsBelow100, NumOfPatientsBetween101And150, NumOfPatientsBetween151And200,
NumOfPatientsOver200, NumOfPatientsUnknown)
) as Unpvt
How can I unpivot the initial data set to that it is in rows not columns?
It is because Where clause is evaluated before the select
SELECT #Income9 = IncomeLevel FROM PovertyLevels WHERE HouseholdNumber = 9
;WITH CTE
AS
(
SELECT PatientProfileID, CASE WHEN v.FamilyMembersinHousehold > 8
THEN ROUND((CAST(AnnualIncome as float)/(CAST(#Income9 as float) +((V.FamilyMembersinHousehold-8)* CAST(#Income9 as Float)))*100.00), 5)
WHEN ((v.FamilyMembersinHousehold IS NULL) OR (AnnualIncome IS NULL)) THEN NULL
ELSE ROUND(((CAST(AnnualIncome AS Float)/CAST(pl.IncomeLevel as Float)) * 100.00), 5) END AS PercentOfPoverty
FROM vPatientDemographics v
LEFT OUTER JOIN PovertyLevels pl ON v.FamilyMembersinHousehold = pl.HouseholdNumber
),intr as
(
SELECT SUM(CASE WHEN PercentOfPoverty <= 100 THEN 1 ELSE 0 END) AS NumOfPatientsBelow100,
SUM(CASE WHEN PercentOfPoverty BETWEEN 101 AND 150 THEN 1 ELSE 0 END) AS NumOfPatientsBetween101And150,
SUM(CASE WHEN PercentOfPoverty BETWEEN 151 AND 200 THEN 1 ELSE 0 END) AS NumOfPatientsBetween151And200,
SUM(CASE WHEN PercentOfPoverty > 200 THEN 1 ELSE 0 END) AS NumOfPatientsOver200,
SUM(CASE WHEN PercentOfPoverty IS NULL THEN 1 ELSE 0 END) AS NumOfPatientsUnknown
FROM CTE
)
Select cnt,range from intr
cross apply (values (NumOfPatientsBelow100,'NumOfPatientsBelow100'),
(NumOfPatientsBetween101And150,'NumOfPatientsBetween101And150'),
(NumOfPatientsBetween151And200,'NumOfPatientsBetween151And200'),
(NumOfPatientsOver200,'NumOfPatientsOver200'),
(NumOfPatientsUnknown,'NumOfPatientsUnknown')) cs (cnt,range)

SQL procedure sum and sum inside select and insert to

I have a little problem. I have a insert into block with select and inside select I have sum. Thats works well. But In this select I need also do some operation on these sums. I don't know how.
Code:
insert into [dbo].[DiscountDailyStatsTemp]
SELECT
#DiscountId,
cast([dbo].[TelemetryData].[EventTime] as date) as 'Date',
sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountLike' then 1 else 0 end) as 'Likes',
sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountDislike' then 1 else 0 end) as 'Dis likes',
sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountShare' then 1 else 0 end) as 'Shares',
SUM(case when [dbo].[TelemetryData].[EventName]='DiscountView' then 1 else 0 end) as 'Views',
SUM(case when [dbo].[TelemetryData].[EventName]='DiscountClick' then 1 else 0 end) as 'Clicks',
Sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountCode' then 1 else 0 end) as 'Downloaded codes',
Sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountSave' then 1 else 0 end) as 'Saves',
sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountClickWWW' then 1 else 0 end) as 'Page redirections',
0 as 'Average CTR',
#UniqueUsers as 'Unique users',
#NewUsers as 'New users',
#ReturningUsers as 'Returning users',
Sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountCommentPositive' then 1 else 0 end) as 'Positive comments',
sum(case when [dbo].[TelemetryData].[EventName] = 'DiscountCommentNegative' then 1 else 0 end) as 'Negative comments'
from [dbo].[TelemetryData]
where [dbo].[TelemetryData].[DiscountId] = #DiscountId
and ([dbo].[TelemetryData].[EventName] = 'DiscountView' or [dbo].[TelemetryData].[EventName] = 'DiscountClick' or
[dbo].[TelemetryData].[EventName] = 'DiscountDislike' or [dbo].[TelemetryData].[EventName] = 'DiscountCode' or
[dbo].[TelemetryData].[EventName] = 'DiscountLike' or [dbo].[TelemetryData].[EventName] = 'DiscountShare' or
[dbo].[TelemetryData].[EventName] = 'DiscountClickWWW' or [dbo].[TelemetryData].[EventName] = 'DiscountSave' or
[dbo].[TelemetryData].[EventName] = 'DiscountCommentPositive' or [dbo].[TelemetryData].[EventName] = 'DiscountCommentNegative')
group by cast([dbo].[TelemetryData].[EventTime] as date)
order by cast([dbo].[TelemetryData].[EventTime] as date) asc
And look there is 0 as 'Average CTR' I need to change it for this:
Round(cast('Clicks' as float) / cast(case when 'Views' = 0 then 1 else 'Views') end as float) * 100, 2) as 'Average CTR',
But it not working. How I can do it?
You can't use the aliases in the same level they are created, and also this is a query with a group by clause, which means each column should be either in the group by or with an aggregation function around it.
You can wrap your query with another select :
SELECT [date],
[likes],
....
Round(cast([Clicks] as float) / cast(case when [Views] = 0 then 1 else [Views] end) as float) * 100, 2) as [Average CTR],
FROM(YOUR QUERY HERE)
Also, use square brackets for columns name.

Need to add a subquery in a CASE expression that is part of an aggregate function

I need to add the number of records in which the status is set to 'reopened'. But the 'reopened' status has several IDs.
This is the subquery that will Id the 'reopen' statuses:
SELECT (CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END) AS Reopened
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%'))
This is the main query that the above query needs to be a part:
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
(SELECT SUM(CASE WHEN s.sr_status_recid = 1 THEN 1 ELSE 0 END)
from v_rpt_service s
where vsrv.sr_status_recid in
(select distinct SR_Status_RecID from SR_Status where [Description] like '%Re-opened%')) AS ReopenedIssues,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmai
FROM v_rpt_service vsrv LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
WHERE vsrv.company_name <> 'XYZ Test Company' AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber
How can I have a subquery that uses a CASE statement and the CASE statement is aggregated?
You can use CROSS APPLY
SELECT DATEPART(WK, vsrv.date_entered) as WkNumber,
COUNT(vsrv.TicketNbr) AS OpenedIssues, --total ticket count
SUM(CASE WHEN vsrv.Closed_Flag = 1 THEN 1 ELSE 0 END) AS ClosedIssues, --sum of tickets with closed_flag = 1
SUM(CountReopen.YesNo) AS NumberOfReopen,
SUM(CASE WHEN vsrvy.Surveys_Completed = 1 THEN 1 ELSE 0 END) AS SurveysCompletedWithConnectWise, -- Surveys_Completed flag in view is 1
SUM(CASE WHEN Source = 'Portal' THEN 1 ELSE 0 END) AS IssueLoggedPortal,
SUM(CASE WHEN Source = 'Email Connector' THEN 1 ELSE 0 END) AS IssueLoggedEmail
FROM v_rpt_service vsrv
LEFT OUTER JOIN v_rpt_SurveysByTicket vsrvy
ON vsrv.TicketNbr = Vsrvy.SR_Service_RecID
CROSS APPLY (
SELECT IIF(COUNT(*) > 0,1,0) YesNo
FROM SR_Status
where [Description] like '%Re-opened%'
AND SR_Status_ID = vsrv.sr_status_recid
) CountReopen(YesNo)
WHERE vsrv.company_name <> 'XYZ Test Company'
AND vsrv.date_entered BETWEEN '01/01/2016' AND '10/07/2016'
GROUP BY DATEPART(WK, vsrv.date_entered)
ORDER BY WkNumber

My stored procedure doesn't returns rows, but my view does

I've designed the following view and tried to use it as a stored procedure; however, my stored procedure does not return the same rows as my view.
My view returns 46 rows:
SELECT
NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY,
SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070],
SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071],
SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700],
SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701],
SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705],
SUM(Cost_Center_Dollars) AS Total, Program_Type
FROM dbo.VDP_Master_Drug_Receipts_Log
WHERE Date_Entered BETWEEN '10/1/2011' AND '11/1/2011'
AND (Program_Type = 'VDP-BULK')
GROUP BY NDC_Text, Trade_Name, Vendor_Code, Program_Type
ORDER BY Trade_Name
Results
My stored procedure
GO ALTER PROCEDURE [dbo].[ShowProductByCategory](#StartDate DateTime, #EndDate DateTime, #Type Varchar(15))
AS
Return
SELECT NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY,
SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070],
SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071],
SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700],
SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701],
SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705],
SUM(Cost_Center_Dollars) AS Total, Program_Type
FROM dbo.VDP_Master_Drug_Receipts_Log
WHERE Date_Entered BETWEEN #StartDate AND #StartDate
AND (Program_Type = 'Type')
GROUP BY NDC_Text, Trade_Name, Vendor_Code, Program_Type
ORDER BY Trade_Name
Your stored procedure is selecting a dataset - so you don't need (and must not have) a RETURN clause in there!
Try this:
ALTER PROCEDURE [dbo].[ShowProductByCategory]
(#StartDate DateTime, #EndDate DateTime, #Type Varchar(15))
AS
-- Return NO RETURN HERE !!
SELECT
NDC_Text, Trade_Name, Vendor_Code, SUM(Quantity_Received) AS QTY,
SUM(CASE WHEN Cost_Center = 7070 THEN Cost_Center_Dollars ELSE 0 END) AS [7070],
SUM(CASE WHEN Cost_Center = 7071 THEN Cost_Center_Dollars ELSE 0 END) AS [7071],
SUM(CASE WHEN Cost_Center = 7700 THEN Cost_Center_Dollars ELSE 0 END) AS [7700],
SUM(CASE WHEN Cost_Center = 7701 THEN Cost_Center_Dollars ELSE 0 END) AS [7701],
SUM(CASE WHEN Cost_Center = 7702 THEN Cost_Center_Dollars ELSE 0 END) AS [7702],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7703],
SUM(CASE WHEN Cost_Center = 7703 THEN Cost_Center_Dollars ELSE 0 END) AS [7705],
SUM(Cost_Center_Dollars) AS Total, Program_Type
FROM dbo.VDP_Master_Drug_Receipts_Log
WHERE Date_Entered BETWEEN #StartDate AND #EndDate
AND (Program_Type = 'Type')
GROUP BY
NDC_Text, Trade_Name, Vendor_Code, Program_Type
ORDER BY
Trade_Name
Your stored procedure's WHERE clause has
WHERE Date_Entered BETWEEN #StartDate AND #StartDate
That should probably be #EndDate
There is also a different Progam_Type, which would lead to different results.
The problem is in WHERE clause
WHERE Date_Entered BETWEEN #StartDate AND #StartDate
AND (Program_Type = 'Type')
I think it must be
WHERE Date_Entered BETWEEN #StartDate AND #EndDate
AND (Program_Type = #Type)

Resources