Using an array in conditional summation with grouping - sql

I have the sql query thats work fine
SELECT
CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN (L.Network = 0 OR L.Network = 1) THEN L.Profit ELSE 0 END),
SUM(CASE WHEN (L.Network != 0 AND L.Network != 1) THEN L.Profit ELSE 0 END)
FROM
[dbo].[Leads] L WITH (NOLOCK)
LEFT JOIN [dbo].[Transactions] S WITH (NOLOCK) ON L.TransactionId = S.Id
GROUP BY
CAST(L.CreationUtcDateTime AS DATE);
I need to make it more generic by using variables instead of hard-coded constants.
I changed the query to:
DECLARE #matchNetworks TABLE (id int)
INSERT #matchNetworks(id) VALUES (0),(1)
SELECT
CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN (L.Network in (SELECT ID from #matchNetworks)) THEN L.Profit ELSE 0 END),
SUM(CASE WHEN (L.Network not in (SELECT ID from #matchNetworks)) THEN L.Profit ELSE 0 END)
FROM
[dbo].[Leads] L WITH (NOLOCK)
LEFT JOIN [dbo].[Transactions] S WITH (NOLOCK) ON L.TransactionId = S.Id
GROUP BY
CAST(L.CreationUtcDateTime AS DATE);
And now i have an error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
How can I use the predefined array of network ids in my query to avoid an error?

Move the comparison to the FROM clause using a LEFT JOIN:
SELECT CAST(L.CreationUtcDateTime AS DATE),
SUM(L.Profit),
SUM(CASE WHEN mn.ID IS NOT NULL THEN L.Profit ELSE 0 END),
SUM(CASE WHEN mn.ID IS NULL THEN L.Profit ELSE 0 END)
FROM [dbo].[Leads] L LEFT JOIN
[dbo].[Transactions] S
ON L.TransactionId = S.Id LEFT JOIN
#matchNetworks mn
ON mn.ID = l.Network
GROUP BY CAST(L.CreationUtcDateTime AS DATE);

Related

using Query one results to run Query two

SELECT distinct
Pd.Cpd as ' accountnumber'
FROM [RQL_ALK_PMT].[Cts_opps] pd
INNER JOIN [RQL_ALK_PMT].[Cts_opps].dpo.cnms_id metg on metg.cnms_id=me.cnms_id
This code would result with this
Result
accountnumber
1332132
3213123
5641202
6412221
1233242
What I would like to do is when the code above gets the results my bottom code reads them and runs them trough its code. The common denominator here would be the account number because its running through a different table
SELECT
pm.AcctNumb as 'accountnumber'
, SUM(CASE WHEN pm.cusidIN ('cr') THEN 1 ELSE 0 END) AS CA
, SUM(CASE WHEN pm.cusidIN ('gb') THEN 1 ELSE 0 END) AS GB
, SUM(CASE WHEN pm.cusidIN ('tev','offev','Lastev') THEN 1 ELSE 0 END) AS chr
, SUM(CASE WHEN pm.cusidIN
('pm','pr','che' )
THEN 1 ELSE 0 END) AS Act
, SUM(CASE WHEN pm.cusidIN ('supev','tev') THEN 1 ELSE 0 END) AS Fulfillment
FROM ops.medadata pm WITH (NOLOCK)
INNER JOIN mw.pim_acct Ma with (nolock) ON ma.AcctNumb= pm.AcctNumb
Where pm.AcctNumb in ()
GROUP BY
pm.AcctNumb
I have tried doing this the code below but it doesnt seem to work
With counta as (
SELECT distinct
Pd.Cpd as ' accountnumber'
FROM [RQL_ALK_PMT].[Cts_opps] pd
INNER JOIN [RQL_ALK_PMT].[Cts_opps].dpo.cnms_id metg on metg.cnms_id=me.cnms_id
)
SELECT
pm.AcctNumb as 'accountnumber'
, SUM(CASE WHEN pm.cusidIN ('cr') THEN 1 ELSE 0 END) AS CA
, SUM(CASE WHEN pm.cusidIN ('gb') THEN 1 ELSE 0 END) AS GB
, SUM(CASE WHEN pm.cusidIN ('tev','offev','Lastev') THEN 1 ELSE 0 END) AS chr
, SUM(CASE WHEN pm.cusidIN
('pm','pr','che' )
THEN 1 ELSE 0 END) AS Act
, SUM(CASE WHEN pm.cusidIN ('supev','tev') THEN 1 ELSE 0 END) AS Fulfillment
FROM ops.medadata pm WITH (NOLOCK)
INNER JOIN mw.pim_acct Ma with (nolock) ON ma.AcctNumb= pm.AcctNumb
left join counta on Pm.accountnumber = counta.accountnumber
Where pm.AcctNumb in (counta.accountnumber)
GROUP BY
pm.AcctNumb
**im having issue with joining the two tables together**
IN is not an equivalent to a join - but you are treating that way.
Instead think of "IN this LIST" and the list could be supplied by you, or by a subquery e.g.
a list given by the query itself
select * from atable
where acol IN ('a','b','c') -- i.e. the list is hardcoded
or, using a subquery
SELECT ...etc.
WHERE pm.AcctNumb IN (
SELECT Pd.Cpd
FROM [RQL_ALK_PMT].[Cts_opps] pd
INNER JOIN [RQL_ALK_PMT].[Cts_opps].dpo.cnms_id metg on metg.cnms_id=me.cnms_id
)
or, if using a CTE
With counta as (
SELECT Pd.Cpd
FROM [RQL_ALK_PMT].[Cts_opps] pd
INNER JOIN [RQL_ALK_PMT].[Cts_opps].dpo.cnms_id metg on metg.cnms_id=me.cnms_id
)
SELECT ... etc.
WHERE pm.AcctNumb IN (
SELECT Cpd
FROM counta
)
note, it usually is not more efficient to use select distinct when forming a subquery to be used with an IN list.

sql join not taking all records from another table

I have a query like this
WITH CTE AS
(
SELECT
U.Name, U.Adluserid AS 'Empid',
MIN(CASE WHEN IOType = 0 THEN Edatetime END) AS 'IN',
MAX(CASE WHEN IOType = 1 THEN Edatetime END) AS 'out',
(CASE
WHEN MAX(E.Status) = 1 THEN 'AL'
WHEN MAX(E.Status) = 2 THEN 'SL'
ELSE 'L'
END) AS leave_status
FROM
Mx_ACSEventTrn
RIGHT JOIN
Mx_UserMst U ON Mx_ACSEventTrn.UsrRefcode = U.UserID
LEFT JOIN
Tbl_Zeo_Empstatus E ON Mx_ACSEventTrn.UsrRefcode = E.Emp_Id
WHERE
CAST(Edatetime AS DATE) BETWEEN '2019-11-03' AND '2019-11-03'
GROUP BY
U.Name, U.Adluserid
)
SELECT
[Name], [Empid], [IN], [OUT],
(CASE
WHEN CAST([IN] AS TIME) IS NULL THEN CAST(leave_status AS NVARCHAR(50))
WHEN CAST([IN] AS TIME) < CAST('08:15' AS TIME) THEN 'P'
ELSE 'L'
END) AS status
FROM
CTE
In my employee master table Mx_UserMst I have 67 employees. But here it is showing only a few employees those who are punched. I want to show all employees from employee master
I believe that the problem is his WHERE clause:
where cast(Edatetime as date) between '2019-11-03' and '2019-11-03'
Why not cast(Edatetime as date) = '2019-11-03'?
I'm not sure in which table the column Edatetime belongs (you should qualify all the columns with the correct table name/alias).
You must move the condition to an ON clause:
WITH CTE AS
(
select U.Name,U.Adluserid as 'Empid',
min(case when IOType=0 then Edatetime end) as 'IN',
max(case when IOType=1 then Edatetime end) as 'out',
case max(E.Status) when 1 then 'AL' when 2 then 'SL' else 'L' end as leave_status
from Mx_UserMst U
left join Mx_ACSEventTrn on Mx_ACSEventTrn.UsrRefcode=U.UserID and (cast(Edatetime as date) between '2019-11-03' and '2019-11-03')
left join Tbl_Zeo_Empstatus E on Mx_ACSEventTrn.UsrRefcode=E.Emp_Id
group by U.Name,U.Adluserid
)
SELECT [Name], [Empid],[IN],[OUT],
case
when cast([IN] as time) is null then cast(leave_status as nvarchar(50))
when cast([IN] as time) < cast('08:15' as time) then 'P'
else 'L'
end as status
FROM CTE
If Edatetime belongs to Tbl_Zeo_Empstatus move the condition to the next join's ON clause.
I also changed the RIGHT to a LEFT join so to make the statement more readable.
If you want to keep everything in a particular table, then that should be the first table in the FROM clause. Subsequent joins should be LEFT JOINs and conditions on subsequent tables should be in the ON clause rather than the WHERE clause.
I would also advise you to use table aliases and to only use single quotes for string and date constants -- NOT column aliases.
The following assumes that IOType and Edatetime are in the table Mx_ACSEventTrn. I should not have to guess. You should qualify all column names in the query.
WITH CTE AS (
SELECT U.Name, U.Adluserid AS Empid,
MIN(CASE WHEN AE.IOType = 0 THEN AE.Edatetime END) AS in_dt,
MAX(CASE WHEN AE.IOType = 1 THEN AE.Edatetime END) AS out_dt,
(CASE WHEN MAX(ES.Status) = 1 THEN 'AL'
WHEN MAX(ES.Status) = 2 THEN 'SL'
ELSE 'L'
END) AS leave_status
FROM Mx_UserMst U LEFT JOIN
Mx_ACSEventTrn AE
ON AE.UsrRefcode = U.UserID AND
CAST(AE.Edatetime AS DATE) BETWEEN '2019-11-03' AND '2019-11-03' LEFT JOIN
Tbl_Zeo_Empstatus ES
ON AE.UsrRefcode = ES.Emp_Id AND
GROUP BY U.Name, U.Adluserid
)
SELECT Name, Empid, IN_DT, OUT_DT,
(CASE WHEN IN_DT IS NULL THEN leave_status
WHEN CAST(IN_DT AS TIME) < CAST('08:15' AS TIME) THEN 'P'
ELSE 'L'
END) AS status
FROM CTE;
Some more points:
Don't name aliases things like IN that are already key words. That is why I gave it the name IN_DT.
There is no reason to cast to a TIME to compare to NULL.
I don't see a reason to cast to NVARCHAR(50) in the outer CASE expression.

Sub select SQL query using where and groupby

When I try below code in SQL Server,
SELECT
dbo.Category.CatNo as Category,
dbo.Category.Categaory as Name,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2016-07-17' AND '2016-07-23')) AS ActualSales,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2015-07-19' AND '2015-07-25')) AS LastYrVariance,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2016-01-01' AND '2016-09-05')) AS YrToDateActual,
(select SUM(dbo.SALES.SELLINC) where (dbo.SALES.DATE BETWEEN '2015-01-01' AND '2015-09-05')) AS LastYrToDateActual
FROM dbo.Category INNER JOIN
dbo.Dissection ON dbo.Category.CatNo = dbo.Dissection.CatNo INNER JOIN
dbo.Division ON dbo.Dissection.DivNo = dbo.Division.ID INNER JOIN
dbo.Departments ON dbo.Dissection.DeptNo = dbo.Departments.DeptID INNER JOIN
dbo.SALES ON dbo.Dissection.DissNo = dbo.SALES.CODE
WHERE (dbo.SALES.BRN = 1)
GROUP BY dbo.Category.CatNo, dbo.Category.Categaory
ORDER BY dbo.Category.CatNo
I get below error message,
Msg 8120, Level 16, State 1, Line 2
Column 'dbo.Category.CatNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY
clause.
Any help please?
The subqueries have a select clause, with a Sum() function which attempts to Sum the values from a column of a table that is not mentioned in the Subquery. (The subqueries have no FROM clause). These subqueries should not be subqueries, they just need to be Sum() expressions.
And you should check into using aliases to simplify your SQL scripts.
SELECT c.CatNo Category, c.Categaory Name,
sum(case when s.Date between '2016-07-17' and '2016-07-23'
then s.SELLINC else 0 end) ActualSales,
sum(case when s.Date between '2015-07-25' and '2015-07-19'
then s.SELLINC else 0 end) LastYrVariance,
sum(case when s.Date between '2016-01-01' and '2016-09-05'
then s.SELLINC else 0 end) YrToDateActual,
sum(case when s.Date between '2015-01-01' AND '2015-09-05'
then s.SELLINC else 0 end) LastYrToDateActual
FROM dbo.Category c
join dbo.Dissection d on d.CatNo = c.CatNo
join dbo.Division v on v.Id = d.DivNo
join dbo.Departments p on p.DeptID = d.DeptNo
join dbo.SALES s on s.code = d.DissNo
WHERE s.BRN = 1
GROUP BY c.CatNo, c.Categaory
ORDER BY c.CatNo
to generate date range dynamically, as requested in comment, use datediff and dateAdd functions: I'm not sure what you meant by previous week (31 week 2016) or Last year same week (31 week 2015), so if this is not right, you can modify to correct.
SELECT c.CatNo Category, c.Categaory Name,
sum(case when s.Date between
dateAdd(week, datediff(week, 0, getdate())-52, 0)
and dateAdd(week, datediff(week, 0, getdate()), 0)
then s.SELLINC else 0 end) ActualSales,
sum(case when s.Date between '2015-07-25' and '2015-07-19'
then s.SELLINC else 0 end) LastYrVariance,
sum(case when s.Date between '2016-01-01' and '2016-09-05'
then s.SELLINC else 0 end) YrToDateActual,
sum(case when s.Date between '2015-01-01' AND '2015-09-05'
then s.SELLINC else 0 end) LastYrToDateActual
FROM dbo.Category c
join dbo.Dissection d on d.CatNo = c.CatNo
join dbo.Division v on v.Id = d.DivNo
join dbo.Departments p on p.DeptID = d.DeptNo
join dbo.SALES s on s.code = d.DissNo
WHERE s.BRN = 1
GROUP BY c.CatNo, c.Categaory
ORDER BY c.CatNo
I cant find a reason to generate that error in your query. Between you can simply those sub-queries by using CASE statements
SELECT dbo.category.catno AS Category,
dbo.category.categaory AS NAME,
Sum(CASE WHEN dbo.sales.date BETWEEN '2016-07-17' AND '2016-07-23' THEN dbo.sales.sellinc END) AS ActualSales,
Sum(CASE WHEN dbo.sales.date BETWEEN '2015-07-19' AND '2015-07-25' THEN dbo.sales.sellinc END) AS LastYrVariance,
Sum(CASE WHEN dbo.sales.date BETWEEN '2016-01-01' AND '2016-09-05' THEN dbo.sales.sellinc END) AS YrToDateActual,
Sum(CASE WHEN dbo.sales.date BETWEEN '2015-01-01' AND '2015-09-05' THEN dbo.sales.sellinc END) AS LastYrToDateActual
FROM dbo.category
INNER JOIN dbo.dissection
ON dbo.category.catno = dbo.dissection.catno
INNER JOIN dbo.division
ON dbo.dissection.divno = dbo.division.id
INNER JOIN dbo.departments
ON dbo.dissection.deptno = dbo.departments.deptid
INNER JOIN dbo.sales
ON dbo.dissection.dissno = dbo.sales.code
WHERE ( dbo.sales.brn = 1 )
GROUP BY dbo.category.catno,
dbo.category.categaory
ORDER BY dbo.category.catno
can you try with the below query.
SELECT Category.CatNo as Category, Category.Categaory as Name,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2016-07-17' AND '2016-07-23')) AS ActualSales,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2015-07-19' AND '2015-07-25')) AS LastYrVariance,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2016-01-01' AND '2016-09-05')) AS YrToDateActual,
(select SUM(SALES.SELLINC) where (SALES.DATE BETWEEN '2015-01-01' AND '2015-09-05')) AS LastYrToDateActual
FROM dbo.Category INNER JOIN
dbo.Dissection ON Category.CatNo = Dissection.CatNo INNER JOIN
dbo.Division ON Dissection.DivNo = Division.ID INNER JOIN
dbo.Departments ON Dissection.DeptNo = Departments.DeptID INNER JOIN
dbo.SALES ON Dissection.DissNo = SALES.CODE
WHERE (SALES.BRN = 1)
GROUP BY Category.CatNo, Category.Categaory
ORDER BY Category.CatNo

SQL Server Converting Rows to Columns

I am currently extracting data using 3 different tables, and below is the output.
Current Result:
Query Used:
SELECT
dbo.TableB.TrackingID, dbo.TableA.FinancialID,
dbo.TableA.ParcelCode, dbo.TableA.TotalAmount,
dbo.TableB.FinanceType, dbo.TableB.TransactionType,
dbo.TableC.CustID
FROM
dbo.TableA
INNER JOIN
dbo.TableB ON dbo.TableA.FinancialID = dbo.TableB.FinancialID
INNER JOIN
dbo.TableC ON dbo.TableB.TrackingID = dbo.TableC.TrackingID
WHERE
(dbo.TableB.TrackingID = '17006218AU')
I would like to have the following output:
Desired Output:
You can get the output you desire with grouping and some CASE statements inside SUM aggregate functions:
SELECT
dbo.TableB.TrackingID,
dbo.TableA.ParcelCode,
dbo.TableC.CustID,
SUM(CASE WHEN dbo.TableB.FinanceType = 'Invoice' THEN dbo.TableA.TotalAmount ELSE 0 END) AS TotalAmount,
SUM(CASE WHEN dbo.TableB.FinanceType = 'Invoice' AND TransType='Card' THEN dbo.TableA.TotalAmount ELSE 0 END) AS CardInvoice,
SUM(CASE WHEN dbo.TableB.FinanceType = 'Invoice' AND TransType='Cash' THEN dbo.TableA.TotalAmount ELSE 0 END) AS CashInvoice,
SUM(CASE WHEN dbo.TableB.FinanceType = 'PaymentRecepit' THEN dbo.TableA.TotalAmount ELSE 0 END) AS CardPaymentRecepit
FROM dbo.TableA
INNER JOIN dbo.TableB ON dbo.TableA.FinancialID = dbo.TableB.FinancialID
INNER JOIN dbo.TableC ON dbo.TableB.TrackingID = dbo.TableC.TrackingID
WHERE
dbo.TableB.TrackingID = '17006218AU'
GROUP BY
dbo.TableB.TrackingID,
dbo.TableA.ParcelCode,
dbo.TableC.CustID

Left Join is not working

I wrote following query
SELECT
us.Id as Id, us.Name as Name,
SUM(CASE WHEN c.isPublish = 0 THEN 1 ELSE 0 END) AS PendingCoupons,
SUM(CASE WHEN c.isPublish = 1 and convert(date,c.PublishedDate,101) >= convert(date, GETDATE(), 101) THEN 1 ELSE 0 END) AS ApprovedCouponsToday,
SUM(CASE WHEN c.isPublish = 0 and convert(date,c. CreateDate, 101) = convert(date, GETDATE(), 101) THEN 1 ELSE 0 END) AS PendingCouponsToday,
SUM(CASE WHEN c.isPublish = 1 THEN 1 ELSE 0 END) AS ApprovedCoupons,
SUM(CASE WHEN c.isPublish = 1 and c.Userid = us.Id and convert(date, c.PublishedDate, 101) >= convert(date, GETDATE(), 101) THEN 1 ELSE 0 END) AS ApprovedByUserToday,
SUM(CASE WHEN c.isPublish = 1 and c.Userid = us.Id THEN 1 ELSE 0 END) AS ApprovedByUser,
SUM(CASE WHEN c.ReviewVerify = 1 and convert(date, c.PublishedDate, 101) >= convert(date, GETDATE(), 101) THEN 1 ELSE 0 END) AS ProcessToday,
COUNT(*) AS Total
FROM
Users AS us
LEFT JOIN
Coupon c ON Userid = us.Id
GROUP BY
us.Name , us.Id
and I have following two tables
and after running above query the result is always this
Is there any error in this query , because its always returning me count " 0 " and I have almost 100 coupons on every user but its not showing
It would seem that since RIGHT JOIN works and gives correct answers (and probably a bogus user id) while a LEFT JOIN doesn't give any results related to coupons at all, the coupons are registered on a non existing user.
LEFT JOIN demands that data exists in the left part of the table that possibly exists in the right side, while RIGHT JOIN does the reverse. In other words, data exists in your rightmost table (coupon) that doesn't have any connection to the left side (user)
I think you may need to use a RIGHT JOIN to get all records form the Coupon table.
You not using the correct join.
You want to get coupon for a user id?
Use this join.
FROM
Users AS us
LEFT JOIN
Coupon c ON us.Id = c.Userid
if this does not work then use:
FROM
Users AS us
LEFT OUTER JOIN
Coupon c ON us.Id = c.Userid