How to sum column of result in sql server - sql

I'm facing a problem, I want to sum the column of my results, this my code:
SELECT
[study_service]
,DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared]) as [Time-ARRIVED-PREPARED]
,DATEDIFF(minute,[study_date_arrivee],[study_date_start]) as [Time-ARRIVED-START]
,DATEDIFF(minute,[study_date_start],[study_date_dossier_pret]) as [Time-START-DOSSPRET]
,DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat]) as [Time-DOSSPRET-DOSSREMIS]
FROM
[VisionTST].[dbo].[PremierTrimestre]
I want the final result like this:
Total of `[Time-ARRIVED-PREPARED]`,`[Time-ARRIVED-Start]`,...

SELECT
[study_service]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_patient_prepared])) as [Time-ARRIVED-PREPARED]
,sum(DATEDIFF(minute,[study_date_arrivee],[study_date_start])) as [Time-ARRIVED-START]
,sum(DATEDIFF(minute,[study_date_start],[study_date_dossier_pret])) as [Time-START-DOSSPRET]
,sum(DATEDIFF(minute,[study_date_dossier_pret],[study_date_remise_resultat])) as [Time-DOSSPRET- DOSSREMIS]
FROM [VisionTST].[dbo].[PremierTrimestre]

Related

Group By with Case Statements SQL Server

