Sum of single column for different conditions - sql

I have two columns in my table colum#1 is varchar(MAX) and column#2 is int.
In column#2 I have negative and positive entries I want sum of positive entries in one column and sum of negative entries in other column in my select query
To achieve this I did
SELECT SUM(atbl.M),SUM(atbl.p)
from (select M=case when column#2<0
then column#2 else 0 end,
P=case when column#2 > 0
then column#2 else 0 end
from testTable) atbl
or
select SUM(case when column#2<0 then column#2 else 0 end) as M
,SUM(case when column#2 > 0 then column#2 else 0 end) as P
from testTable
Is there any better way to achieve this.

If you do it frequently on that column then use a view:
create view testView as
select
sum(case when column#2 < 0 then column#2 else 0 end) as M,
sum(case when column#2 > 0 then column#2 else 0 end) as P
from testTable

Related

How to add columns from another table using a SELECT

I have a table named Action, which I want to add new columns to it.
The columns that I want to add are the result of this query :
SELECT DATE_AFFECTATION ,
SUM(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END) AS TotalCloturé,
SUM(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END) AS TotalEncours,
SUM(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END) AS TotalGelé,
SUM(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END) AS TotalHorsdélai,
SUM(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END) AS TotalNouveau,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalCloturé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalEncours,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalGelé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalHorsdélai,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalNouveau
FROM DIM_Ticket
GROUP BY DATE_AFFECTATION
ORDER BY DATE_AFFECTATION
Is this possible?
This is too long for a comment.
If you want to add columns to an existing table, then the syntax is alter table. For instance:
alter table add column TotalCloturé int;
The default values are NULL or you can provide a constant default (such as 0 in this case).
You don't add columns with a query.
Hence, your question is quite unclear. Are you simply trying to assign values to existing columns? Do you just want a result set with these column values in addition to the columns in the original table? Do you need triggers (or some other mechanism) so the values remain synchronized?
Your query return more than one column.
You can't add a column and fill it in a single Query.

Update a complex SQL query to add a new column with the sum of two columns

The below SQL query creates a table with n number of columns named in the next line.
...., curr_amount, tax_amount, ....
I am having a very tough time updating the below query to create a new column called total and position it exactly after tax_amount column and the total column should contain the values that are obtained after sum of curr_amount & tax_amount.
I have been working on this from more than one day but couldn't figure it out.
P.S. Still a noob here. Thanks alot for your time.
.
SELECT Isnull(t.total_month, 'Total') total_month,
t.tax_amount,
t.curr_amount,
t.usage_qty,
t.kh_qty,
t.bill_cnt
FROM (SELECT dbo.Sigmadf(bm.posted_date, 'YYYY-MM') total_month,
Sum(CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END) usage_qty,
Sum(CASE
WHEN bm.curr_amount IS NULL THEN 0
ELSE bm.curr_amount
END) curr_amount,
Sum(CASE
WHEN bm.adj_amount IS NULL THEN 0
ELSE bm.adj_amount
END) adj_amount,
Sum(CASE
WHEN bm.bal_fwd_amount IS NULL THEN 0
ELSE bm.bal_fwd_amount
END) bal_forward,
Sum(CASE
WHEN bm.tax_amount IS NULL THEN 0
ELSE bm.tax_amount
END) tax_amount,
Sum(CASE
WHEN bm.due_amount IS NULL THEN 0
ELSE bm.due_amount
END) due_amount,
Sum(CASE
WHEN bm.last_total_paid_amount IS NULL THEN 0
ELSE bm.last_total_paid_amount * -1
END) paid_amount,
Sum(CASE
WHEN bm.bill_print = 'Y' THEN 1
ELSE 0
END) pdf_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '0' THEN 1
ELSE 0
END) reg_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '1' THEN 1
ELSE 0
END) ftime_cnt,
Sum(CASE
WHEN Isnull(bm.bill_handling_code, '0') = '9999' THEN 1
ELSE 0
END) ltime_cnt,
Count(*) bill_cnt,
Sum(CASE
WHEN bill_status = '01' THEN 1
ELSE 0
END) canc_cnt,
Sum(CASE
WHEN bill_status = '01' THEN
CASE
WHEN rr.usage_qty IS NULL THEN 0
ELSE Cast (rr.usage_qty AS NUMERIC(18, 2))
END
ELSE 0
END) canc_usg,
Sum(CASE
WHEN vis.kh_qty IS NULL THEN 0
ELSE Cast(vis.kh_qty AS NUMERIC(18, 2))
END) kh_qty
FROM bill_master bm WITH (nolock)
INNER JOIN (SELECT bill_no,
Sum(CASE
WHEN vpb.recurr_charge_type IN ( 'T4',
'SLF' )
THEN
CASE
WHEN vpb.print_qty = 'Y'
AND vpb.usage_qty IS NOT NULL
THEN
Cast (vpb.usage_qty AS
NUMERIC(18, 2))
ELSE 0
END
ELSE 0
END) usage_qty
FROM v_print_bills_all vpb
GROUP BY bill_no) rr
ON rr.bill_no = bm.bill_no
LEFT OUTER JOIN vis_bill_master_cr vis WITH (nolock)
ON bm.bill_no = vis.bill_no
WHERE 1 = 1
AND dbo.Trunc(bm.posted_date) >= '20150101'
AND dbo.Trunc(bm.posted_date) <= '20151124'
AND bm.posted_date IS NOT NULL
AND bm.cust_id NOT IN (SELECT cc.code_type cust_id
FROM code_table cc WITH (nolock)
WHERE cc.code_tabname = 'RptExclCust'
AND cc.code_value = 'cust_id')
GROUP BY dbo.Sigmadf(bm.posted_date, 'YYYY-MM') WITH rollup)t
I must say that the explanation is not so clear.
From my understanding, you want the total of two columns.
So, wrap all your query between parenthesis, call it subQuery, and make the sum of the two columns on top:
SELECT subQuery.total_month as bill_date,
subQuery.curr_amount as amount,
subQuery.tax_amount tax,
subQuery.curr_amount + subQuery.tax_amount as [total],
...
FROM
(..your entire query here..) as subQuery

SQL percentage with rows same table with different where condition

I want to do a query like:
select
count(asterisk) where acción='a'/count(asterisk) where acción='b' * 100
from
same_table
grouped by day
but I don't want use subquery, is it possible with joins?
I`m not sure the syntax is correct, but you can use something like this:
SELECT day,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A,
SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) AS SUM_B,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A / SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) * 100 AS result
FROM your_table
GROUP BY day
The concept is to actually sum the the values that you need, instead of count.

Choosing one value from partially duplicate rows

I am trying to choose between two rows of data in order to get a total count of type.
Table - Evaluations
EvaluationID (link to minEval_ID and max_EvalID)
EstablishedDelays
Table - Outcome
min_EvalID
max_EvalID
EligTypeRecalc
This is what my current query is:
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) Then 1 Else 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 Then 1 Else 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=1 Then 1 Else 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays=2 Then 1 Else 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN a.EligTypeRecalc=2 and b.EstablishedDelays>=3 Then 1 Else 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 Then 1 Else 0 END) 'Clinical Judgement'
from Outcome a
join Evaluations b
on a.max_EvalID=b.evaluationid and a.min_evalID=b.evaluationID
where a.EligTypeRecalc<>3
The problem I'm encountering is picking the correct Eval_ID to choose the correct number of delays and not count the other. The EstablishedDelays associated with max_EvalID is correct unless the EligTypeRecalc is 0, then it should count the delays associated with min_EvalID.
So far I've come up with this basic logic but I'm having a mind block on how to get it to the next step:
CASE WHEN EligTypeRecalc=max_EvalID
THEN EstablishedDelays=max_EvalID
ELSE EstablishedDelays=min_EvalID
Bonus points: I only have Read access to the database.
**What's the proper query syntax to use to select the row associated and exclude the other?
Thanks for your advice!
You might try wrapping your summary of the Evaluations table into a CTE and then join that to the Outcome table.
Here is the code I used, its a little long:
WITH min_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.min_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
),
max_eval AS
(
SELECT
a.DataMatch,
a.max_EvalID,
a.EligTypeRecalc,
b.evaluationid,
b.EstablishedDelays
FROM Outcomes a
JOIN Evaluations b
ON a.max_EvalID=b.evaluationid
WHERE a.EligTypeRecalc<>3
)
SELECT "NewEligType"=
COUNT (*),
SUM (CASE WHEN a.EligTypeRecalc IN (1,4,5,7) THEN 1 ELSE 0 END) 'Established Condition',
SUM (CASE WHEN a.EligTypeRecalc=6 THEN 1 ELSE 0 END) 'Established Delay & At-Risk',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=1) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=1) THEN 1 ELSE 0 END) 'Established Delay only: One Delay',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays=2) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays=2) THEN 1 ELSE 0 END) 'Established Delay only: Two Delays',
SUM (CASE WHEN (a.EligTypeRecalc=2 AND a.EstablishedDelays>=3) OR (a.EligTypeRecalc=2 AND a.EstablishedDelays=0 AND b.EligTypeRecalc=2 AND b.EstablishedDelays>=3) THEN 1 ELSE 0 END) 'Established Delay only: Three+ Delays',
SUM (CASE WHEN a.EligTypeRecalc=8 THEN 1 ELSE 0 END) 'Clinical Judgement'
FROM max_eval a
JOIN min_eval b
ON a.DataMatch=b.DataMatch

select sum statement

I created this select statement I will convert to a view. I need help with this. I need to be able to add the total of Minority that = Yes and No show total on report pages.
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor
You nearly have it:
...
SUM (case ps.Minority when 'Yes' then 1 else 0 end) AS MinorityYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) AS MinorityNo,
COUNT(*) AS Total,
...
With the Total I'm assuming that every row should be counted. This is what you want if:
The only values that exist in the column are 'Yes' and 'No' or
Values different from 'Yes' and 'No' should also be counted in the total.
You're forcing us to guess what you want. You have a count of the people who said that they were in a minority; do you want a count of the people who said No? Or do you want a count of the number who said 'either "Yes" or "No"' and excluding those who gave 'decline to say' or simply no answer at all?
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotalYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) MinorityTotalNo,
SUM (case ps.Minority when 'Yes' then 1 when 'No' then 1 else 0 end)
AS StatedMinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor