I'm getting error cannot perform an aggregate function on an expression containing an aggregate or a subquery - sql-server

SELECT sum(sum(DATEDIFF(DAY,LeaveBreakup.StartDate,LeaveBreakup.EndDate)+1)) AS totalNoOfDays,
LeaveApplication.Id,
LeaveBreakup.StartDate,
LeaveBreakup.EndDate,
LeaveApplication.ReasonForLeave,
LeaveApplication.ProcessorComment,
Team.Name,
LeaveTypeDetail.Name
FROM LeaveApplication
INNER JOIN Employee ON LeaveApplication.Employee=Employee.Id
INNER JOIN Team ON Employee.Team=Team.Id
INNER JOIN LeaveBreakup ON LeaveApplication.Id=LeaveBreakup.LeaveApplication
INNER JOIN LeaveTypeDetail ON LeaveBreakup.LeaveType=LeaveTypeDetail.LeaveType
WHERE Employee.Team=5
AND LeaveStatus!=0
AND LeaveBreakup.StartDate BETWEEN '01-01-2016' AND '01-31-2016'
AND LeaveBreakup.WhichHalf=0
GROUP BY LeaveApplication.Id,
LeaveBreakup.StartDate,
LeaveBreakup.EndDate,
LeaveApplication.ReasonForLeave,
LeaveApplication.ProcessorComment,
Team.Name,
LeaveTypeDetail.Name
ORDER BY LeaveBreakup.StartDate

Try something like this.
SELECT sum(totalNoOfDays) as total,
a.Id,
a.StartDate,
a.EndDate,
a.ReasonForLeave,
a.ProcessorComment,
a.Name,
a.Name
from
select
(
select sum(DATEDIFF(DAY,LeaveBreakup.StartDate,LeaveBreakup.EndDate)+1) AS totalNoOfDays,
LeaveApplication.Id,
LeaveBreakup.StartDate,
LeaveBreakup.EndDate,
LeaveApplication.ReasonForLeave,
LeaveApplication.ProcessorComment,
Team.Name,
LeaveTypeDetail.Name
FROM LeaveApplication
INNER JOIN Employee ON LeaveApplication.Employee=Employee.Id
INNER JOIN Team ON Employee.Team=Team.Id
INNER JOIN LeaveBreakup ON LeaveApplication.Id=LeaveBreakup.LeaveApplication
INNER JOIN LeaveTypeDetail ON LeaveBreakup.LeaveType=LeaveTypeDetail.LeaveType
WHERE Employee.Team=5
AND LeaveStatus!=0
AND LeaveBreakup.StartDate BETWEEN '01-01-2016' AND '01-31-2016'
AND LeaveBreakup.WhichHalf=0
GROUP BY LeaveApplication.Id,
LeaveBreakup.StartDate,
LeaveBreakup.EndDate,
LeaveApplication.ReasonForLeave,
LeaveApplication.ProcessorComment,
Team.Name,
LeaveTypeDetail.Name
)a
group by
a.Id,
a.StartDate,
a.EndDate,
a.ReasonForLeave,
a.ProcessorComment,
a.Name,
a.Name
ORDER BY a.StartDate

Related

MSSQL Query framing

I have 3 tables like master and 2 child tables . I am using join condition but did not get as expected client needs.
Query:
select * from(
select a.id as mid,b.id,b.val from ##mastertable a right join ##table1 b
on a.id=b.id ) as c inner join ##table2 d on c.mid=d.id
Kindly provide any other way to get proper result.
Try:
Select a.id, b.id, b.val
From (mastertable a
Right Join table1 b
On a.id = b.id)
Inner Join table2 d
On a.id = d.id;
This should work :
select * from (
(select master.id from master_table) master
LEFT OUTER JOIN
(select table1.id, table1.value from table_1) table1
ON
master.id = table1.id
FULL OUTER JOIN
(select table2.id, table2.value from table_2) table2
ON
table1.id = table2.id
AND
table1.val = table2.val
)
;
Try below: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2b58625961c444997829abb45d40417b
select * from master m inner join
(select table2.id as id1, table2.value as val1, table1.value as val2, table1.id as id2
from table2 left join table1
on table1.id=table2.id and table1.value=table2.value)x on m.id=x.id1
This will be same as you expected
select
m.id, t1.id, t1.value, t2.id, t2.value
from
m
left join
t1
on
m.id = t1.id
left join
t2
on
t1.id = t2.id and t1.value = t2.value
order by m.id, t1.value
here M is your master table, T1 is table1 and t2 is table2

