Getting row sum of the Table - sql

Is it possible to take the row sum of the all rows from a datatable in SQL. I have one datatable like this
Date col1 col2 col3
30/7/2012 5 2 3
31/7/2012 2 3 5
01/08/2012 2 4 1
but i want to achieve like this by creating one column name Total
like:
Date Total col1 col2 col3
30/7/2012 10 5 2 3
31/7/2012 9 1 3 5
01/08/2012 7 2 4 1
Is it Possible? If yes please help me for the same.

try this:
select Date,
col1 + col2 + col3 as Total,
col1, col2, col3
from your_table;

You can use the DataColumn.Expression property of your Total DataColumn to calculate the values in the column as the sum of the values of other columns in each row:
var totalColumn = new DataColumn("Total");
// Add your Total column to your DataTable.
dt.Columns.Add(totalColumn);
// The new column will be the second in the DataTable, like your diagram shows.
totalColumn.SetOrdinal(1);
// Use the sum of each row's Col1, Col2 and Col3 for the values in this column.
totalColumn.Expression = "[Col1] + [Col2] + [Col3]";

Related

Assign row number per ID, where certain values always have fixed numbers

I want to create a query that assigns row numbers per ID in a database table, and certain specific values always get fixed row numbers. For instance, if the value in col2 is A, then the row number should be consistently set to 1. Similarly, if col2 contains the value B, then the row number should always be 2. All other values in col2 should be assigned row numbers in consecutive order starting from 3.
Desired result:
myid col1 col2 row_number
----------------------------------
1 foo A 1
1 bar B 2
1 foobar C 3
1 foobar D 4
2 foobar A 1
2 foob X 3
3 hello B 2
3 hello Z 3
3 hi Y 4
Here is an example which is not working properly.
Sounds like you want to start the row_number with a specific offset, ignoring constant values and assigning them a constant row number.
You can do something a bit ugly like this:
SELECT myid, col1, col2,
case
when col2 = 'A' then 1
when col2 = 'B' then 2
else row_number() over (partition by myid
order by case when col2 = 'A' then 'ZZZ'
when col2 = 'B' then 'ZZZ1'
else col2
end) + 2
end as row_number
FROM newtable
ORDER BY myid, row_number
Result:
MYID COL1 COL2 ROW_NUMBER
1 foo A 1
1 bar B 2
1 foobar C 3
1 foobar D 4
2 foobar A 1
2 foob X 3
3 hello B 2
3 hello Y 3
3 hello Z 4
This start the row number from +2 (Depending on the number of constant values [A,B]), giving each constant value a value that will be sorted last in the row_number window function so the rest will be sorted first.

How to sum all row per item?

In relation to my previous question How to use the result of previous row in oracle?
I need to sum the value per item.
Col | Col A | Col B
Item1 1 | 1 (col A)
Item1 2 | 3 (colA + prevColB)
Item1 3 | 6 (colA + prevColB)
Item2 1 | 1 (colA)
Item2 4 | 5 (colA + prevColB)
Item2 3 | 8 (colA + prevColB)
SQL tables represent unordered sets. Your cumulative sum assumes an ordering of the table, that is not apparent in the question.
The syntax for the cumulative sum is:
select t.*
sum(cola) over (partition by col order by ?) as colb
from t;
The ? is for the column (or expression) that represents the ordering of the rows.
If you mean Just one previous row value(but not overall sum), then use lag function ,which gives the value of the column for the previous row, as in the following SQL :
select colA, colA+
lag(colA,1,0) over (partition by Col order by Col ) as ColB
from tab;
COLA COLB
1 1
2 3
3 5
1 1
4 5
3 7
SQL Fiddle Demo
col is item i thing, u can try bellow
select col, sum(col A), sum( col B) from tb group by col
enjoy it broh

How write a query to transpose a table in hive with variable columns?

My current table is as below
username col1 col2 col3
x 1 2 3
y 4 5 6
z 7 8 9
I have several table like this but the number of columns may be different. (example: another table have columns username,col1,col2,col3,col4,col5)
I want to collapse all the columns to single column and introduce the new column to store column names
So now my new tables look like this
username col_new val
x col1 1
x col1 2
x col1 3
y col2 4
y col2 5
y col2 6
z col3 7
z col3 8
z col3 9
I can do it manually. But since the number columns are different in each table, So want a hive query which can take variable number of columns and create the above table like format.
Please suggest.

Return Multiple Rows Based On A Value From Another Column

I have a table that has 3 columns. Col1 is an identity column and sets to autoincrement. Now I want to return multiple rows based on a value in Col3, and the Col2 increments by 1.
For example, if I insert 25 in Col2 and 7 in Col3, I want an output similar to this one:
Col1 Col2 Col3
---- ---- ----
1 25 7
2 26 7
3 27 7
4 28 7
5 29 7
6 30 7
7 31 7
I tried something using partition in my SELECT query, but still I didn't get the desired output. What do you think is the better way to do this?
As far as I understand you're looking for something like this:
SELECT ROW_NUMBER() OVER (ORDER BY col3, col1) AS col2, col3 FROM ...
Provide more details if that doesn't fit.

Row to Column and Column to Row conversion in query

I want to invert the data in my table, i.e. convert a:
Row to Column.
Column to Row.
The actual table data is
col1 col2
---------------------------------
row1 1 2
row2 3 4
Expected output :
col1 col2
---------------------------------
row1 1 3
row2 2 4
I have tired with a normal select query but couldn't work out how to do this. Is it possible in PL/SQL?
(select * from (select a,b from t) pivot(sum(a) for b in(2,4)))
union
(select * from (select a,b from t) pivot(sum(b) for a in(1,3)));