how to minus negative values in sum? - sql

I am using SUM() function. But SUM() sums the negative value in the column. In a column if the value is positive then it should be added and for negative values should be substracted and not added as the SUM()
20.00
20.00
20.00
20.00
-20.00
20.00
20.00
40.00
20.00
20.00
20.00
20.00
20.00
-20.00
-20.00
20.00
sum() should return 220 and not 440.
Is returning 440.

To subtract negative numbers rather than add them you would use SUM(ABS(col)) but just to check this is what you actually need example results below.
WITH YourTable(col) AS
(
SELECT 2 UNION ALL
SELECT -5
)
SELECT
SUM(ABS(col)) AS [SUM(ABS(col))],
SUM(col) AS [SUM(col)]
FROM YourTable
Returns
SUM(ABS(col)) SUM(col)
------------- -----------
7 -3

SELECT SUM(ABS(Column_Name)) FROM Table_Name;

Related

Converting Columns into Rows in Pentaho

i have a table below that looks like the current one below. How can i put the values for date, count2, amount2 from columns into rows just ONCE?
current:
date type count amount type2 date count2 amount2
1/1/20 00 5 13.49 ZZZ 1/1/20 0 5.00
1/1/20 12 6 14.69 ZZZ 1/1/20 0 5.00
1/1/20 11 10 20.66 ZZZ 1/1/20 0 5.00
expected
date type count amount
1/1/20 12 6 14.69
1/1/20 12 6 14.69
1/1/20 11 10 20.66
1/1/20 ZZZ 0 5.00
what can you do it just splits data flow into two select steps.
in the first select step use your first three columns.
in the second select step use your remaining other columns. and use filter rows to remove redundancy. then use dummy step and link to both select steps.

Excel 2007 Calculations

I have a Excel sheet, one date column with different description and values. How can I calculate the sum of the values for each dates using formula not pivot table?
I need summary as shown below:
Date Jun-15 Jul-15
1 175.00 200.00
2 100.00 75.00
3 200.00 175.00
4 60.00 70.00
5 20.00 225.00
6 80.00 50.00
You can use a simple SUMIF() formula:
Hi you can try this to solve your problem. Please let me know it this resolves your query.

how to calculate balances in an accounting software using postgres window function

I'ved got a problem same as this but I am using Postgres.
Calculate balance with mysql
have a table which contains the following data:
ID In Out
1 100.00 0.00
2 10.00 0.00
3 0.00 70.00
4 5.00 0.00
5 0.00 60.00
6 20.00 0.00
Now I need a query which gives me the following result:
ID In Out Balance
1 100.00 0.00 100.00
2 10.00 0.00 110.00
3 0.00 70.00 40.00
4 5.00 0.00 45.00
5 0.00 60.00 -15.00
6 20.00 0.00 5.00
How best to handle "balance" calculation. I was told there is window function in postgres, how would this be done using postgres window functions ?
Thanks.
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
order by id
Fiddle: http://sqlfiddle.com/#!15/97dc5/2/0
Consider changing your column names "In" / "Out" so that you don't need to put them in quotes. (They are reserved words)
If you wanted only one customer (customer_id = 2):
select t.*, sum("In"-"Out") over(order by id) as balance
from tbl t
where customer_id = 2
order by id
If your query were to span multiple customers and you wanted a running balance that RESTARTED with each customer, you could use:
select t.*, sum("In"-"Out") over( partition by customer_id
order by customer_id, id ) as balance_by_cust
from tbl t
order by customer_id, id

Determining if value is between two other values in the same column

I'm currently working on a project that involves using a user-provided charge table to calculate fees.
The table looks like:
MaxAmount Fee
10.00 1.95
20.00 2.95
30.00 3.95
50.00 4.95
As seen in the table above, any MaxAmount up to 10.00 is charged a 1.95 fee. Any MaxAmount between 10.01 and 20.00 is charge a 2.95 fee, etc. Finally, any MaxAmount above 50.00 is charged 4.95.
I'm trying to come up with a sql query that will return the correct fee for a given MaxAmount. However, I'm having trouble doing so. I've tried something similar to the following (assuming a provided MaxAmt of 23.00):
SELECT Fee FROM ChargeTable WHERE 23.00 BETWEEN MaxAmt AND MaxAmt
Of course, this doesn't give me the desired result of 3.95.
I'm having trouble adapting SQL's set-based logic to this type of problem.
Any and all help is greatly appreciated!
If the MaxAmount behaves as the table suggests, then you can use:
select top 1 fee
from ChargeTable ct
where #Price <= MaxAount
order by MaxAmount desc
As you describe it, you really want another row:
MaxAmount Fee
0.00 1.95
10.00 1.95
20.00 2.95
30.00 3.95
50.00 4.95
Your original table does not have enough values. When you have 4 break points, you actually need 5 values -- to handle the two extremes.
With this structure, then you can do:
select top 1 fee
from ChargeTable ct
where #Price >= MaxAount
order by MaxAmount desc
You could try something like:
SELECT min(Fee) FROM Fees WHERE 23<=MaxAmount
Have a look here for an example:
http://sqlfiddle.com/#!2/43f2a/5

SQL get the next rows value into current row

I am trying to get the next row's 'alignment' value and store it into the current row in a field called nextAlign. I have tried using grouping and also the unique identifier for this table is not consistent and doesn't always have an increment of 1. Here is some data:
type field starttop startleft length decimals alignment
-------------------------------------------------------------------------------------------
Text CUSTOMER DETAILS CONFIRMATION 13.00 38.00 30.00 0.00 Centre
Text Please spare a few minutes to 15.00 2.00 30.00 0.00 Left
Text confirm your details as held 15.00 2.00 30.00 0.00 Wrap
Text on our system. You may fax the 15.00 2.00 30.00 0.00 Wrap
Text form to the number above. 15.00 2.00 30.00 0.00 Wrap
Text Any information you may supply 17.00 2.00 30.00 0.00 Left
Text will not be made available to 17.00 2.00 30.00 0.00 Wrap
Text third parties according to the 17.00 2.00 30.00 0.00 Wrap
Text Privacy Act. 17.00 2.00 30.00 0.00 Wrap
Text Legal name: 20.50 2.00 30.00 0.00 Left
All I want is a column called 'nextAlign' that has the following data:
nextAlign
-Left
-Wrap
-Wrap
-Wrap
-Left
-Wrap
You didn't specify your DBMS, so this is ANSI SQL:
select type,
field,
align,
lead(align) over (order by starttop) as next_align,
starttop,
startleft
from the_table
use ROW_NUMBER() OVER(ORDER BY )
and outer join it to the same select this way: select_1_rownumber = select_2_rownumber+1
with temptable as
( select rownum
,type
,field
,starttop
,startleft
,length
,decimals
,alignment
from YOURTABLE )
select t.type
,t.field
,t.starttop
,t.startleft
,t.length
,t.decimals
,t.alignment
( select tt.alignment from temptable tt where tt.rownum = t.rownum+1 ) nextalign
from temptable t
Might work for you.