missing expression error - sql

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;

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.

Grouping case when results

I have the following query working.
select sh.encounter_id
, claims_total_allowable_cost
, (case when proc_code_desc = 'Inj Cefazolin Sodium, 500mg' then 1 else 0 end) as Cefazolin
, (case when proc_code_desc = 'Inj Cefoxitin Sodium, 1 G' then 1 else 0 end) as Cefoxitin
, (case when proc_code_desc = 'Inj Fentanyl Citrate' then 1 else 0 end) as Fentanyl
, (case when proc_code_desc = 'Inj Hydromorphone, 4mg' then 1 else 0 end) as Hydro
, (case when proc_code_desc = 'Inj Ketorolac Trometha, 15mg' then 1 else 0 end) as Ketorolac
, (case when proc_code_desc = 'Inj, Ondansetron Hci, 1 Mg' then 1 else 0 end) as Ondansetron
, (case when proc_code_desc = 'Inj, Propofol, 10 Mg' then 1 else 0 end) as Propofol
, (case when proc_code_desc = 'Ringer*' then 1 else 0 end) as Ringers
from health as sh
where cohort_description = 'Lap '
and sh.admit_dtm >= '2018-10-01'::date
and sh.admit_dtm <= '2019-09-30'::date
I get a result like this one:
encounter_id claims_total_allowable_cost cefazolin cefoxitin fentanyl hydro ketorolac
121 4200.85 0 0 0 0 0
121 4200.85 0 0 0 0 0
121 4200.85 0 0 0 0 1
121 4200.85 0 0 0 1 0
I would like a result like this:
encounter_id claims_total_allowable_cost cefazolin cefoxitin fentanyl hydro ketorolac
121 4200.85 0 0 0 1 1
Any help would be greatly appreciated!!
Just add group by and max() to your existing query
Select column1, max(column2),
Max(column3) ...
From (your existing query)
Group by column1

Count the number of column having non zero values

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

Trying to fix this -- found ")" (at char 81) expecting a keyword -- error

I'm trying to run this but I keep getting, error
^ found ")" (at char 81) expecting a keyword
The char 81 is in the 2nd SELECT portion AS
There is clearly an keyword AS in there
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
You have wrong closing ) after each END.. try removing
SELECT full_name, clm_id,
CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '0TO30'
WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END AS '31TO60'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '61TO90'
WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END AS '91TO120'
WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END AS 'GreaterThan120'
ELSE 'OTHERS'
END AS DAYS_RANGE,
COUNT(*) as NUMBER_OF_DAYS
FROM
(
SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%'
AND EXTNL_STAT_CD = 'SUBMITTED'
) A
LEFT JOIN mt_employee_current_vw
ON clm_claim_rds_vw.examined_by_empl_id = mt_employee_current_vw.employee_number
GROUP BY full_name
I think you want conditional aggregation:
SELECT ec.full_name, ec.clm_id,
SUM(CASE WHEN OPEN_DAYS BETWEEN 0 AND 30 THEN 1 ELSE 0 END) AS cnt_0TO30,
SUM(CASE WHEN OPEN_DAYS BETWEEN 31 AND 60 THEN 1 ELSE 0 END) AS cnt_31TO60,
SUM(CASE WHEN OPEN_DAYS BETWEEN 61 AND 90 THEN 1 ELSE 0 END) AS cnt_61TO90,
SUM(CASE WHEN OPEN_DAYS BETWEEN 91 AND 120 THEN 1 ELSE 0 END) AS cnt_91TO120,
SUM(CASE WHEN OPEN_DAYS >= 120 THEN 1 ELSE 0 END) AS cnt_GreaterThan120
SUM(CASE WHEN OPEN_DAYS IS NULL OR OPEN_DAYS < 0 THEN 1 ELSE 0 END) as cnt_Others,
COUNT(*) as NUMBER_OF_DAYS
FROM (SELECT DATE(CURRENT_DATE) - DATE(RCVD_DT) AS Open_Days
FROM clm_claim_rds_vw
WHERE CLM_ID like 'CC%' AND
EXTNL_STAT_CD = 'SUBMITTED'
) ccr LEFT JOIN
mt_employee_current_vw ec
ON ccw.examined_by_empl_id = ec.employee_number
GROUP BY ec.full_name, ec.clm_id;

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,