compare value of one column sum to other column of same table - sql

I have a table with some columns.I would like to take sum one column and compare the value to other single column value. if sum of first column equal of last row of other column then ok other wise show this difference.
Example.
Table abc have column code,val_1 and val_2
code val_1 val_2
A_1 200 100
A_1 150 50
A_1 250 25
A_1 50 650
Now if sum of val_1 is equal to last row of Val_2 "650" then ok if not equal then show it.

SQL tables represent unordered tables. There is no "last row".
If you do have an ordering column, you can use window functions:
select t.*
from (select t.*,
sum(val_1) over (partition by code) as sum_val_1,
row_number() over (partition by code order by <orderingcol> desc) as seqnum
from t
) t
where seqnum = 1 and sum_val_1 <> val_2;
If the last value in val_2 is always the maximum, then aggregation suffices:
select code
from t
group by code
having sum(val_1) <> max(val_2);

Related

SSRS sum values based on row number

I would like to get the sum of all values in a column IF they have a rowNumber greater than the current row
so if i have 4 rows:
Row 1 would be the sum of rows 2,3 and 4
Row 2 would be the sum of rows 3 and 4
Row 3 would be the value of row 4
Row 4 would be 0
This is the query I currently have:
SELECT
no_,
name AS member,
amount,
effective,
balance
FROM c
WHERE
status != 'closed'
AND amount != 0.00
ORDER BY no_, effective
In my ssrs table I'm using the expression RowNumber("member") to assign row numbers for each row per member
You would seem to want a cumulative sum in reverse order, excluding the current value. I would express this as:
select sum(val) over (order by rownum desc) - val
I am clear how this relates to your query.
You can also express this using a window frame:
select sum(val) over (order by rownum rows between 1 following and unlimited following)
The only difference is that this would return NULL for the last row rather than 0.

Find Nearest rows in SQL

I have table with 2 records and column name quantity.
quantity
2268
22680
so,
when my required quantity 2500 then i want to display both 2 records
when my required quantity 2000 then display 1st row.
You seem to want a cumulative sum. The ANSI standard method would be:
select t.*
from (select t.*, sum(quantity) over (order by ?) as cume_quantity
from t
) t
where cume_quantity - quantity <= <your value here>;
The ? is for the column or expression that specifies the ordering of the rows.

SQL select top rows based on limit

Please help me t make below select query
Source table
name Amount
-----------
A 2
B 3
C 2
D 7
if limit is 5 then result table should be
name Amount
-----------
A 2
B 3
if limit is 8 then result table
name Amount
-----------
A 2
B 3
C 2
You can use window function to achieve this:
select name,
amount
from (
select t.*,
sum(amount) over (
order by name
) s
from your_table t
) t
where s <= 8;
The analytic function sum will be aggregated row-by-row based on the given order order by name.
Once you found sum till given row using this, you can filter the result using a simple where clause to find rows till which sum of amount is under or equal to the given limit.
More on this topic:
The SQL OVER() clause - when and why is it useful?
https://explainextended.com/2009/03/08/analytic-functions-sum-avg-row_number/

Sum values from one column if Index column is distinct?

How do I sum values from one column when index column is distinct?
Initially, I had this SQL query:
SELECT COALESCE(SUM(ISNULL(cast(Quantity as int),0)),0) AS QuantitySum FROM Records
Also tried to do this, but this is incorrect when some Quantity values happen to be the same:
SELECT COALESCE(SUM(DISTINCT ISNULL(cast(Quantity as int),0)),0) AS QuantitySum FROM Records
How can I fix this query to sum only records quantity that is distinct by Index value?
Example of Table:
Index Quantity
AN121 40
AN121 40
BN222 120
BN111 20
BN2333 40
So.. I want to return 220
I have duplicate Ids, but quantity can be the same for different records
Do you mean that you only want to sum one value of quantity for each individual value of the index column?
select sum(case when row_number() over (partition by `index` order by newid()) = 1
then cast(Quantity as int)
end) as QuantitySum
from Records;
Or, do you mean that you only want to sum values of quantity when there is exactly one row with a given index value:
select sum(case when count(*) over (partition by `index`) = 1
then cast(Quantity as int)
end) as QuantitySum
from Records;
Both of these use window functions to restrict the values being processed.
Also, a column called quantity should be stored as a numeric type, so conversion isn't needed to take the sum.
You can try something like:
SELECT DISTINCT COL1
, SUM(COL2)
FROM MYTABLE
GROUP BY COL1
You can use this, if you have duplicated Ids and Quantity:
SELECT COALESCE(SUM(DISTINCT ISNULL(cast(Quantity as int),0)),0) AS QuantitySum
FROM (SELECT Id, Min(Quantity) From Records group by Id)

query for roww returning the first element of a group in db2

Suppose I have a table filled with the data below, what SQL function or query I should use in db2 to retrieve all rows having the FIRST field FLD_A with value A, the FIRST field FLD_A with value B..and so on?
ID FLD_A FLD_B
1 A 10
2 A 20
3 A 30
4 B 10
5 A 20
6 C 30
I am expecting a table like below; I am aware of grouping done by function GROUP BY but how can I limit the query to return the very first of each group?
Essentially I would like to have the information about the very first row where a new value for FLD_A is appearing for the first time?
ID FLD_A FLD_B
1 A 10
4 B 10
6 C 30
Try this it works in sql
SELECT * FROM Table1
WHERE ID IN (SELECT MIN(ID) FROM Table1 GROUP BY FLD_A)
A good way to approach this problem is with window functions and row_number() in particular:
select t.*
from (select t.*,
row_number() over (partition by fld_a order by id) as seqnum
from table1
) t
where seqnum = 1;
(This is assuming that "first" means "minimum id".)
If you use t.*, this will add one extra column to the output. You can just list the columns you want to avoid this.