TSQL: SUM not working like i thought it would

I have the following query:
SELECT
a.Name,
ISNULL(CAST(sum((b.qty * b.unit_rate)* b.Eng_RPQ )/100 AS DECIMAL(8,1)),0) AS [EngHours],
SUM(BR.BlendedRate)
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Project p on p.id = c.project_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
INNER JOIN
(SELECT
a.Name, c.id,
CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2)) AS BlendedRate
FROM
Activity_Details b
INNER JOIN
Activity c on b.activity_id = c.id
INNER JOIN
Team f on f.activity_id = c.id
INNER JOIN
SOF_Details d on d.id = f.sof_detail_id
INNER JOIN
Project p on p.id = c.project_id
INNER JOIN
Rate r on r.projectid = p.id
INNER JOIN
Teammate_Type tt on tt.id = f.team_type_id
RIGHT OUTER JOIN
Discipline a on c.discipline_id = a.id
GROUP BY
a.Name, c.id, f.POH, d.HourlyRate, r.Discount) AS BR ON BR.id = c.id
GROUP BY
a.Name
ORDER BY
a.Name
Which yields:
Name EngHours BlendedRate
Architechtural 80.8 38.48
Architechtural 80.8 55.33
Architechtural 80.8 55.40
I want to SUM this BlendedRate and ROUND it but if i try SUM(BR.BlendedRate) to the SELECT and remove the BR.BlendedRate in the GROUP BY
I get:
Name EngHours BlendedRate
Architechtural 242.3 895.26
I was expecting BlendedRate to equal 149.21
Any idea what i am doing wrong?
unable to comment due to reputation. It is a crude solution, but your code is returning duplicated (seemingly 6) records. The code should be fixed elsewhere, but without sample data it is difficult. In the mean time a crude solution would be to add a distinct clause to the sum function
SUM( DISTINCT BR.BlendedRate)

group by clause is not working

SELECT
dm.DISTRICT_NAME ,
od.REGULAR_WORKERS_COUNT,
od.DAILY_OR_CASUAL_WORKERS_COUNT,
od.CONTRACT_WORKERS_COUNT,
od.TOTAL_COUNT
FROM
ORG_DETAILS od with (NOLOCK)
INNER JOIN
DISTRICT_MASTER dm with (NOLOCK) ON od.DISTRICT_ID = dm.DISTRICT_ID
GROUP BY
dm.district_name
I had this code, and I am looking to group my table with district_name. I am getting an error though.
Error message :
Column 'ORG_DETAILS.REGULAR_WORKERS_COUNT' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
You should try this:-
SELECT
dm.DISTRICT_NAME ,
COUNT(od.REGULAR_WORKERS_COUNT),
COUNT(od.DAILY_OR_CASUAL_WORKERS_COUNT),
COUNT(od.CONTRACT_WORKERS_COUNT),
COUNT(od.TOTAL_COUNT)
FROM
ORG_DETAILS od with (NOLOCK)
INNER JOIN
DISTRICT_MASTER dm with (NOLOCK) ON od.DISTRICT_ID = dm.DISTRICT_ID
GROUP BY
dm.district_name
SELECT
dm.DISTRICT_NAME ,
sum(od.REGULAR_WORKERS_COUNT),
sum(od.DAILY_OR_CASUAL_WORKERS_COUNT),
sum(od.CONTRACT_WORKERS_COUNT),
sum(od.TOTAL_COUNT)
FROM
ORG_DETAILS od with (NOLOCK)
INNER JOIN
DISTRICT_MASTER dm with (NOLOCK) ON od.DISTRICT_ID = dm.DISTRICT_ID
GROUP BY
dm.district_name

SQL query to fetch repeat column values within time frame

I have an eCommerce website where I am getting lot of fraud orders.. I'd like to pull out those Order_No.
Here is my query
SELECT
O.Order_No, O.Customer_ID, O.DateOrdered, O.IPAddress,
C.FirstName, C.LastName, CD.nameoncard
FROM
Order_No O
INNER JOIN
CardData CD ON O.card_id = CD.id
INNER JOIN
Customers C ON O.customer_id = C.customer_id
ORDER BY
O.order_no desc
Here's the criteria I want to follow:
If the customer_id repeats more than once in 6hrs
If the IPAddress repeats more than once in 6hrs
If the Lastname is NOT found in Nameoncard
Can someone help please?
can you try this
WITH Tmp (Order_No, Customer_id, DateOrdered, IPAddress, FirstName, LastName, NameOnCard)
AS
(
SELECT Ord.Order_No, Ord.Customer_Id, Ord.DateOrdered, Ord.IPAddress,
Cust.FirstName, Cust.LastName, CustData.NameOnCard
FROM Order_No Ord
INNER JOIN Customers Cust
ON
Cust.Customer_Id = Ord.Customer_Id
INNER JOIN
CardData CustData
ON CustData.Id = Ord.Card_Id
)
SELECT DISTINCT a.*
FROM Tmp a
INNER JOIN Tmp b
ON a.Order_No <> b.Order_No
AND a.Customer_Id = b.Customer_Id
WHERE DATEDIFF(hour, a.DateOrdered, b.DateOrdered) >= 6
UNION
SELECT DISTINCT c.*
FROM Tmp c
INNER JOIN Tmp d
ON c.Order_No <> d.Order_No
AND c.IPAddress = d.IPAddress
WHERE DATEDIFF(hour, c.DateOrdered, d.DateOrdered) >= 6
UNION
SELECT DISTINCT e.*
FROM Tmp e
WHERE ISNULL(e.NameOnCard,'') = ''
here is the query:
select * from
(
select b.order_no,b.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
left join order_no as b on a.customer_id=b.customer_id
inner join carddata as cd on b.customer_id=cd.customer_id
INNER JOIN Customers C ON b.customer_id = C.customer_id
where a.order_no < b.order_no
and datediff(hour,a.dateordered,b.dateordered) between 0 and 6
union
select b.order_no,b.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
left join order_no as b on a.IPAddress=b.IPAddress
inner join carddata as cd on b.customer_id=cd.customer_id
INNER JOIN Customers C ON b.customer_id = C.customer_id
where a.order_no < b.order_no
and datediff(hour,a.dateordered,b.dateordered) between 0 and 6
union
select a.order_no,a.dateordered,a.customer_id, C.FirstName, C.LastName, cd.nameoncard from order_no as a
inner join carddata as cd on a.customer_id=cd.customer_id
INNER JOIN Customers C ON a.customer_id = C.customer_id
where charindex(C.LastName,cd.nameoncard) = 0
) as abc

Select only columns from joined tables from CTE

The following is my CTE:
;WITH CTE AS
(SELECT O.*, E.Num, E.Amount
FROM OData O
INNER JOIN Equip E
ON O.Name = E.Name)
SELECT * FROM CTE -- gives results I want to join to
The following is the query that I want to SELECT from (and only use this SELECT statement for my query results:
SELECT
MU.Type
,MU.Num
,MU.MTBUR
,MF.MTBF
,MU.Hours
,MF.Hours
FROM
MUType_Stage MU
INNER JOIN
MFType_Stage MF
ON
MU.Type = MF.Type
AND
MU.Num = MF.Num
-- Need do JOIN to CTE right here
INNER JOIN
Status_STAGE S
ON
MU.Nu = S.Part
LEFT OUTER JOIN
RCN N
ON
N.Name = R.Part
LEFT OUTER JOIN
Repair RR
ON
R.ACSS_Name = RR.Name
So basically I need to JOIN to the CTE inside the SELECT query in which I want the results.
OR ALTERNATIVELY Uses this select statement to join to the CTE but only what the selected columns from the second select statement
Try this syntax
WITH CTE
AS (SELECT O.*,
E.Num,
E.Amount
FROM OData O
INNER JOIN Equip E
ON O.Name = E.Name)
SELECT MU.Type,
MU.Num,
MU.MTBUR,
MF.MTBF,
MU.Hours,
MF.Hours
FROM MUType_Stage MU
INNER JOIN MFByACType_Stage MF
ON MU.Type = MF.Type
AND MU.Num = MF.Num
INNER JOIN CTE C --- JOIN HERE as like other tables
ON C.Num = MF.Num
INNER JOIN Status_STAGE S
ON MU.Nu = S.Part
LEFT OUTER JOIN RCN N
ON N.Name = R.Part
LEFT OUTER JOIN Repair RR
ON R.ACSS_Name = RR.Name

Resources