How to break my results down into Months - sql

SELECT
COUNT(CALL_KEY) AS CONTACTS,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
CAST((CAST(SUM(A.talk_time)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END) AS float) + CAST(SUM(A.hold_time) AS float)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END)) + CAST(SUM(A.work_time) AS float)
/ CAST(COUNT(CASE WHEN A.answer_time > 0 THEN 1 ELSE 1 END) AS numeric(8, 2)) AS Int) AS AHT,
SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS Total_ABN,
SUM(A.answer_speed) AS ASA,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '5/31/2016' AND A.CALENDAR_DATE <= '7/19/2016' AND A.SPLIT IN (43) AND ( A.ent_split IS NOT NULL)
Here's My Results
CONTACTS CH AHT Total_ABN ASA CO
17199 16744 640 448 580643 17192

Assuming that the month can be obtained from the A.CALENDAR_DATE column
SELECT Month(A.CALENDAR_DATE) as Month,
COUNT(CALL_KEY) AS CONTACTS,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
CAST((CAST(SUM(A.talk_time)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END) AS float) + CAST(SUM(A.hold_time) AS float)
/ (CASE WHEN COUNT(A.answer_time) = 0 THEN 1 ELSE COUNT(A.answer_time) END)) + CAST(SUM(A.work_time) AS float)
/ CAST(COUNT(CASE WHEN A.answer_time > 0 THEN 1 ELSE 1 END) AS numeric(8, 2)) AS Int) AS AHT,
SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS Total_ABN,
SUM(A.answer_speed) AS ASA,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '5/31/2016' AND A.CALENDAR_DATE <= '7/19/2016' AND A.SPLIT IN (43) AND ( A.ent_split IS NOT NULL)
Group by Month(A.CALENDAR_DATE)

Related

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

Need to break down into day..Right now its rolled up into months

SELECT A.SPLIT,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 1 END) AS AHT
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015' AND A.CALENDAR_DATE <= '9/30/2016' AND A.SPLIT IN (9,26,23,43,48,33,56,70,71,72,74,50) AND ( A.ent_split IS NOT NULL)
Group BY A.SPLIT
Heres is some of the data my code puts out.
Split CH CO AHT
9 957157 1156319 1156461
You mean this?
SELECT A.SPLIT, A.CALENDAR_DATE,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) AS CH,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 0 END) + SUM(CASE WHEN abandon_time IS NOT NULL AND answer_time IS NULL THEN 1 ELSE 0 END) AS CO,
SUM(CASE WHEN answer_time IS NOT NULL AND A.ent_split IS NOT NULL AND call_type NOT LIKE 'PBX%' THEN 1 ELSE 1 END) AS AHT
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015' AND A.CALENDAR_DATE <= '9/30/2016' AND A.SPLIT IN (9,26,23,43,48,33,56,70,71,72,74,50) AND ( A.ent_split IS NOT NULL)
Group BY A.SPLIT, A.CALENDAR_DATE

Breakdown of data

SELECT SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) AS CH
,SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) + SUM(CASE
WHEN abandon_time IS NOT NULL
AND answer_time IS NULL
THEN 1
ELSE 0
END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015'
AND A.CALENDAR_DATE <= '4/30/2016'
AND A.SPLIT IN (
35
,55
,73
)
AND (A.ent_split IS NOT NULL)
Which in return rolled up all 3 split information to
CH 175282
CO 209452
My problem is I need the data to show individual fr each one of the split IN
"Data for each split" sounds like you need to use GROUP BY. Have you tried something like this?
SELECT A.SPLIT,
SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) AS CH
,SUM(CASE
WHEN answer_time IS NOT NULL
AND A.ent_split IS NOT NULL
AND call_type NOT LIKE 'PBX%'
THEN 1
ELSE 0
END) + SUM(CASE
WHEN abandon_time IS NOT NULL
AND answer_time IS NULL
THEN 1
ELSE 0
END) AS CO
FROM reporting_call_matrix AS A WITH (NOLOCK)
WHERE A.CALENDAR_DATE >= '10/01/2015'
AND A.CALENDAR_DATE <= '4/30/2016'
AND A.SPLIT IN (
35
,55
,73
)
AND (A.ent_split IS NOT NULL)
GROUP BY A.SPLIT
ORDER BY A.SPLIT

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,