Sum all row exclude the negative numbers in Oracle Reports - sql

I have one table with 10 rows. I want to sum the values in a particular column. But I want to exclude all the negative numbers.
How can I sum just the positive numbers and ignore the negative numbers?
This is my pictures example:

With your specifications,
Select sum(col)
from table
where col>0;

You can do something like
SELECT SUM( case when column_name > 0
then column_name
else 0
end ) sum_of_non_negative
FROM table_name

Select sum(col)
from table
where col>0
AND col not like '-%';

Related

SQL: identify if there are multiples (not duplicates) in a column

I am currently struggling in identifying a possibility to identify certain patterns in my data using SSMS.
I wish to identify rows that contain multiples (x2, x3, or x*4) of an entry within the same column.
I really have no clue on how to even start my "where" statement right now.
SELECT [numbers], [product_ID]
FROM [db].[dbo].[tablename]
WHERE [numbers] = numbers*2
My problem is that with the code above I can obviously only identify zeros.
Google only helps me out with finding duplicates but I can't find a way to identify multiples of a value...
My desired result would be a table that only contains numbers (linked to product_IDs) that are multiples of each other
Anyone can help me out here?
If a column contains multiples, then all are multiples of the smallest non-zero value. Let me assume the values are positive or zero for this purpose.
So, you can determine if this is the case using window functions and modulo arithmetic:
select t.*
from (select t.*,
min(case when number > 0 then number end) over () as min_number
from t
) t
where number % min_number = 0 or min_number = 1;
If you want to know if all numbers meet this criteria, use aggregation:
select (case when min(number % min_number) = 0 then 'all multiples' else 'oops' end)
from (select t.*,
min(case when number > 0 then number end) over () as min_number
from t
) t
My desired result would be a table that only contains numbers (linked to product_IDs) that are multiples of each other
You'll need to test all pairs of rows, which means a CROSS JOIN.
Something like this:
with q as
(
SELECT [numbers],
[product_ID],
cast(a.numbers as float) / coalesce(b.numbers, null) ratio
FROM [tablename] a
CROSS JOIN [tablename] b
)
select *
from q
where ratio = cast(ratio as bigint)
and ratio > 1

Can't get a correct sort with SQL Server 2008

I have a table with returns such as 12X90, 12X120, 12X160, etc.
The numbers after the "X" are weights. I need to pad the weights only with leading zeros so that 90 become 090. A normal sort will put the 90 last instead of first, I need the list sorted correctly, by weight.
How can I achieve that?
Use charindex to get the number after X in the column and order by that number.
select * from tablename
order by cast(case when charindex('X',col) > 0 then substring(col,charindex('X',col)+1,len(col))
else col end as numeric)
If the string before X should also be considered for sorting, use
select * from tablename
order by
cast(case when charindex('X',col) > 0 then substring(col,1,charindex('X',col)-1)
else col end as numeric)
,cast(case when charindex('X',col) > 0 then substring(col,charindex('X',col)+1,len(col))
else col end as numeric)
Sample demo
use a cross apply
select
b.* from table t1
cross apply
(select replace(t.weight,'12x','') as replaced from table t2 where t2.weight=t1.weight) b
order by b.replaced

How to calculate an average in SQL excluding zeroes?

I want to calculate the average of a column of numbers, but i want to exclude the rows that have a zero in that column, is there any way this is possible?
The code i have is just a simple sum/count:
SELECT SUM(Column1)/Count(Column1) AS Average
FROM Table1
SELECT AVG(Column1) FROM Table1 WHERE Column1 <> 0
One approach is AVG() and CASE/NULLIF():
SELECT AVG(NULLIF(Column1, 0)) as Average
FROM table1;
Average ignores NULL values. This assumes that you want other aggregations; otherwise, the obvious choice is filtering.
Multiple roads lead to Rome...
select sum(Column1) / sum( case Column1
case 0 then 0 else 1
end )
from table1
SELECT AVG(column) FROM TABLE
where column **not like** '0%'

Counting spaces before and after a decimal point

I have data set as a varchar(500), but I only know if it's numeric or character.
I need to count the max spaces of the length of a column AND the max spaces after a decimal point.
For Example:
ColumnA
1234.56789
123.4567890
would return 11 spaces total AND 7 spaces after the decimal.
It can be two separate queries.
SELECT len(ColumnA), len(columnA) - charIndex('.',ColumnA)
FROM theTable
SELECT LEN(ColumnA )
,CHARINDEX('.',REVERSE(ColumnA ))-1
FROM Table1
If a value has no decimal, the above will return -1 for the spaces after decimal, so you could use:
SELECT LEN(ColumnA)
,CASE WHEN ColumnA LIKE '%.%' THEN CHARINDEX('.',REVERSE(ColumnA))-1
ELSE 0
END
FROM Table1
Demo of both: SQL Fiddle
If you just wanted the MAX() then you'd just wrap the above in MAX():
SELECT MAX(LEN(ColumnA ))
,MAX(CHARINDEX('.',REVERSE(ColumnA ))-1)
FROM Table1
SELECT ColumnA, Len(ColumnA) As Total, LEN(SUBSTRING(ColumnA,CHARINDEX('.',ColumnA,LEN(ColumnA)) As Decimal
FROM TABLE

Choose grouping value based on condition

I need to construct a query which meets the following criteria:
select X columns and MAX(column1), group by X columns
if column1 contains negative value (can be only one in given group), then instead of MAX(column1) display 'reset'
I know I can do it by using case before grouping and substituting negative values with a very large number and then using case after grouping, but this is an awfully messy solution.
Does anybody have a better idea how to solve this?
You can additionally select the minimum value and check that for negativity.
Something like the following:
select foo, case when min_value < 0 then 'reset' else to_char(max_value) end
from
(
select foo, max(column1) as max_value, min(column1) as min_value
from your_table
group by foo
)
Or, without the sub query:
select foo, case when min(column1) < 0 then 'reset' else to_char(max(column1)) end
from your_table
group by foo