An item with the same key has already been added in SSRS - sql

Why I am getting the error:
An item with the same key has already been added.
In ssrs for code:
SELECT e.id AS eid,
p.dscr AS jdscr,
e.startdate AS hdate,
Concat(e.fname,e.lname) AS NAME,
m.weekenddate AS weekenddate
m.bramt AS bramt,
m.conamt AS conamt,
w.twork AS twork
FROM tblemp e
LEFT JOIN
(
SELECT cid,
weekenddate,
COALESCE((
CASE
WHEN mrtype = 'BR' THEN sum(mgamt)
END),0) AS bramt ,
COALESCE((
CASE
WHEN mrtype = 'con' THEN sum(mgamt)
END),0) AS conamt
FROM mngrtbl
GROUP BY cid,
weekendate,
mrtype) m ON e.id = m.cid
LEFT JOIN
(
SELECT emp,
sum(twork) AS twork
FROM tblans
GROUP BY emp) w ON e.id = w.emp
LEFT JOIN pname p ON e.pid = p.ponid

Related

IF ELSE in sql query

I would like to know what's wrong in this query. In fact I have 3 tables and the idea is that I have 4 dates date_vesting_1 to date_vesting_4, and I should check the Last completed date to compere it with another date. So I used If ELSE in the query to start from date 4 to date 1 but it crashes every time I excute.
Thank you in advance.
IF ( (SELECT (c.Date_vesting_4) FROM D_plan_characteristics AS c ) is NOT NULL)
SELECT
e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM
R_employee As e INNER JOIN
(
D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p
ON c.charac_plan_id = p.charac_plan_id
)
ON e.emp_id = p.emp_id
WHERE
( (c.date_cessibility) < (c.Date_vesting_4) )
ELSE IF ( (SELECT (c.Date_vesting_3) FROM D_plan_characteristics As c ) is NOT NULL)
SELECT
e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM
R_employee As e INNER JOIN
(
D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p
ON c.charac_plan_id = p.charac_plan_id
)
ON e.emp_id = p.emp_id
WHERE
( (c.date_cessibility) < (c.Date_vesting_3) )
ELSE IF ((SELECT (c.Date_vesting_2) FROM D_plan_characteristics As c ) is NOT NULL)
SELECT
e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM
R_employee As e INNER JOIN
(
D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p
ON c.charac_plan_id = p.charac_plan_id
)
ON e.emp_id = p.emp_id
WHERE
( (c.date_cessibility) < (c.Date_vesting_2) )
ELSE IF ((SELECT (c.Date_vesting_1) FROM D_plan_characteristics As as c ) is NOT NULL)
SELECT
e.emp_id, e.emp_lname, e.emp_fname, e.effective_date As eff_date
FROM
R_employee As e INNER JOIN
(
D_plan_characteristics AS c INNER JOIN D_collaborator_plan As p
ON c.charac_plan_id = p.charac_plan_id
)
ON e.emp_id = p.emp_id
WHERE
( (c.date_cessibility) < (c.Date_vesting_1) );
I think it's simpler to just use one single query using coalesce:
SELECT e.emp_id,
e.emp_lname,
e.emp_fname,
e.effective_date AS eff_date
FROM r_employee AS e
INNER JOIN ( d_plan_characteristics AS c
INNER JOIN d_collaborator_plan AS p
ON c.charac_plan_id = p.charac_plan_id )
ON e.emp_id = p.emp_id
WHERE ( ( c.date_cessibility ) <
c.date_cessibility < Nz(c.date_vesting_4, Nz(c.date_vesting_3,Nz(c.date_vesting_2,Nz(c.date_vesting_1,getdate()))))
)
It "crashes" because the syntax is way off that of Access SQL.
So locate a tutorial in Access and queries. You'll need a quite different approach than your current.
Finally this is the answer:
SELECT
e.emp_id,
e.emp_lname,
e.emp_fname,
e.effective_date AS eff_date
FROM r_employee AS e
INNER JOIN ( d_plan_characteristics AS c
INNER JOIN d_collaborator_plan AS p
ON c.charac_plan_id = p.charac_plan_id )
ON e.emp_id = p.emp_id
WHERE ( ( c.date_cessibility ) < Nz(c.Date_vesting_4, Nz(c.Date_vesting_3, Nz(c.Date_vesting_2, Nz(c.Date_vesting_1, ""))))
);

