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.
Related
This question already has answers here:
Sum of data in varchar column
(4 answers)
Closed 3 years ago.
I am having an issue. I have looked for some help online but none of them showed accurate result. I will really appreciate if anyone can help me in this regard.
Here is the scenario:
I have a db_table 'A' where I have to merge the rows that is duplicate also I have to add another column that is in Character datatype
it is something like this.
Table A
ID |SCORE(character)
-------------------
90 |10000
93 |00005
94 |02000
90 |02000
94 |00005
The output will be:
ID |SCORE(character)
-------------
90 |12000
93 |00005
94 |02005
You need to use SUM and GROUP BY. Try like:
SELECT id,sum( convert (int,score)) from myTable group by ID
It's simply
SELECT ID,
SUM(ISNULL(TRY_CAST(Score AS INT), 0))
FROM YourTable
GROUP BY ID;
Try the following:
select ID, REPLICATE('0',5-LEN(SUM(CAST(SCORE as int))))+ CAST(SUM(CAST(SCORE as int)) as varchar) as Score
from Table
group by ID
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)
It is a mystery to me with this text book example. We have simply:
Transaction_ID (primary key), Client_ID, Transaction_Amount, Month
1 1 500 1
2 1 1000 1
3 1 10 2
4 2 11 2
5 3 300 2
6 3 10 2
... ... ... ...
I want to calculate in SQL the mean(Transaction_Amount), std(Transaction_Amount) and the some percentile(Transaction amount) grouped by Client_ID. But is seems, even given that percentile is a very similar calculation than the standard deviation, SQL cannot do it with a simple statement as:
SELECT
mean(Transaction_Amount),
std(Transaction_Amount),
percentile(Transaction_Amount)
FROM
myTable
GROUP BY
Client_ID, Month
Or can it?
It gets worse becuase I also need to Group By Month in addition to Client_ID.
Thanks a lot!
Sven
I'm sure Oracle can do the calculations you want. I just don't know what they are. You specify that you want something grouped by ClientId. Yet, your sample query has two keys in the GROUP BY.
Some functions that you want to look at are:
AVG()
STDDEV()
PERCENT_RANK()
Without sample data and desired results (or a very clear explanation of what you are trying to calculate), I can't put together a query.
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;
I have a question regarding Oracle SQL.
My data looks like this:
id year
-- ----
1 2000
1 2001
1 2002
1 2003
1 2006
1 2000
2 2001
2 2002
2 2003
3 2003
3 2005
4 2012
4 2013
I want the id's which have the years 2001, 2002, 2003.
My result set:
id
--
1
2
Please help me with this. I actually tried searching this, but couldn't figure a way to search about my particular problem.
SQL
SELECT t.id
FROM TABLE t
WHERE t.year in(2001,2002,2003)
GROUP BY t.id
Sample SqlFiddle
http://sqlfiddle.com/#!2/4ec9f/2/0
Explanation
You want to filter your data set to only show rows with certain years, so that is what you put in the where clause WHERE t.year in(2001,2002,2003).
Since a single id can be in multiple years, your result set would contain duplicates. To remove the duplicates you could GROUP BY the ID or use the DISTINCT statement to only show unique elements.
UPDATE
Based on comments, here's a version that will only display id's that have all three years. We use DISTINCT t.YEAR to avoid counting id's that perhaps would have a single year repeated multiple times. The HAVING COUNT(DISTINCT t.YEAR) = 3 part ensures that we only include id's that have all three years.
SELECT t.id
FROM years t
WHERE t.year in(2001,2002,2003)
GROUP BY t.id
HAVING COUNT(DISTINCT t.YEAR) = 3
Updated sqlFiddle, which includes a data set where id of 3 has two rows for 2003 to show off the logic that only counts unique years for an ID.
select distinct id
from table
where year in(2001,2002,2003)