Combining Rows and Taking Averages - sql

I have a table that looks like
#Sector max1 avg1 max2 avg2 numb
C 133 14 45 3 27
N 174 9 77 3 18
M 63 3 28 1 16
I would like to join rows N and M together call it X and take the max value of max1 and max2 while taking the avg of avg1, avg2, and numb in their respective columns to return
#Sector max1 avg1 max2 avg2 numb
C 133 14 45 3 27
X 174 6 77 2 17

Try this way:
select sector, max1,avg1,max2,avg2,numb
from tab
where sector not in ('M','N')
union all
select 'X' as sector, max(max1),avg(avg1),max(max2),avg(avg2),avg(numb)
from tab
where sector in ('M','N')

something like:
select
case when sector in ('N','M') then 'X' else sector end sect,
max(max1) max1,
avg(avg1) avg1,
max(max2) max2,
avg(avg2) avg2,
avg(numb) numb
from tabname
group by
case when sector in ('N','M') then 'X' else sector end

Related

How to combine same color with different sizes in one row using sql

I'm currenly working on combining color with different sizes and align it in one row because i'm going to display it in a table
Here's my SQL Query
SELECT
a.materialnumber, a.color, SUM(a.qty) as qty, c.CurrentSRP , a.size
FROM
table1 a, table2 c, table3 d
WHERE
a.stoID = 11
AND a.stoprocessID = 69
AND a.plantcode = 'PB001'
AND a.materialnumber = c.StockCode
AND a.size = c.Size
AND a.color = c.Color
AND a.stoprocessID = d.stoprocessID
AND a.plantcode = d.plantcode
AND a.materialnumber = '123456'
AND a.color = 'BLACK'
GROUP BY
a.materialnumber, a.color, a.qty, a.size, c.CurrentSRP
The result is
STOCKNUM COLOR QTY PRICE SIZE
-----------------------------------------
123456 BLACK 2 800 32
123456 BLACK 1 800 36
Under the Stocknum 123456 there's 1 color on it with 2 different size i want to display in it only one row
And I will align it on the table like this
STOCK NUM COLOR 24 25 26 27 28 29 30 32 34 36
123456 BLACK 0 0 0 0 0 0 0 2 0 1
That should be the output in my html
But i ended up with this output
**STOCK NUM COLOR 24 25 26 27 28 29 30 32 34 36**
123456 BLACK 0 0 0 0 0 0 0 2 0 0
123456 BLACK 0 0 0 0 0 0 0 0 0 1
Can someone help me with this?

oracle grand total row and column wise?

I want a query that will count the row wise and column wise total,i have found the way to calculate column wise count but not getting row wise count.
select nvl(to_char(R.LTHT_FLAG), 'total') as a,
SUM(CASE WHEN p.STATUS_CODE='0' OR p.STATUS_CODE='1' THEN 1 ELSE 0 END) K,
SUM(CASE WHEN p.STATUS_CODE='2' THEN 1 ELSE 0 END) W,
SUM(CASE WHEN p.STATUS_CODE='4' THEN 1 ELSE 0 END) C,
SUM(CASE WHEN p.STATUS_CODE='6' THEN 1 ELSE 0 END) R
from WORKASSIGNMENT P,RESOURCES R WHERE P.EMP_CODE=R.EMP_CODE
group by rollup (R.LTHT_FLAG);
the output of the following query is column wise count which is like.
A K W C R Total
DEVELOPMENT 1 18 397 0 ?
HT 43 21 673 0 ?
LT 83 14 7955 60 ?
SLD 306 9 4621 24 ?
----------------------------------------
total 433 62 13646 84 ?
but now i want row wise count for this output
Please help me to get the sum row-wise
Just add all the columns to get the total for each row.
For example,
SQL> WITH sample_data AS(
2 SELECT 'DEVELOPMENT' A, 1 k, 18 w, 397 c, 0 r FROM dual UNION ALL
3 SELECT 'HT' A, 43 k, 21 w, 673 c, 0 r FROM dual UNION ALL
4 SELECT 'LT' A, 83 k, 14 w, 7955 c, 60 r FROM dual UNION ALL
5 SELECT 'SLD' A, 306 k, 9 w, 4621 c, 24 r FROM dual
6 )
7 -- end of sample_data mimicking real table
8 SELECT t.*, k+w+c+r total FROM sample_data t;
A K W C R TOTAL
----------- ---------- ---------- ---------- ---------- ----------
DEVELOPMENT 1 18 397 0 416
HT 43 21 673 0 737
LT 83 14 7955 60 8112
SLD 306 9 4621 24 4960
SQL>
Above, instead of sample_data, put your current SQL as a sub-query in the FROM clause.