Oracle SQL inner join

I have following query, which works fine.
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
exists_db = 0
and is_checked = 1
and batch_counter > 0
and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN koll_customer_export e
ON c.id = e.id
But, when I add another JOIN (last join) with SELECT and WHERE it gives error: "Invalid identifier"
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
exists_db = 0
and is_checked = 1
and batch_counter > 0
and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
SELECT time_of_delivery
FROM koll_customer_export
WHERE time_of_delivery=2) e
ON e.id = c.id
I wonder, what is problem with the last join?
This is the join:
INNER JOIN
(SELECT time_of_delivery
FROM koll_customer_export
WHERE time_of_delivery=2
) e
ON e.id = c.id
You are referring to e.id, but you don't have that in the subquery. Try this:
INNER JOIN
(SELECT time_of_delivery, id
--------------------------^
FROM koll_customer_export
WHERE time_of_delivery=2
) e
ON e.id = c.id
INNER JOIN (
SELECT time_of_delivery
FROM koll_customer_export
WHERE time_of_delivery=2) e
ON e.id = c.id
there is no column named id.

SQL complex join query with sum

I am trying to obtain sum of amounts based on a column(sysprocode) which is unique for the composite key (Organizationunitid and payrollcodeid).So far I have managed to get amount based on Organizationunitid and payrollcodeid but not alongside sysprocode.A look at my SQLfiddle should clarify this more Click
Here I have tried this sql query
SELECT TB1.OUId,TB1.OUName,(TB2.Amount - TB3.ManualDeduction) AS amt
FROM
(
SELECT ou.OrganizationUnitID AS OUId,ou.OrganizationUnitName AS OUName
FROM OrganizationUnits ou
) TB1,
(SELECT e.OrganizationUnitID AS OUId,SUM(trn.Amount) AS Amount
FROM Employees e
LEFT JOIN tblPeriodTransactions trn ON (e.EmployeeID=trn.Employee_ID)
where trn.Period_Month =6 and trn.Period_Year=2013 and trn.PayrollCode_ID=2
GROUP BY e.OrganizationUnitID
)TB2,
(SELECT e.OrganizationUnitID AS OUId,SUM(ep.ManualDeduction) AS ManualDeduction
FROM Employees e
LEFT JOIN tblEmployeePension ep ON (e.EmployeeID=ep.Employee_ID)
GROUP BY e.OrganizationUnitID
)TB3
WHERE (TB2.OUId=TB1.OUId)
AND (TB3.OUId=TB1.OUId)
Here is sample output in imgur sample output
Check the following query whether it is working or not but i think calculating pension is problem because there is no PayrollCode_ID in tblEmployeePension:
SELECT
OU.OrganizationUnitID,
OU.OrganizationUnitName,
SPC.sysprocode,
PC.PayrollCode_ID,
SUM(PD.Amount),
(SELECT SUM(tblEmployeePension.ManualDeduction) FROM tblEmployeePension WHERE Employee_ID IN (SELECT Employee_ID FROM Employees WHERE OrganizationUnitID = OU.OrganizationUnitID)) AS Pension,
SUM(PD.Amount) - (SELECT SUM(tblEmployeePension.ManualDeduction) FROM tblEmployeePension WHERE Employee_ID IN (SELECT Employee_ID FROM Employees WHERE OrganizationUnitID = OU.OrganizationUnitID)) as amt
FROM
tblPeriodTransactions PD
INNER JOIN
Employees E
ON
PD.Employee_ID = E.EmployeeID
INNER JOIN
OrganizationUnits OU
ON
E.OrganizationUnitID = OU.OrganizationUnitID
INNER JOIN
tblPayrollCode PC
ON
PD.PayrollCode_ID = PC.PayrollCode_ID
INNER JOIN
sysprocodes SPC
ON
SPC.organisationunitid = OU.OrganizationUnitID AND
SPC.PayrollCode_ID = PC.PayrollCode_ID
GROUP BY
OU.OrganizationUnitID,
PC.PayrollCode_ID,
SPC.sysprocode,
OU.OrganizationUnitName
I solved it finally after fumbling about
SELECT TB4.Syscode,
CASE TB4.accountType WHEN 'c' THEN concat('-', (TB2.Amount - TB3.ManualDeduction))
ELSE (TB2.Amount - TB3.ManualDeduction) end AS amount
FROM
(
SELECT ou.OrganizationUnitID AS OUId,ou.OrganizationUnitName AS OUName
FROM OrganizationUnits ou
) TB1,
(SELECT e.OrganizationUnitID AS OUId,SUM(trn.Amount) AS Amount
FROM Employees e
LEFT JOIN tblPeriodTransactions trn ON (e.EmployeeID=trn.Employee_ID)
WHERE trn.Period_Month = 6
AND trn.Period_Year = 2013
AND trn.PayrollCode_ID = 2
GROUP BY e.OrganizationUnitID
)TB2,
(SELECT e.OrganizationUnitID AS OUId,SUM(ep.ManualDeduction) AS ManualDeduction
FROM Employees e
LEFT JOIN tblEmployeePension ep ON (e.EmployeeID=ep.Employee_ID)
GROUP BY e.OrganizationUnitID
)TB3,
(SELECT ou.OrganizationUnitID AS OUId,sp.sysprocode as Syscode,sp.accountType AS accountType
FROM OrganizationUnits ou
INNER JOIN sysprocodes sp ON (ou.OrganizationUnitID=sp.organisationunitid)
INNER JOIN tblpayrollcode pc ON (pc.PayrollCode_ID = sp.PayrollCode_ID)
where sp.PayrollCode_ID = 2
GROUP BY ou.OrganizationUnitID,sp.sysprocode,sp.PayrollCode_ID,sp.accountType
)TB4
WHERE (TB2.OUId=TB1.OUId)
AND (TB3.OUId=TB1.OUId)
AND(TB4.OUId =tb3.OUId)
Click HERE to view the SQLFIDDLE

