SUM of paired columns in SQL - sql-server

Iam having TableInput
A B AB ID H I
1 1 1-1 3 1 2.2 //CASE1
1 1 1-1 3 2 3.4 //CASE1
1 4 1-4 3 1 2.2
1 4 1-4 3 4 4.2
1 4 1-4 3 4 3.2
1 5 1-5 3 4 1.2
1 5 1-5 3 4 3.2
1 6 1-6 3 4 5.2
Here consider CASE1 where AB column is having value of 1-1,finding the same pair 1-1 in other rows and must add columns values in H .
There fore my result table must be like..
A B AB SUM(H) SUM(I)
1 1 1-1 3 5.6
1 4 1-4 9 9.6
1 5 1-5 8 2.4
1 6 1-6 4 5.2
Not getting idea of how to query for my output table..
Can any one please help

This is called a GROUP BY:
SELECT A, B, AB,
SUM(H) AS SumH,
SUM(I) AS SumI
FROM dbo.TableName
GROUP BY A, B, AB
You have to apply aggregate functions on columns that you want to select that are not part of the GROUP BY.

Select A,B,AB,
SUM(H) as [SUM(H)],
SUM(I) as [SUM(I)]
FROM Table1
GROUP BY A, B, AB

SELECT MAX(A), MAX(B), AB, MAX(ID), SUM(H), SUM(I)
FROM Table1
GROUP BY AB
Result:

Related

Repeat a sum as a column in SQL

I have a table
WO# Seg# Part# QTY TotSale FlatSale
1 1 1 5 35 159
1 1 2 2 100 159
1 2 3 3 50 50
I need to calculate the FlatSale/SUM(TotSale) for each Seq# & WO# but do not group Seg# into one row.
I need this
WO# Seg# Part# QTY TotSale FlatSale Calc
1 1 1 5 35 159 1.177
1 1 2 2 100 159 1.177
1 2 3 3 50 50 1
With my code I am able to only do the division on each individual line like this:
select *, FlatSale/TotSale as Calc from table
WO# Seg# Part# QTY TotSale FlatSale Calc
1 1 1 5 35 159 4.54
1 1 2 2 100 159 1.59
1 2 3 3 50 50 1
I wouldn't mind leaving my Calc column and adding another column if that's the easiest way to do it.
Maybe something like this:
SELECT
[WO#],[Seg#],[Part#],[QTY],[TotSale],[FlatSale]
((FlatSale*100)/(SUM([TotSale]) OVER(PARTITION BY [Seg#] ORDER BY [Seg#])))/100 AS Calc
FROM SomeTable
You can use a lateral join. Here's something quick (not tested as I don't have access to mssql at the moment):
select *, x.CalcPrice as Calc
from table t
outer apply (
select t.FlatSale/SUM(ix.TotSale) CalcPrice
from table ix where ix.[WO#] = t.[WO#]
and ix.[Seg#] = t.[Seg#]) x
Something like that.

TSQL Aggregating values based on other column

I have a table that looks as follows
Amount Factor Month Customer
1 1 2 A
3 1 2 A
4 -1 2 A
2 1 2 B
2 1 2 B
3 -1 2 B
4 1 3 A
5 1 3 A
6 -1 3 A
I want to aggregate (sum) the column Amount per Month and Customer. The Amounts should be multiplied with the value in the column Factor.
Hence, the result should look as follows (could be an UPDATE to the same table or a new table):
Amount Factor Month Customer
0 1 2 A
1 1 2 B
3 1 3 A
Try below
SELECT SUM(Amount * Factor) as Amount,Month,Customer
FROM tableName
GROUP BY Month,Customer
I think this is what you want:
select month, customer, sum(amount * factor) as sum_amount
from t
group by month, customer;
I'm not sure why you would want factor in the result set.

Top 9 Distinct records after using sub query for cumulative sum

I have SQL data table with following data structure
ID Hrly Hshed Dust_removal_to_done Dust_removal_done Update_datetime
2 ER MGS 4 4 2009-05-05
3 ER AQ 4 2 2009-05-05
4 SR ANDA 4 4 2009-05-05
5 ECR HOME 5 5 2009-05-05
6 NR GZB 5 5 2009-05-05
7 NR LDH 5 5 2009-05-05
8 NCR JHS 5 5 2009-05-05
9 NCR CNB 5 5 2009-06-05
10 SCR LGD 5 5 2009-06-05
11 SCR LGD 5 5 2009-05-05
the data is fed by users on daily basis.
Further I am using a stored procedure for cumulative sum of 'Dust_removal_done' as
ALTER PROCEDURE [dbo].[RAP_regular] as
SELECT Hshed, HRly, Dust_removal_to_done, Dust_removal_done, (SELECT SUM(Dust_removal_done) FROM TPHRAP_regular t2
where t2.Hshed = TPHRAP_regular.Hshed and t2.Update_datetime <= TPHRAP_regular.Update_datetime) as cumulative_dust_removal
FROM TPHRAP_regular
This stored procedure is giving me result as under
Hshed Hrly Dust_removal_to_done Dust_removal_done cumulative_dust_removal
MGS ER 4 4 4
AQ ER 4 2 2
ANDA SR 4 4 4
HOME ECR 5 5 5
GZB NR 5 5 5
LDH NR 5 5 5
JHS NCR 5 5 5
CNB NCR 5 5 5
LGD SCR 5 5 10
LGD SCR 5 5 5
This is working fine. Now the issue is that there are only 9 Hsheds and therefore I want to display only 9 latest records (unique Hshed along with cumulative column) in my grid view as final result so that no Hshed will repeate in the table. How to achieve this? please help.
You need to change your stored procedure(Has to be in it since you are discarding the date field in it).
You can use ROW_NUMBER() window function to filter only the latest records, like this:
SELECT Hshed,HRly,Dust_removal_to_done,Dust_removal_done,cumulative_dust_removal
FROM(
SELECT Hshed, HRly, Dust_removal_to_done, Dust_removal_done,
(SELECT SUM(Dust_removal_done) FROM TPHRAP_regular t2
where t2.Hshed = TPHRAP_regular.Hshed
and t2.Update_datetime <= TPHRAP_regular.Update_datetime) as cumulative_dust_removal,
ROW_NUMBER() OVER (PARTITION BY Hshed ORDER BY Update_datetime DESC) as rnk
FROM TPHRAP_regular)
WHERE rnk = 1
EDIT: You should also use SUM() OVER(..) for cumulative sum , no need to select from the table twice:
SELECT t.Hshed,
t.HRly,
t.Dust_removal_to_done,
t.Dust_removal_done,
t.cumulative_dust_removal
FROM (SELECT Hshed,
HRly,
Dust_removal_to_done,
Dust_removal_done,
SUM(Dust_removal_done) OVER(PARTITION BY Hshed ORDER BY Update_datetime) as cumulative_dust_removal,
ROW_NUMBER() OVER(PARTITION BY Hshed ORDER BY Update_datetime DESC) as rnk
FROM TPHRAP_regular) t
WHERE t.rnk = 1

Optimize query of sql server

I want number of users present in a department.i have 9 department.
now i am using "while" loop to count no of users present in an department.
Can you please suggest how to count no of users present in an department.
Department table(Columns: Id and DeptName)
User Table(Columns: Id, Name, deptId)
Please help me
Thanks
[EDIT]
User table
----------
id name deptid
1 a 1
2 b 1
3 c 2
4 d null
5 e 3
6 f 4
7 g 2
8 h 4
9 i 5
10 j 5
11 k null
Department
----------
id name
1 x
2 x1
3 y
4 y1
5 z
6 z1
Result
------
count depetname
9 all
2 x
2 x1
1 y
2 y1
2 z
0 z1
[/EDIT]
see DEMO
EDIT
you should be use RIGHT JOIN
select 'all', count(0) from users
union
SELECT department.name, COUNT(users.id) FROM users Right Outer JOIN department
ON users.deptId=department.id GROUP BY department.id

How can one use T-SQL to select only those rows that have column B Value not greater than the next greater values in column A?

A B
1 2
1 1
1 3
1 4
3 3
3 4
4 5
How can one use T-SQL to select only those rows that have column B value not greater than the next greater values in column A?
For example, if above table is the input, the output should be as follows;
A B
1 2
1 1
3 3
4 5
select t1.A, t1.B
from tbl t1
where t1.B < isnull((select min(t2.A)
from tbl t2
where t2.A > t1.A), t1.B+1)

Resources