Collecting values from the upper rows [duplicate] - sql

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 8 years ago.
I have a table in sql server and I want to summarize a cell with upper rows in T-Sql like this:
Quantity Total
-----------------------------
1 1
5 6
12 18
20 38
I use SQL server 2008, How can I achieve this?
Regards, Majid.

You are looking for a cumulative sum, it would appear. If you are using SQL Server 2012 or later, just do:
select quantity, sum(quantity) over (order by quantity)
from table t;
Note the order by quantity. You need to specify the ordering for the cumulative sum, typically doing another sum.
In earlier versions, you can do this using a correlated subquery:
select quantity,
(select sum(t2.quantity)
from table t2
where t2.quantity <= t.quantity
) as total
from table t;

Related

Oracle sql rownum between how to shrink the query [duplicate]

This question already has answers here:
How do I limit the number of rows returned by an Oracle query after ordering?
(14 answers)
How ROWNUM works in pagination query?
(3 answers)
Oracle SELECT TOP 10 records [duplicate]
(6 answers)
Oracle "LIMIT n,m" equivalent [duplicate]
(1 answer)
Closed 2 years ago.
I would like to get records from 25 to 50. I write this code, but, it looks terrible with double select clause.
Select * From (
Select eto.*, rownum rn from employee_trip_orders eto
) where rn between 25 and 50 ;
How can i shrink it to use one select like that?
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
I don't need the second one. Thanks. I have old 11c Oracle version and offset keyword is not suitting me
How can i shrink it to use one select like that?
Since you are on Oracle 11g you cannot. You must use subquery inline to achieve your desired output.
Select eto.*, eto.rownum rn from employee_trip_orders eto
where rn between 25 and 50 ;
That query will never return a row. ROWNUM value is incremented only after it is assigned. Please see How ROWNUM works in pagination query.
From Oracle 12c onwards, you could use the new Top-n Row limiting feature.
You are not using order by clause so what is the meaning of the rownum? In the end, You are only fetching random 26 (25-50 inclusive) records.
So You can achieve the desired result using the following code:
Select eto.*, rownum rn
from employee_trip_orders eto
where rownum<= 26 ;
Cheers!!

How to pick up median value in between 3 records? - SQL [duplicate]

This question already has answers here:
Function to Calculate Median in SQL Server
(37 answers)
Closed 4 years ago.
Please assist me, I need to pick 790 since its in middle but not able to figure it out.
id Value
12 780
123 796
124 790
Thank you in advance
This should help to get what you want. Get the average value from your table, then get first record with smallest difference with average value.
SELECT TOP 1 id, value FROM yourTable
ORDER BY ABS(value - (SELECT AVG(value) FROM yourTable))
or using variables, depending on the size of your table
DECLARE #median DECIMAL
SELECT #median = AVG(value) FROM yourTable
SELECT TOP 1 * FROM yourTable
ORDER BY ABS( value - #median)

How do I average the last 6 months of sales within SQL based on period AND year?

How do I average the last 6 months of sales within SQL?
Here are my tables and fields:
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR,
And I need to average these fields
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
The hard part I'm having is understanding how to average the last whole 6 months, ie. fsicalcalperiod 2-6(inside fiscalcalyear 2017).
I'm hoping for some help on what the SQL command text should look like since I'm very new to manipulating SQL outside of the UI.
Sample Data
My Existing SQL String:
SELECT IM_ItemWhseHistoryByPeriod.ITEMCODE,
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR
FROM MAS_AME.dbo.IM_ItemWhseHistoryByPeriod
IM_ItemWhseHistoryByPeriod
ScaisEdge Attempt #1
if fiscalyear and fiscalperiod are number you could use
select avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
or for each item code
select itemcode, avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
group by itemcode
Try the following solution and see if it works for you:
select avg(DOLLARSSOLD) as AvgDollarSod,
avg(QUANTITYSOLD) as AvgQtySold
from IM_ItemWhseHistoryByPeriod
where FISCALCALYEAR = '2017
and FISCALCALPERIOD between 2 and 6

ASP Classic SQL query to get cumulative ammounts per day [duplicate]

This question already has answers here:
How to get cumulative sum
(16 answers)
Closed 6 years ago.
I am struggling with a SQL query I have a collection process and I want to have a query that show me every day how much we have collected so far during the current month in a cumulative way. I have all the information in one table named Procesado the date field is Process Date and the amount is in lcamnt Field.
So far I have this:
Query:
SELECT
Procesado.ProcessDate, SUM(Procesado.lcamnt) AS Monto
FROM
Procesado
WHERE
Procesado.ProcessDate >= Procesado.ProcessDate
GROUP BY
Procesado.ProcessDate
Table value
Table Name: Procesado
ProcessDate lcamnt
05/26/2016 $1000
05/26/2016 $500
05/27/2016 $2000
05/27/2016 $1000
05/28/2016 $5000
Desired output
05/26/2016 $1500
05/27/2016 $4500
05/28/2016 $9500
select p.ProcessDate,
(select sum(lcamnt)
from procesado p2
where
p2.ProcessDate <= p.ProcessDate
) as Monto
from procesado p
group by p.processDate

SQL Select SUM if ID is the same [duplicate]

This question already has answers here:
SQL Server : SUM() of multiple rows including where clauses
(6 answers)
Closed 8 years ago.
I know this is probably a simple question since I'm still learning, but I am trying to find a SQL Select statement that will calculate the SUM of each row's pieces and weight IF their secondary IDs are the same. So the select is collapsing and getting the SUM of rows with the same secondary ID, while also returning the unique rows as well.
ID_a ID_b pieces weight
--------------------------
1 1 10 20
2 1 20 30
3 2 40 40
Will result in
ID_b: 1 pieces: 30 weight: 50
ID_b: 2 pieces: 40 weight: 40
Thank you.
Group by the id_b and then you can use aggregate functions to sum up the values for each group
select id_b, sum(pieces), sum(weight)
from your_table
group by id_b
select id_b,sum(pieces),sum(weight)
from table
group by id_b
Here is the query you're looking for:
SELECT TID_b
,SUM(pieces) AS [totalPieces]
,SUM(weight) AS [totalWeight]
FROM yourTable T
GROUP BY T.ID_b
Hope this will help you.