SQL Inner join group... missing expression

I have following query, which works fine:
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
exists_db = 0
and is_checked = 1
and batch_counter > 0
and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
SELECT id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock
FROM koll_customer_export) e
ON e.id = c.id
My requirement is to add another column 'YellowCategory'. For that I tried to change the query to following:
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE
exists_db = 0
and is_checked = 1
and batch_counter > 0
and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
SELECT id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock, COUNT(b.batch_counter) AS YellowCategory,
FROM koll_customer_export
WHERE to_date(created_date,'DD.MM.RRRR HH24:MI:SS') < to_date(sysdate-time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
GROUP BY b.batch_counter) e
ON e.id = c.id
But then I get "missing expression" error. I guess in last INNER JOIN. Don't know where is the problem... Any help?
Update
With following query, I get b.batch_counter invalid identifier error now.
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount,
b.InvoiceCount, e.time_of_delivery, e.time_of_delivery_mail,
e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c
INNER JOIN (
SELECT batch_prefix, batch_counter, COUNT(*) AS BatchCount,
SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE exists_db = 0 and is_checked = 1
and batch_counter > 0 and trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix
) b
ON b.batch_prefix=c.customer_prefix
INNER JOIN (
SELECT id, time_of_delivery, time_of_delivery_mail,
time_of_delivery_clock,
COUNT(b.batch_counter) AS YellowCategory
FROM koll_customer_export
WHERE to_date(created_date,'DD.MM.RRRR HH24:MI:SS')
< to_date(sysdate- time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
GROUP BY b.batch_counter
) e
ON e.id = c.id
Is this the query you want?
SELECT c.id, c.customer_name, b.batch_prefix, b.BatchCount, b.InvoiceCount,
e.time_of_delivery, e.time_of_delivery_mail, e.time_of_delivery_clock, e.YellowCategory
FROM koll_customers c INNER JOIN
(SELECT batch_prefix, COUNT(*) AS BatchCount, SUM (batch_counter) AS InvoiceCount
FROM koll_batchlogs
WHERE exists_db = 0 and is_checked = 1 and batch_counter > 0 and
trunc(created_date) > trunc(sysdate-7)
GROUP BY batch_prefix
) b
ON b.batch_prefix = c.customer_prefix INNER JOIN
(SELECT id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock,
COUNT(*) AS YellowCategory,
FROM koll_customer_export
WHERE to_date(created_date, 'DD.MM.RRRR HH24:MI:SS') < to_date(sysdate-time_of_delivery,'DD.MM.RRRR HH24:MI:SS')
GROUP BY id, time_of_delivery, time_of_delivery_mail, time_of_delivery_clock
) e
ON e.id = c.id;
It is impossible for me to say if this is what you really need. But I think the query will at least compile so you can run it.. The where clause in the e subquery looks really strange. Why would you be converting a date column into a date column using to_date()?
You typed one comma too much at the end of the SELECT list inside the last INNER JOIN.

How to Merge 2 Queries in 1

I have these 2 SQL queries that i need to join.
Query1 (Shows the list of Entiries):
SELECT
E.EntityId
,E.EntityNo
, E.Name
, E.ShortName
FROM [xx].[xx].[Entities] E
WHERE
E.Type = 'A'
AND E.YearEndMonth = 6
Query2 (Shows list of Contacts):
SELECT P.Firstname,P.LastName, P.Email
FROM [xxx].[xx].[People] P
WHERE P.PeopleId= (
SELECT TOP 1 A.PeopleId
FROM [xxx].[xx].[EntityAttentions] A
WHERE A.EntityId = ?
ORDER BY A.EntityAttentionTypeId ASC
)
My Objective is to have a combined list of Entities and Contact people.
I tried this but with a have error (Only one expression can be specified in the select list when the subquery is not introduced with EXISTS)
SELECT
E.EntityId
,E.EntityNo
, E.Name
, E.ShortName
,
(SELECT P.Firstname,P.LastName, P.Email
FROM [xxx].[xx].[People] P
WHERE P.PeopleId= (
SELECT TOP 1 A.PeopleId
FROM [xxx].[xx].[EntityAttentions] A
WHERE A.EntityId = E.EntityId
ORDER BY A.EntityAttentionTypeId ASC
)
)
FROM [xxx].[xx].[Entities] E
WHERE
E.Type = 'A'
AND E.YearEndMonth = 6;
I guess I must use some kind of JOIN but don't know exactly what to do.
Any help is greatly appreciated.
Try this,
SELECT
E.EntityId
,E.EntityNo
, E.Name
, E.ShortName
, B.Firstname,B.LastName, B.Email
FROM [xxx].[xx].[Entities] E
CROSS APPLY
( SELECT P.Firstname,P.LastName, P.Email
FROM [xxx].[xx].[People] P
WHERE P.PeopleId=
(
SELECT TOP 1 A.PeopleId
FROM [xxx].[xx].[EntityAttentions] A
WHERE A.EntityId = E.EntityId
ORDER BY A.EntityAttentionTypeId ASC
)
) B
WHERE
E.Type = 'A'
AND E.YearEndMonth = 6;
Try this one -
SELECT
E.EntityId
, E.EntityNo
, E.Name
, E.ShortName
, P.*
FROM [xxx].[xx].[Entities] E
OUTER APPLY (
SELECT P.Firstname,P.LastName, P.Email
FROM [xxx].[xx].[People] P
WHERE P.PeopleId = (
SELECT TOP 1 A.PeopleId
FROM [xxx].[xx].[EntityAttentions] A
WHERE A.EntityId = E.EntityId
ORDER BY A.EntityAttentionTypeId
)
) P
WHERE E.Type = 'A'
AND E.YearEndMonth = 6;