Count the number of column having non zero values - sql

I have the following script,
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
Please refer the screenshot as the output of the above script.
What I want:
I want a column after the Total as the total number of the column having nonzero values.
Like in the picture the values should be 2 as TotalCount1 and Totalcount3 have non zero values.

SELECT COUNT(*) AS Total,
...
...
CASE ( WHEN SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END) +
CASE ( WHEN SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) > 0 THEN 1 ELSE 0 END)
as SumOfNonZeros
FROM [Party]
Or maybe simpler
SELECT COUNT(*) AS Total,
COUNT(CASE WHEN TypeId ='4' THEN 1 END) AS 'TotalCount1',
COUNT(CASE WHEN TypeId ='6' THEN 1 END) AS 'TotalCount2',
COUNT(CASE WHEN TypeId ='1' THEN 1 END) AS 'TotalCount3',
COUNT(CASE WHEN TypeId ='10' THEN 1 END) AS 'TotalCount4',
COUNT(CASE WHEN TypeId ='5' THEN 1 END) AS 'TotalCount5',
COUNT(CASE WHEN TypeId ='8' THEN 1 END) AS 'TotalCount6',
COUNT( DISTINCT CASE WHEN TypeId IN ('4', '6', '1', '10', '5', '8')
THEN TypeId
END ) as CountOfNonZeros
FROM [Party]

Using CASE expression over the selecting result, you can do this:
SELECT (
CASE WHEN TotalCount1 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount2 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount3 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount4 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount5 > 0 THEN 1 ELSE 0 END +
CASE WHEN TotalCount6 > 0 THEN 1 ELSE 0 END) AS Result
FROM (
SELECT COUNT(*) AS Total,
SUM(CASE WHEN TypeId ='4' THEN 1 ELSE 0 END) AS 'TotalCount1',
SUM(CASE WHEN TypeId ='6' THEN 1 ELSE 0 END) AS 'TotalCount2',
SUM(CASE WHEN TypeId ='1' THEN 1 ELSE 0 END) AS 'TotalCount3',
SUM(CASE WHEN TypeId ='10' THEN 1 ELSE 0 END) AS 'TotalCount4',
SUM(CASE WHEN TypeId ='5' THEN 1 ELSE 0 END) AS 'TotalCount5',
SUM(CASE WHEN TypeId ='8' THEN 1 ELSE 0 END) AS 'TotalCount6'
FROM [Party]
) A

Related

SQL group by within sum()

I have the following sql statement which produces the following output (filtered result for 7/8 DueDate)
SELECT
JobType.BillingCategory,
Jobs.DueDate,
Sum(Impressions.PRINTtot) AS SumOfPRINTtot,
Sum(Impressions.PRINTrem) AS SumOfPRINTrem,
Sum(Impressions.CARDtot) AS SumOfCARDtot,
Sum(Impressions.CARDrem) AS SumOfCARDrem,
Sum(Impressions.BOOKtot) AS SumOfBOOKtot,
Sum(Impressions.BOOKrem) AS SumOfBOOKrem
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate,
JobType.BillingCategory;
I am trying to get all of these results on one line: the identifier would be the DueDate and the sums of the values in the Impressions table would be summed for each BillingCategory. Example below (omitting CARD & BOOK sums just for visual purposes w/ too many columns)
You could use a CASE expression to summarize your data as such. You could modify your query to sum for only that billing category, I have used CARD in the example below to summarize the metrics for Impressions.PRINTtot and SumOfPRINTrem
SELECT
Jobs.DueDate,
Sum(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCard,
Sum(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCard,
....<repeat>
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate
Edit 1:
Based on the Billing Categories listed in your question
A complete example may look like:
SELECT
Jobs.DueDate,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotCARD,
SUM(CASE WHEN JobType.BillingCategory='CARD' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremCARD,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='CARDTIPON' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremCARDTIPON,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotEOB,
SUM(CASE WHEN JobType.BillingCategory='EOB' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremEOB,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDE' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremMEMBERGUIDE,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.PRINTtot ELSE 0 END) AS SumOfPRINTtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.PRINTrem ELSE 0 END) AS SumOfPRINTremMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.CARDtot ELSE 0 END) AS SumOfCARDtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.CARDrem ELSE 0 END) AS SumOfCARDremMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.BOOKtot ELSE 0 END) AS SumOfBOOKtotMEMBERGUIDEHD,
SUM(CASE WHEN JobType.BillingCategory='MEMBERGUIDEHD' THEN Impressions.BOOKrem ELSE 0 END) AS SumOfBOOKremMEMBERGUIDEHD
FROM
(
Impressions
INNER JOIN Jobs ON Impressions.JobNo = Jobs.JobNo
)
INNER JOIN JobType ON (Jobs.AccountName = JobType.AccountName)
AND (Jobs.Product = JobType.Product)
GROUP BY
Jobs.DueDate
With a specific database/tool there may be various functions that may prove useful. However, I find in these cases especially since your billing categories may change over time, a script where you can run anywhere is sometimes helpful. I've included the script I used to generate the code below
var types='CARD,CARDTIPON,EOB,MEMBERGUIDE,MEMBERGUIDEHD'.split(',');
var metrics = metrics='PRINTtot,PRINTrem,CARDtot,CARDrem,BOOKtot,BOOKrem'.split(',');
var metricTemplate="SUM(CASE WHEN JobType.BillingCategory='[TYPE]' THEN Impressions.[METRICNAME] ELSE 0 END) AS SumOf[METRICNAME][TYPE]";
var summary_lines = []
for(var i=0;i < types.length;i++){
for(var j=0;j<metrics.length;j++){
summary_lines.push(metricTemplate.replaceAll('[TYPE]',types[i]).replaceAll('[METRICNAME]',metrics[j]))
}
}
complete_metrics = summary_lines.join(",\n");
console.log(complete_metrics)
The simplest option is to use your query as a CTE (Common Table Expression) and then you can use it as a base for another query.
For example:
with
q as (
-- your query here
)
select
max(DueDate) as DueDate,
sum(case when BillingCategory = 'CARD' then SumOfPRINTtot else 0 end) as SumOfPRINTtotC,
sum(case when BillingCategory = 'CARD' then SumOfPRINTrem else 0 end) as SumOfPRINTremC,
sum(case when BillingCategory = 'CARDTIPON' then SumOfPRINTtot else 0 end) as SumOfPRINTtotCT,
sum(case when BillingCategory = 'CARDTIPON' then SumOfPRINTrem else 0 end) as SumOfPRINTremCT,
sum(case when BillingCategory = 'EOB' then SumOfPRINTtot else 0 end) as SumOfPRINTtotE,
sum(case when BillingCategory = 'EOB' then SumOfPRINTrem else 0 end) as SumOfPRINTremE,
sum(case when BillingCategory = 'MEMBERGUIDE' then SumOfPRINTtot else 0 end) as SumOfPRINTtotMG,
sum(case when BillingCategory = 'MEMBERGUIDE' then SumOfPRINTrem else 0 end) as SumOfPRINTremMG,
sum(case when BillingCategory = 'MEMBERGUIDEHD' then SumOfPRINTtot else 0 end) as SumOfPRINTtotMGH,
sum(case when BillingCategory = 'MEMBERGUIDEHD' then SumOfPRINTrem else 0 end) as SumOfPRINTremMGH
from q
In some databases you can use the FILTER clause as well. You don't mention which specific database, so this solution will work on virtually all databases.

How to do mathematical comparision with SQL CASE statement

I have a student table around 100k records and I have two types of data in it: student name and level type with selection values primary, secondary, intermediate & university
I want to filter out the student from this table, whose have count > 0, in all level primary, secondary, intermediate & university
I was able to find the sum for each student in each level using the following query
SELECT
student_id,
SUM(CASE WHEN lev_type = 'primary' THEN 1 ELSE 0 END) AS primary,
SUM(CASE WHEN lev_type = 'secondary' THEN 1 ELSE 0 END) AS secondary,
SUM(CASE WHEN lev_type = 'intermediate' THEN 1 ELSE 0 END) AS intermediate,
SUM(CASE WHEN level_type = 'university' THEN 1 ELSE 0 END) AS university
FROM
student_details
GROUP BY
student_id
and I am getting a result like (note that my result is 92242 row(s))
attendee_id primary secondary intermediate uni
student1 0 1 1 2
student2 0 1 1 0
student3 88 209 92 32
student4 0 1 1 0
student5 0 1 1 0
How to filter out student3 from this result?
You can simply add a where statement as follows:
SELECT student_id,
SUM(case when lev_type = 'primary' then 1 else 0 end) as primary,
SUM(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
SUM(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
SUM(case when lev_type = 'university' then 1 else 0 end) as university
FROM student_details
GROUP BY student_id
WHERE primary = 0 OR secondary = 0 OR intermediate = 0 OR university = 0
HAVING might get you what you want. For example:
SELECT student_id,
sum(case when lev_type = 'primary' then 1 else 0 end) as primary,
sum(case when lev_type = 'secondary' then 1 else 0 end) as secondary ,
sum(case when lev_type = 'intermediate' then 1 else 0 end) as intermediate ,
sum(case when level_type = 'university' then 1 else 0 end) as university
from student_details
group by student_id
-- Put the criteria here by which you want to filter
having sum(case when lev_type = 'primary' then 1 else 0 end) = 0
and sum(case when lev_type = 'secondary' then 1 else 0 end) = 0
and sum(case when lev_type = 'intermediate' then 1 else 0 end) = 0
and sum(case when level_type = 'university' then 1 else 0 end) = 0

missing expression error

Any reason why i'm getting a missing expression error?
select
CASE WHEN
(
SELECT COUNT(*) AS COUNT
FROM TRANSPLANT_ORGANS
INNER JOIN ORGAN_TYPE ON TRANSPLANT_ORGANS.TX_ORG_ID = ORGAN_TYPE.ORG_RECORD_ID
WHERE COUNT > 0
)
THEN
(
SELECT SUBSTR(CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 660 AND 662 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/KIDNEY' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 640 AND 650 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/LIVER' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (620,622) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/PANCREAS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (621) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/PANCREAS ISLETS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (700) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/HEART' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (600) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/LUNGS' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 601 AND 607 THEN 1 ELSE 0 END) WHEN 2 THEN '/LUNGS' WHEN 0 THEN '' ELSE '/LUNG' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (680) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/INTESTINES' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 685 AND 686 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE 'SMALL INTESTINE' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID BETWEEN 683 AND 684 THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE 'LARGE INTESTINES' END,
CONCAT(CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (681) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/DUODENUM' END,
CASE SUM(CASE WHEN ORGAN_TYPE.ORG_ID IN (682) THEN 1 ELSE 0 END) WHEN 0 THEN '' ELSE '/STOMACH' END))))))))))),2,255)
FROM TRANSPLANT_ORGANS
INNER JOIN ORGAN_TYPE ON TRANSPLANT_ORGANS.TX_ORG_ID = ORGAN_TYPE.ORG_RECORD_ID
)
END
First off, you are retrieving a COUNT from transplant_organs, but then never doing a comparison. WHERE COUNT > 0 can be removed and then with the ending parenthesis you can put ) > 0.
Second, your outer select doesn't have a FROM clause. At the very least use FROM dual.
Example:
SELECT CASE
WHEN (SELECT 1 FROM dual) > 0
THEN (SELECT 'Something' FROM dual)
END
FROM dual;

