This is my current sql and and a small sample of the output:
SELECT BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC, DEPBEN.DEPENDENT, EMDEPEND.REL_CODE, MPLOYEE.PROCESS_LEVEL
FROM BNBEN INNER JOIN
DEPBEN ON BNBEN.COMPANY = DEPBEN.COMPANY AND BNBEN.EMPLOYEE = DEPBEN.EMPLOYEE AND BNBEN.PLAN_TYPE = DEPBEN.PLAN_TYPE AND BNBEN.PLAN_CODE = DEPBEN.PLAN_CODE INNER JOIN
BNCOVOPT ON BNBEN.COMPANY = BNCOVOPT.COMPANY AND BNBEN.PLAN_TYPE = BNCOVOPT.PLAN_TYPE AND BNBEN.PLAN_CODE = BNCOVOPT.PLAN_CODE AND
BNBEN.COVER_OPT = BNCOVOPT.COVERAGE_OPT INNER JOIN
EMDEPEND ON DEPBEN.COMPANY = EMDEPEND.COMPANY AND DEPBEN.EMPLOYEE = EMDEPEND.EMPLOYEE AND DEPBEN.DEPENDENT = EMDEPEND.SEQ_NBR INNER JOIN
EMPLOYEE ON EMDEPEND.COMPANY = EMPLOYEE.COMPANY AND EMDEPEND.EMPLOYEE = EMPLOYEE.EMPLOYEE
WHERE (BNBEN.START_DATE = CONVERT(DATETIME, '2019-01-01 00:00:00', 102))
GROUP BY BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC, DEPBEN.DEPENDENT, EMDEPEND.REL_CODE, EMPLOYEE.PROCESS_LEVEL
HAVING (EMPLOYEE.PROCESS_LEVEL = 'HR')
Employee PlanType PlanCode CoverageOption CoverageDesc DependentSequence DependDescrip
7010 DN DN01 3 Employee + Family 1 SPOUSE
7010 DN DN01 3 Employee + Family 2 SON
7010 DN DN01 3 Employee + Family 3 DAUGHTER
I need to see the data displayed as:
Employee PlanType PlanCode CoverageOption CoverageDesc Spouse NbrDependents
7010 DN DN01 3 Employee 1 2
+Family
Thanks.
use tempdb
go
drop table #data
go
create table #data
(
Employee int ,
PlanType varchar(100) ,
PlanCode varchar(100) ,
CoverageOption int ,
CoverageDesc varchar(100) ,
DependentSequence int ,
DependDescrip varchar(100)
)
go
insert into #data select 7010,'DN','DN01',3,'Employee + Family',1,'SPOUSE'
insert into #data select 7010,'DN','DN01',3,'Employee + Family',2,'SON'
insert into #data select 7010,'DN','DN01',3,'Employee + Family',3,'DAUGHTER'
go
SQL
SELECT employee,
plantype,
plancode,
coverageoption,
coveragedesc,
Sum(CASE
WHEN dependdescrip = 'SPOUSE' THEN 1
ELSE 0
END) AS Spouse,
Sum(CASE
WHEN dependdescrip IN ( 'SON', 'DAUGHTER' ) THEN 1
ELSE 0
END) AS NbrDependents
FROM #data
GROUP BY employee,
plantype,
plancode,
coverageoption,
coveragedesc
go
I don't have permission to create temp table or view from my database. So is there another way to create subquery to get it works instead create temp table. I know this is very complicate in one query, but please help me out with this query because I only have ready permission from my SQL database.
what @harishgg1 gave you should work. the temp table he provided you is just sampling of the data you did not provide
Implementing what @harishgg1 did into your query goes something like this.
SELECT BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC,
Sum(CASE
WHEN DEPENDENT = 'SPOUSE' THEN 1
ELSE 0
END) AS Spouse,
Sum(CASE
WHEN DEPENDENT IN ( 'SON', 'DAUGHTER' ) THEN 1
ELSE 0
END) AS NbrDependents
FROM BNBEN
INNER JOIN DEPBEN ON BNBEN.COMPANY = DEPBEN.COMPANY AND BNBEN.EMPLOYEE = DEPBEN.EMPLOYEE AND BNBEN.PLAN_TYPE = DEPBEN.PLAN_TYPE AND BNBEN.PLAN_CODE = DEPBEN.PLAN_CODE
INNER JOIN BNCOVOPT ON BNBEN.COMPANY = BNCOVOPT.COMPANY AND BNBEN.PLAN_TYPE = BNCOVOPT.PLAN_TYPE AND BNBEN.PLAN_CODE = BNCOVOPT.PLAN_CODE AND BNBEN.COVER_OPT = BNCOVOPT.COVERAGE_OPT
INNER JOIN EMDEPEND ON DEPBEN.COMPANY = EMDEPEND.COMPANY AND DEPBEN.EMPLOYEE = EMDEPEND.EMPLOYEE AND DEPBEN.DEPENDENT = EMDEPEND.SEQ_NBR
INNER JOIN EMPLOYEE ON EMDEPEND.COMPANY = EMPLOYEE.COMPANY AND EMDEPEND.EMPLOYEE = EMPLOYEE.EMPLOYEE
WHERE (BNBEN.START_DATE = CONVERT(DATETIME, '2019-01-01 00:00:00', 102))
GROUP BY BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC
HAVING (EMPLOYEE.PROCESS_LEVEL = 'HR')
I got an error when try to run your query.. Please help... thank you very much
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'SPOUSE' to data type smallint.
When I changed to Dependent as you suggested,
Sum(CASE
WHEN DEPENDENT = DEPENDENT THEN 1
ELSE 0
END) AS Spouse,
Sum(CASE
WHEN DEPENDENT IN ( DEPENDENT ) THEN 1
ELSE 0
END) AS NbrDependents
I got the count for spouse and NbrDependents are the same. Here's the output
EMPLOYEE PLAN_TYPE PLAN_CODE COVER_OPT COV_DESC Spouse NbrDependents
7010 DN DN01 3 Employee + Family 3 3
7010 HL CI01 3 Employee+Children 2 2
7010 HL GAP1 3 Employee+Children 2 2
7010 HL MED4 4 EE + Family 3 3
7010 HL VS02 3 Employee + Family 3 3
the table name is EMDEPEND and column name for spouse and son are REL_CODE.
I join EmployeeID from DEPEND table to EMDEPEND
and Dependent# from DENPEND table to SEQ_NBR from EMDEPEND table
SELECT BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC,
Sum(CASE
WHEN REL_CODE= 'SPOUSE' THEN 1
ELSE 0
END) AS Spouse,
Sum(CASE
WHEN REL_CODE IN ( 'SON', 'DAUGHTER' ) THEN 1
ELSE 0
END) AS NbrDependents
FROM BNBEN
INNER JOIN DEPBEN ON BNBEN.COMPANY = DEPBEN.COMPANY AND BNBEN.EMPLOYEE = DEPBEN.EMPLOYEE AND BNBEN.PLAN_TYPE = DEPBEN.PLAN_TYPE AND BNBEN.PLAN_CODE = DEPBEN.PLAN_CODE
INNER JOIN BNCOVOPT ON BNBEN.COMPANY = BNCOVOPT.COMPANY AND BNBEN.PLAN_TYPE = BNCOVOPT.PLAN_TYPE AND BNBEN.PLAN_CODE = BNCOVOPT.PLAN_CODE AND BNBEN.COVER_OPT = BNCOVOPT.COVERAGE_OPT
INNER JOIN EMDEPEND ON DEPBEN.COMPANY = EMDEPEND.COMPANY AND DEPBEN.EMPLOYEE = EMDEPEND.EMPLOYEE AND DEPBEN.DEPENDENT = EMDEPEND.SEQ_NBR
INNER JOIN EMPLOYEE ON EMDEPEND.COMPANY = EMPLOYEE.COMPANY AND EMDEPEND.EMPLOYEE = EMPLOYEE.EMPLOYEE
WHERE (BNBEN.START_DATE = CONVERT(DATETIME, '2019-01-01 00:00:00', 102))
GROUP BY BNBEN.EMPLOYEE, BNBEN.PLAN_TYPE, BNBEN.PLAN_CODE, BNBEN.COVER_OPT, BNCOVOPT.COV_DESC
HAVING (EMPLOYEE.PROCESS_LEVEL = 'HR')