I have a table similar to structure below
table
ID EmployeeID factor
1. 100 A
2. 100 B
2. 100 C
my requirement is for a given EmployeeID I need to replace the factor with numerical values and sum the factor where A = 1 , B=2 and C=3
using a select query I was able to replace the values and see the table
select employeeid,
case factor
WHEN 'F' THEN 4
WHEN 'EC' THEN 2
WHEN 'E' THEN 1
ELSE 0
END as factor
From employee
where employee_id = 100
but when I try to use sum and group by on this getting query syntax error
select employeeid,count(*) as count
sum(case factor
WHEN 'F' THEN 4
WHEN 'EC' THEN 2
WHEN 'E' THEN 1
ELSE 0
END as factor) as factor
From employee
where employee_id = 100
group by employeeid,count
2
Answers
Use
GROUP BY and HAVING
to fetch data for particular employee after grouping data with that key.Also correct the
SUM(CASE...END)
statementSo, SQL query should be:
Sample Query: db<>fiddle