MS SQL 2008 Studio creating syntax error

I have a project where I create a query string dynamically. It's not too complicated, just complex. I specifically have a problem adding a select into the mix. Studio adds an "as" when I save the view.
SELECT TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState,
(CASE WHEN (dbo.Users_Info.Address1 = '' OR
dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
(SELECT UserID
FROM Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_ES
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_CS
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per,
dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
dbo.Users_Info.Custom_JobAd = 1 OR
dbo.Users_Info.Elite_JobAd = 1 OR
dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
(SELECT MAX(date_purchased) AS EJA_Date_Purchased
FROM dbo.QEliteContacts
WHERE (contact_id = dbo.Users.ID)) AS Date_Purchased_EJA,
dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,
The view QEliteContacts is as follows:
SELECT bd.date_purchased, bd.ID, csc.contact_id
FROM dbo.Board_Details AS bd LEFT OUTER JOIN
dbo.Client_Services_Contacts AS csc ON csc.service_id = bd.ID
WHERE (bd.ad_type LIKE '%Elite%')
Near the bottom of the first block of code is the statement "(SELECT MAX(date_purchased) AS EJA_Date_Purchased". This is the new code I have added to the view. I get and error "invalid syntax near 'as'". If I remove the alias from the max(date_purchased) studio will add 'as expr1' when I save the view. If I remove the alias from the select, studio will add 'as expr1' when I save the view.
To tell the truth, I'm not sure I'm attacking the problem of getting the max date properly.
Remove the second 'as'.ie, remove the portion ' AS Date_Purchased_EJA ' and give that instead of ' AS EJA_Date_Purchased '.In effect there should be only the first 'AS' statement.Then execute the query in Query Designer window and not in View Designer Window.The code is shown below.
SELECT TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState,
(CASE WHEN (dbo.Users_Info.Address1 = '' OR
dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
(SELECT UserID
FROM Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_ES
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_CS
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per,
dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
dbo.Users_Info.Custom_JobAd = 1 OR
dbo.Users_Info.Elite_JobAd = 1 OR
dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
(SELECT MAX([EJA_Date_Purchased]) from
(SELECT date_purchased AS [EJA_Date_Purchased]
FROM dbo.QEliteContacts
WHERE (contact_id = dbo.Users.ID))t),
dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,

PL/SQL cannot add more than 2 arguments in add operator?

I have this procedure:
PROCEDURE P_SUMMARIZE_ACTIVATED
(
pStartDate IN DATE,
pEndDate IN DATE,
SummaryCur OUT MEGAGREEN_CUR
)
IS
BEGIN
OPEN SummaryCur FOR
SELECT USER_ID,
sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) AS Package1,
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) AS Package2,
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END) AS Package3,
(Package1 + Package2 + Package3) AS TOTAL
FROM ALL_ADSL
WHERE ACTIVATION_DATE BETWEEN pStartDate AND pEndDate
group by USER_ID
ORDER BY USER_ID;
END;
This procedure cannot be complied, SQL Developer returns this error:
Error(544,36): PL/SQL: ORA-00904: "PACKAGE3": invalid identifier
How can I do to fix that?
Thank you.
You can nest the SQL to avoid duplicating the SUM and CASE expressions:
SELECT USER_ID,
Package1,
Package2,
Package3,
(Package1 + Package2 + Package3) AS TOTAL
FROM
( SELECT USER_ID,
sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) AS Package1,
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) AS Package2,
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END) AS Package3
FROM ALL_ADSL
WHERE ACTIVATION_DATE BETWEEN pStartDate AND pEndDate
group by USER_ID
)
ORDER BY USER_ID;
Alternatively, if there are only 3 service types in total, you could do:
SELECT USER_ID,
sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) AS Package1,
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) AS Package2,
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END) AS Package3
COUNT(*) AS Total
FROM ALL_ADSL
WHERE ACTIVATION_DATE BETWEEN pStartDate AND pEndDate
group by USER_ID
ORDER BY USER_ID;
Or if there are other service types to exclude then
SELECT USER_ID,
sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) AS Package1,
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) AS Package2,
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END) AS Package3
SUM(CASE WHEN SERVICETYPE_ID IN (1,2,3) THEN 1 ELSE 0 END) AS Total
FROM ALL_ADSL
WHERE ACTIVATION_DATE BETWEEN pStartDate AND pEndDate
group by USER_ID
ORDER BY USER_ID;
The error comes from using the alias of the 3 sums. Try using this instead:
PROCEDURE P_SUMMARIZE_ACTIVATED
(
pStartDate IN DATE,
pEndDate IN DATE,
SummaryCur OUT MEGAGREEN_CUR
)
IS
BEGIN
OPEN SummaryCur FOR
SELECT USER_ID,
sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) AS Package1,
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) AS Package2,
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END) AS Package3,
(sum(case SERVICETYPE_ID WHEN 1 THEN 1 ELSE 0 END) +
SUM(CASE SERVICETYPE_ID WHEN 2 THEN 1 ELSE 0 END) +
SUM(CASE SERVICETYPE_ID WHEN 3 THEN 1 ELSE 0 END)) INTO TOTAL
FROM ALL_ADSL
WHERE ACTIVATION_DATE BETWEEN pStartDate AS pEndDate
group by USER_ID
ORDER BY USER_ID;
END;