I would like to group by with the case statements in SQL Server. I created cases that's my revenue range. I'd like to calculate the number of sales orders within all these revenue ranges(cases)
Below please find my queries:
Select
sum(OrderQuantity) as Orders,
case when (sum(SalesAmount-TaxAmt-Freight)<100 and sum(SalesAmount-TaxAmt-Freight)>=0) then '$0-$100'
when (sum(SalesAmount-TaxAmt-Freight)>=100 and sum(SalesAmount-TaxAmt-Freight)<500) then '$100-$500'
when (sum(SalesAmount-TaxAmt-Freight)>=500 and sum(SalesAmount-TaxAmt-Freight)<1000) then '$500-$1000'
when (sum(SalesAmount-TaxAmt-Freight)>=1000 and sum(SalesAmount-TaxAmt-Freight)<2500) then '$1000-$2500'
when (sum(SalesAmount-TaxAmt-Freight)>=2500 and sum(SalesAmount-TaxAmt-Freight)<5000) then '$2500-$5000'
when (sum(SalesAmount-TaxAmt-Freight)>=5000 and sum(SalesAmount-TaxAmt-Freight)<10000) then '$5000-$10000'
when (sum(SalesAmount-TaxAmt-Freight)>=10000 and sum(SalesAmount-TaxAmt-Freight)<50000) then '$10000-$50000'
when (sum(SalesAmount-TaxAmt-Freight)>=50000 and sum(SalesAmount-TaxAmt-Freight)<100000) then '$50000-$100000'
when (sum(SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
end as SalesAmountCategory
From
dbo.FactResellerSales
group by
SalesAmountCategory;
I expect to get the result like:
result example
I keep getting errors when i try to group by based on the case statements. The error is "Invalid column name 'SalesAmountCategory'". How can i do that? Many thanks in advance!
It looks like you want to know number of orders in each category
So, you need first to map each order to category and then group by it, like that:
select SalesAmountCategory, count(*) from
(
Select case
when ((SalesAmount-TaxAmt-Freight)>=100000) then '>$100000'
when ((SalesAmount-TaxAmt-Freight)>=50000) then '$50000-$100000'
when ((SalesAmount-TaxAmt-Freight)>=10000) then '$10000-$50000'
when ((SalesAmount-TaxAmt-Freight)>=5000) then '$5000-$10000'
when ((SalesAmount-TaxAmt-Freight)>=2500) then '$2500-$5000'
when ((SalesAmount-TaxAmt-Freight)>=1000) then '$1000-$2500'
when ((SalesAmount-TaxAmt-Freight)>=500) then '$500-$1000'
when ((SalesAmount-TaxAmt-Freight)>=100) then '$100-$500'
when ((SalesAmount-TaxAmt-Freight)<100) then '$0-$100'
end as SalesAmountCategory
From dbo.FactResellerSales
) as t
group by SalesAmountCategory

Sum is not getting after grouping also in SQL Server

I have a query like this.
select
TC.F_Exhibition_Code, TC.F_Exhibition,
c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson,
c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate,
TC.F_Paymentmethod, TC.F_Currency,
TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt,
sum(TC.F_Totalamt) as sum
from
T_CreditInvoice TC
left join
T_Customer c on c.F_Customer_Code = tc.F_Customer_Code
where
TC.F_CreditInvoiceNo = 'INV100098'
group by
TC.F_Exhibition_Code, TC.F_Exhibition, c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson, c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate, TC.F_Paymentmethod,
TC.F_Currency, TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt
I trying to get sum of my F_Totllamt column, but I am not getting it.
What is wrong with my query?
You can use window function and write as:
sum(TC.F_Totalamt) OVER (PARTITION BY TC.F_CreditInvoiceNo ORDER BY (SELECT 1))as sum
in existing query.. hope it helps!!
Demo
You're grouping on the variable you want to get the sum of. Hence you get the sum of every single "TC.F_Totalamt" because you're also grouping over that same variable.
Just remove "TC.F_Totalamt" from the "group by" statement and it will work.

Where Multiple Values SQL SERVER

For this code I need the sum of the serv1 - serv8 where the student is receiving "Speech Language Therapy". I can not for the life of me figure out how to get those. If I say where serv1 equals 'SLT' then it will show up as null for serv 2. If I do it for serv2 it will show up as another service like DD for serv1.
My sum isn't working I would like to sum all of the services up by school but I'll take what anyone is willing to help me with. Any help is appreciated
SELECT SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name AS [[[School Name]]]]]]],
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SUM (SPE_Student_Profile_VW.mins1),
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
FROM DBO.SPE_Student_Profile_VW SPE_Student_Profile_VW
WHERE ( SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47')
GROUP BY SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name,
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SPE_Student_Profile_VW.mins1,
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
the column you are trying to sum should not be in the group by clause, all the others should.
What I mean is the SELECT clause has this:
SUM (SPE_Student_Profile_VW.mins1),
and the GROUP BY has this part which should not be there:
SPE_Student_Profile_VW.mins1,
because it is the column he is trying to sum and you can't group by a column that you are aggregating with a function and that is why SUM isn't working
not sure of what is required but from what I get, you can probably get what you need if you add another
OR
in your
WHERE
Something like
WHERE (SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
(SPE_Student_Profile_VW.serv2 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
SPE_Student_Profile_VW.serv3 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47'
and so on.
Thanks.
In Group by clause use all columns which you are used in select statement.Except the column you are calculating sum in select statement.
Thanks.

SQL transform two records into two columns

I don´t know if the title is correct but basicaly I need this(result of query):
r_e_s_id person_id
89074 161704
89074 161703
89095 161708
89095 161707
68651 129884
68651 129883
81512 161074
81512 161073
To be inserted/updated(in case that there will be r_e_s_id values in table) into table COMM_PROP which has cols - r_e_s_id, perid1, perid2...like this:
r_e_s_id perid1 perid2
89074 161704 161703
89095 161708 161707
68651 129884 129883
81512 161074 161073
How can I do this? I´m using Oracle 11g.
Thank you for any help!!!
Considering only 2 values i.e. 1 max and 1 min per r_e_s_id, Try this:-
SELECT r_e_s_id, MAX(person_id) AS perid1, MIN(person_id) AS perid2
FROM YOUR_TABLE
GROUP BY r_e_s_id;

Access: Simplify sql-query: multiple IIFs

I got a table with a date-column and a column like this:
Column
01-00-00-00
01-02-00-00
03-06-00-00
05-13-00-00
....
What I want to do: Find out how many entries start with "01","02","03",... for every day.
Therefore I need to first substract the first 2 characters, count them and group by the date.
I have a code that works fine, but needs really long. Do you have an idea for a more simple query?
SELECT
Date,
sum(IIF(Platz='03',1,0)) as Gang_03,
sum(IIF(Platz='04',1,0)) as Gang_04,
sum(IIF(Platz='05',1,0)) as Gang_05,
sum(IIF(Platz='06',1,0)) as Gang_06,
sum(IIF(Platz='07',1,0)) as Gang_07,
sum(IIF(Platz='08',1,0)) as Gang_08,
sum(IIF(Platz='09',1,0)) as Gang_09,
sum(IIF(Platz='10',1,0)) as Gang_10,
sum(IIF(Platz='11',1,0)) as Gang_11,
sum(IIF(Platz='12',1,0)) as Gang_12,
sum(IIF(Platz='13',1,0)) as Gang_13,
sum(IIF(Platz='14',1,0)) as Gang_14,
sum(IIF(Platz='15',1,0)) as Gang_15,
sum(IIF(Platz='16',1,0)) as Gang_16,
sum(IIF(Platz='17',1,0)) as Gang_17,
sum(IIF(Platz='18',1,0)) as Gang_18,
sum(IIF(Platz='19',1,0)) as Gang_19,
sum(IIF(Platz='20',1,0)) as Gang_20
FROM
(
SELECT
time_neu as Date,
LEFT(Platz_von,2) as Platz
FROM
00_Gesamt_Pickauf
)
GROUP BY
Date