SQL Server - Matrix of multiple WHERE queries - sql

If I have a table like below:
Source Event Date Qty
Site A Create Account 5/05/2018 6
Site B Create Account 4/05/2018 12
Site A Update Account 6/05/2018 1
Site A Update Notes 7/05/2018 2
Site B Add Dependant 5/05/2018 1
Site C Create Account 5/05/2018 14
And another like this:
Date OrdersRec
4/05/2018 162
5/05/2018 123
6/05/2018 45
7/05/2018 143
And want to build a sort of matrix table to be able to use again and again for various date ranged queries, like this:
Date Create Update UpdateNotes AddDependant OrdersRec
4/05/2018 12 0 0 0 162
5/05/2018 20 0 0 1 123
6/05/2018 0 1 0 0 45
7/05/2018 0 0 2 0 143
What would be the best way of doing so?
I started with an INSERT into for the dates and the CreateAccount numbers, but I'm not sure how to go back and update the other counts for particular dates, or even if thats stupid and I'm missing something obvious.
Thanks!

Just use union all or join. There is no reason to create a new table for this:
select date, sum(v_create) as v_create, sum(v_update) as v_update,
sum(v_updatenotes) as v_updatenotes, sum(v_adddependent) as v_adddependent,
sum(orderrec) as orderec
from ((select t1.date, v.*, 0 as OrdersRec
from table1 t1 cross apply
(values ( (case when event = 'Create Account' then quantity else 0 end),
(case when event = 'Update Account' then quantity else 0 end),
(case when event = 'Update Notes' then quantity else 0 end),
(case when event = 'Add Dependent' then quantity else 0 end)
)
) v(v_create, v_update, v_updatenotes, v_adddependent)
) union all
(select date, 0, 0, 0, 0, orderrec
from table2
)
) t
group by date
order by date;
This handles a lot of edge cases, such as when dates are missing from one or the other tables.

Why don't you create a variable table having all the dates, and use it as your main table.
Then you would so something like this
select m.date,
sum(case when event = 'Create Account' then 1 else 0) as create,
sum(case when event = 'Update Account' then 1 else 0) as updates,
sum(o.ordersrec) as orders
from #main as m
inner join #orders as o on o.date = m.date
inner join #events as e on e.date = m.date
group by m.date

Related

Sum a column and perform more calculations on the result? [duplicate]

This question already has an answer here:
How to use an Alias in a Calculation for Another Field
(1 answer)
Closed 3 years ago.
In my query below I am counting occurrences in a table based on the Status column. I also want to perform calculations based on the counts I am returning. For example, let's say I want to add 100 to the Snoozed value... how do I do this? Below is what I thought would do it:
SELECT
pu.ID Id, pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
Snoozed + 100 AS Test
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
I get this error:
Invalid column name 'Snoozed'.
How can I take the value of the previous SUM statement, add 100 to it, and return it as another column? What I was aiming for is an additional column labeled Test that has the Snooze count + 100.
You can't use one column to create another column in the same way that you are attempting. You have 2 options:
Do the full calculation (as #forpas has mentioned in the comments above)
Use a temp table or table variable to store the data, this way you can get the first 5 columns, and then you can add the last column or you can select from the temp table and do the last column calculations from there.
You can not use an alias as a column reference in the same query. The correct script is:
SELECT
pu.ID Id, pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+100 AS Snoozed
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
MSSQL does not allow you to reference fields (or aliases) in the SELECT statement from within the same SELECT statement.
To work around this:
Use a CTE. Define the columns you want to select from in the CTE, and then select from them outside the CTE.
;WITH OurCte AS (
SELECT
5 + 5 - 3 AS OurInitialValue
)
SELECT
OurInitialValue / 2 AS OurFinalValue
FROM OurCte
Use a temp table. This is very similar in functionality to using a CTE, however, it does have different performance implications.
SELECT
5 + 5 - 3 AS OurInitialValue
INTO #OurTempTable
SELECT
OurInitialValue / 2 AS OurFinalValue
FROM #OurTempTable
Use a subquery. This tends to be more difficult to read than the above. I'm not certain what the advantage is to this - maybe someone in the comments can enlighten me.
SELECT
5 + 5 - 3 AS OurInitialValue
FROM (
SELECT
OurInitialValue / 2 AS OurFinalValue
) OurSubquery
Embed your calculations. opinion warning This is really sloppy, and not a great approach as you end up having to duplicate code, and can easily throw columns out-of-sync if you update the calculation in one location and not the other.
SELECT
5 + 5 - 3 AS OurInitialValue
, (5 + 5 - 3) / 2 AS OurFinalValue
You can't use a column alias in the same select. The column alias do not precedence / sequence; they are all created after the eval of the select result, just before group by and order by.
You must repeat code :
SELECT
pu.ID Id,pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END)+ 100 AS Test
FROM
Prospects p
INNER JOIN
ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE
p.Store = '108'
GROUP BY
pu.Name, pu.Id
ORDER BY
Name
If you don't want to repeat the code, use a subquery
SELECT
ID, Name, LeadCount, Working, Uninterested,Converted, Snoozed, Snoozed +100 AS test
FROM
(SELECT
pu.ID Id,pu.Name Name,
COUNT(*) LeadCount,
SUM(CASE WHEN Status = 'Working' THEN 1 ELSE 0 END) AS Working,
SUM(CASE WHEN Status = 'Uninterested' THEN 1 ELSE 0 END) AS Uninterested,
SUM(CASE WHEN Status = 'Converted' THEN 1 ELSE 0 END) AS Converted,
SUM(CASE WHEN SnoozedId > 0 THEN 1 ELSE 0 END) AS Snoozed
FROM Prospects p
INNER JOIN ProspectsUsers pu on p.OwnerId = pu.SalesForceId
WHERE p.Store = '108'
GROUP BY pu.Name, pu.Id) t
ORDER BY Name
or a view

SQL Server error simplify code due to multiple joins

I need an alternative way to simplify this code.
This code computes the age of the balances of the client depending on the repayment schedule of loan. The filters are 1-7 days, 8-30, 31-60 .... and so on until it reach 181 and above
with membersWithLoans as -- gets members with loan
(
select
a.memberid, a.loanid, a.loanamt, a.intamt
from
loanmst a
where
loandt <= '12/19/2016'
and status = 'O'
)
,selectPaymentToDate as -- gets payments of the members to date
(
select
b.loanid, sum(a.princollamt) as princollamt1,
sum(a.intcollamt) as intcollamt1
from
collectiondtl a
inner join
membersWithLoans b on a.loanid = b.loanid
where
a.accdate <= '12/19/2016'
group by
b.loanid)
,selectBalanceToDate as -- gets the balance of member to date
(
select
b.loanid,
sum(a.princeamt) as prinBalanceToDate,
sum(a.instamt) as intBalanceToDate,
sum(a.insamt) as insuBalanceToDate
from
loandtl a
inner join
membersWithLoans b on a.loanid = b.loanid
where
a.duedt <= '12/19/2016'
group by
b.loanid)
, combineBalanceWithpayment as -- combine payment and balance
(
select a.loanid,a.loanamt, a.intamt,
(case
when b.prinBalanceToDate is null then 0
else b.prinBalanceToDate end) as prinBalanceToDate2,
(case
when b.intBalanceToDate is null then 0
else b.intBalanceToDate end) as intBalanceToDate2,
(case
when b.insuBalanceToDate is null then 0
else b.insuBalanceToDate end) as insuBalanceToDate2,
(case
when c.princollamt1 is null then 0
else c.princollamt1 end) as PrincipalCollectiontoDate,
(case
when c.intcollamt1 is null then 0
else c.intcollamt1 end) as IntCollectiontoDate,
cast(((case
when b.prinBalanceToDate is null then 0
else b.prinBalanceToDate
end)
-
(case
when c.princollamt1 is null then 0
else c.princollamt1 end))as decimal(10,2)) as Arrears
from
membersWithLoans a
left join selectBalanceToDate b
on a.loanid=b.loanid
left join selectPaymentToDate c
on a.loanid=c.loanid
)
,filterNegativeArrears as
(
select *
from
combineBalanceWithpayment
where Arrears > 0
)
the code above gets the member information
,select1To7days as -- this code gets amount to be paid in a specific schedule
(
select b.loanid,
sum((case
when a.princeamt is null then 0
else a.princeamt end))as prin7Daysbalance
from loandtl a
inner join membersWithLoans b
on a.loanid=b.loanid
where
a.duedt > DATEADD(day,-7,'12/19/2016')
and
a.duedt<='12/19/2016'
group by b.loanid
)
,select8to30days as -- this code gets amount to be paid in a specific schedule
(
select b.loanid,
sum((case
when a.princeamt is null then 0
else a.princeamt end))as prin8To30Daysbalance
from loandtl a
inner join membersWithLoans b
on a.loanid=b.loanid
where
a.duedt<=DATEADD(day,-7,'12/19/2016')
and a.duedt > DATEADD(day,-30,'12/19/2016')
group by b.loanid
)
-- and so on ..... the filters for schedule is compose of 31 to 60days, 61 to 90 days,
--121 to 180 days, 181 and above. there is no pattern since it the requirement on days may change
, computePar1To7days as -- computes the 1 to 7 days
(
select a.loanid, cast((a.arrears - a.Par1To7days) as decimal(10,2)) as deductedArrears, a.Par1To7days
from
(
select a.loanid,a.arrears,
cast((case
when a.arrears >= b.prin7Daysbalance then b.prin7Daysbalance -- if the arrears is greater than the 7days balance to be collected then it will be show
else a.arrears end)as decimal(10,2))as Par1To7days -- else the remaining is the arrears
from
filterNegativeArrears a
left join select1To7days b
on a.loanid=b.loanid
) a
where cast((a.arrears - a.Par1To7days) as decimal(10,2)) > 0
)
,computePar8To30days as -- computes the 8 to 30 days
(
select a.loanid, cast((a.arrears - a.Par8To30days)as decimal(10,2)) as deductedArrears, a.Par8To30days
from
(
select a.loanid, a.deductedArrears as arrears,
cast((case
when (a.deductedArrears) > 0
then
(case
when (a.deductedArrears)>= b.prin8To30Daysbalance
then b.prin8To30Daysbalance
else (a.deductedArrears)
end)
else 0 end)as decimal(10,2))as Par8To30days
from computePar1To7days a
left join select8To30days b
on a.loanid=b.loanid
) a
where cast((a.arrears - a.Par8To30days) as decimal(10,2)) > 0
)
-- so on until all par is computed. 31 to 60 days, 61 to 90 days,
--121 to 180 days, 181 and above. there is no pattern since it the requirement on days may change
the code above gets the sum of data from specific schedules like
1 to 7 days, 8-30 days, 31 to 60 days, 61 to 90 days, 121 to 180 days, 181 and above
select a.*,
b.Par1To7days,
c.Par8To30days,
d.Par31To60days,
e.Par61To90days,
f.Par91To120days,
g.Par121To180days --,
--h.Par181AndAbovedays
from
filterNegativeArrears a
left join computePar1To7days b
on a.loanid=b.loanid
left join computePar8To30days c
on a.loanid=c.loanid
left join computePar31To60days d
on a.loanid=d.loanid
left join computePar61To90days e
on a.loanid=e.loanid
left join computePar91To120days f
on a.loanid=f.loanid
left join computePar121To180days g
on a.loanid=g.loanid
--left join computePar181AndAbovedays h
-- on a.loanid=h.loanid
the code above joins the computed age
The code is working fine and calculating fine
but when I add more join in the selection I get an error
left join computePar181AndAbovedays h
on a.loanid=h.loanid
The problem is I started encountering the error:
An expression services limit has been reached. Please look for
potentially complex expressions in your query, and try to simplify
them.
I still need more table to join with my query.
Can you suggest ways to simplify this query is highly appreciated
You are obviously running into a limit there and budging that limit will not be possible with the query you have. Follow the advice given in the error message: simplify.
How? Well you have a multitude of CTE's defined. Another way of writing your query is by materializing the CTE's in temporary tables before the actual query and then using the temporary tables instead.
For example, this CTE:
membersWithLoans as -- gets members with loan
(
select a.memberid, a.loanid,a.loanamt,a.intamt
from loanmst a
where loandt<='12/19/2016'
and status = 'O'
)
Can be be materialized to a temporary table:
select a.memberid, a.loanid,a.loanamt,a.intamt
into #membersWithLoans
from loanmst a
where loandt<='12/19/2016'
and status = 'O'
This will create a temporary table #membersWithLoans that can be used in further temporary table creations or in your final query.
To illustrate further, suppose you have materialized all your CTE's to temporary tables, you would have no more WITH clause. You would then finally use the temporary tables in your final SELECT query:
-- create all temporary tables (one of them being #filterNegativeArrears)
select
a.*,
-- the rest of your selections
from
#filterNegativeArrears a
-- the rest of the joined temporary tables
-- the rest of your query (WHERE, ORDER BY etc)

IF else Stored procedure confusion

This is a table
name a b c normal loan status
abc 50 60 70 normal
bcd 50 50 50 loan
what i want first is to get total of values of columns a,b and c in accordance with their status.. meaning; total amount of name with status normal should come under normal column,and of status loan should come under loan column..
i should get abc's total 50+60+70 in normal
and bcd's total 50+50+50 in loan
how do i do that ??
i tried if else in SP , but cant seem to be getting it
I think the below is what you need. The below query is a standard ANSI query:
SELECT name
,SUM(CASE WHEN status = 'normal' THEN (a + b + c) ELSE 0 END) AS normal
,SUM(CASE WHEN status = 'loan' THEN (a + b + c) ELSE 0 END) AS loan
,status
FROM yourTable
GROUP BY name, status
OUTPUT:
name normal loan status
bcd 0 150 loan
abc 180 0 normal
As per your requirement in the comment to update the existing rows:
UPDATE yourTable
SET normal = CASE WHEN status = 'normal' THEN (a + b + c) ELSE 0 END,
loan = CASE WHEN status = 'loan' THEN (a + b + c) ELSE 0 END
FROM yourTable
Please note that you need to update the table every time you insert new record or to change the original INSERT statement to include those columns as well.
In single select you can make it.
Try
SELECT *,(CASE WHEN [status]='normal' THEN [a]+[b]+[c] ELSE null END) as normal,(CASE WHEN [status]='loan' THEN [a]+[b]+[c] ELSE null END) as loan FROM YourTable

Converting a cursor/while loop into a set based approach

I am very new to SQL and I am trying to update a stored procedure that has a cursor in it. I had never seen a cursor prior to this one. The cursor's select statement has an inner join, but returns only a single column of IDs. The cursor calculates the number of deleted accounts for every ID, on a row by row basis.
At the end of the stored procedure, the number of deletion variables are inserted into a table
I was hoping someone that understands more about cursors/while loops would be able to suggest the best way to convert the code above into an efficient set based approach.
This is a set based way:
;WITH IDS AS
(
SELECT DISTINCT c.p_id
FROM dbo.deletion_h dh
INNER JOIN dbo.Child c
ON dh.C_id = c.c_id
WHERE CONVERT(CHAR(25),dh.delete_date,101) = #ReportDate
AND c.isT = 1
AND c.p_id NOT IN (SELECT p_id FROM dbo.Parent WHERE support = 'Y')
), Data AS
(
SELECT p_id,
COUNT(*) ActiveChild,
SUM(CASE WHEN isT = 1 AND [level] <> 'H' THEN 1 ELSE 0 END) activePk8,
SUM(CASE WHEN isT = 1 AND [level] = 'H' THEN 1 ELSE 0 END) activeHS
FROM dbo.child c
WHERE [login] <> 'f'
AND EXISTS( SELECT 1 FROM IDS
WHERE p_id = c.p_id)
GROUP BY p_id
)
SELECT SUM(CASE WHEN ActiveChild > 0 THEN 1 ELSE 0 END) NumParentDeletions,
SUM(CASE WHEN activechildPk8 > 0 THEN 1 ELSE 0 END) NumDeletionsPk8,
SUM(CASE WHEN activeHS > 0 THEN 1 ELSE 0 END) NumDeletionsHS
FROM Data
You can modify the last SELECT to make it insert those values into your table.

SQL Count with multiple conditions then join

Quick one,
I have a table, with the following structure
id lid taken
1 1 0
1 1 0
1 1 1
1 1 1
1 2 1
Pretty simply so far right?
I need to query the taken/available from the lid of 1, which should return
taken available
2 2
I know I can simply do two counts and join them, but is there a more proficient way of doing this rather than two separate queries?
I was looking at the following type of format, but I can not for the life of me get it executed in SQL...
SELECT
COUNT(case taken=1) AS taken,
COUNT(case taken=0) AS available FROM table
WHERE
lid=1
Thank you SO much.
You can do this:
SELECT taken, COUNT(*) AS count
FROM table
WHERE lid = 1
GROUP BY taken
This will return two rows:
taken count
0 2
1 2
Each count corresponds to how many times that particular taken value was seen.
Your query is correct just needs juggling a bit:
SELECT
SUM(case taken WHEN 1 THEN 1 ELSE 0 END) AS taken,
SUM(case taken WHEN 1 THEN 0 ELSE 1 END) AS available FROM table
WHERE
lid=1
Alternatively you could do:
SELECT
SUM(taken) AS taken,
COUNT(id) - SUM(taken) AS available
FROM table
WHERE
lid=1
SELECT
SUM(case WHEN taken=1 THEN 1 ELSE 0 END) AS taken,
SUM(case WHEN taken=0 THEN 1 ELSE 0 END) AS available
FROM table
WHERE lid=1
Weird application of CTE's:
WITH lid AS (
SELECT DISTINCT lid FROM taken
)
, tak AS (
SELECT lid,taken , COUNT(*) AS cnt
FROM taken t0
GROUP BY lid,taken
)
SELECT l.lid
, COALESCE(a0.cnt, 0) AS available
, COALESCE(a1.cnt, 0) AS taken
FROM lid l
LEFT JOIN tak a0 ON a0.lid=l.lid AND a0.taken = 0
LEFT JOIN tak a1 ON a1.lid=l.lid AND a1.taken = 1
WHERE l.lid=1
;