PostgreSQL: Select particular column and its total row count

I will explain my problem with an sample example
create table foo(id int,idx int,idy int,fld int,fldx varchar);
insert into foo values (1,2,3,55,'AA'),(2,3,4,77,'AB'),(3,4,8,55,'AX'),(9,10,15,77,'AR'),
(3,4,8,11,'AX'),(3,4,8,65,'AX'),(3,4,8,77,'AX');
id,idx,idy, fld,fldx
1 2 3 55 AA
2 3 4 77 AB
3 4 8 55 AX
9 10 15 77 AR
3 4 8 11 AX
3 4 8 65 AX
3 4 8 77 AX
I need to select only column fld and its total count of each column(fld) in descending order
Expected Result :
fld count
---------
77 3
55 2
11 1
65 1
select fld
,count(fld) rw_count
from foo
group by fld
order by rw_count desc
Group By
select fld,count(*) from foo group by 1 order by 2 desc ;

Merge two row into two column with third column same as it is

I have SQL output like this :
LINE SIZE TOT_COUNT
A 20 113
A 40 3
B 20 4
B 40 2
C 20 142
C 40 452
But I want like this:
LINE 20 40
A 113 3
B 4 2
C 142 452
Note: This is already a output, not any column of any table.
select
line,
sum(case size when 20 then tot_count end) as "20", -- use min is the same.
sum(case size when 40 then tot_count end) as "40"
from
your_table
group by
line
If the first output is from another query, you can replace your_table with your query as an sub query.

sum of differencies of rows

I have example values in column like this:
values
-------
89
65
56
78
74
73
45
23
5
654
643
543
345
255
233
109
43
23
2
The values are rising up and then fall down to 0 and rising up again.
I need to count differencies between rows in new column and the sum of these differencies too (cumulative sum) for all values. The values 56 and 5 are new differencies from zero
The sum is 819.
Example from bottom> (23-2)+(43-23)+(109-43)+..+(654-643)+(5)+(23-5)+..
Okay, here is my try. However, you need to add an Identity field (which I called "AddSequence") that starts with 1 for the first value ("2") and increments by one for every other value.
SELECT SUM(C.Diff) FROM
(
SELECT CASE WHEN (A.[Value] - (SELECT [Value] FROM [TestValue] AS B WHERE B.[AddSequence]= A.[AddSequence]-1)) > 0
THEN (A.[Value] - (SELECT [Value] FROM [TestValue] AS D WHERE D.[AddSequence]= A.[AddSequence]-1))
ELSE 0
END AS Diff
FROM [TestValue] AS A
) AS C
The first solution I had neglected that fact that we had to start over whenever the difference was negative.
I think you are looking for something like:
SELECT SUM(a - b)) as sum_of_differences
FROM ...
I think you want this for the differences, I've tested it in sqlite
SELECT CASE WHEN (v.value - val) < 0 THEN 0 ELSE (v.value - val) END AS differences
FROM v,
(SELECT rowid, value AS val FROM v WHERE rowid > 1) as next_val
WHERE v.rowid = next_val.rowid - 1
as for the sums
SELECT SUM(differences) FROM
(
SELECT CASE WHEN (v.value - val) < 0 THEN 0 ELSE (v.value - val) END AS differences
FROM v,
(SELECT rowid, value AS val FROM v WHERE rowid > 1) AS next_val
WHERE v.rowid = next_val.rowid - 1
)
EDITED - BASED OFF OF YOUR QUESTION EDIT (T-SQL)
I don't know how you can do this without adding an Id.
If you ad an Id - this gives the exact output you had posted before your edit. There's probably a better way, but this is quick and dirty - for a one time shot. Using a SELF JOIN. Differences was the name of your new column originally.
UPDATE A
SET differences = CASE WHEN A.[values] > B.[Values] THEN A.[values] - B.[Values]
ELSE A.[values] END
FROM SO_TTABLE A
JOIN SO_TTABLE B ON A.ID = (B.ID - 1)
OUTPUT
Select [Values], differences FROM SO_TTABLE
[values] differences
------------------------
89 24
65 9
56 56
78 4
74 1
73 28
45 22
23 18
5 5
654 11
643 100
543 198
345 90
255 22
233 124
109 66
43 20
23 21
2 0