I have a database with rows and columns of data, the row,col data is some summed data (ie. 1 3 5 7 from original data = 1 2 2 2).
I want to get the original data by subtraction eg. val=r1c2-r1c1 etc across the rows and columns. Is this possible in sql without having to do lots of individual select statements for each row/column?
I would like to do this for all rows and columns in database, like the following pseudocode
a[ 1,3,5,7;
2,5,6,7 ];
for(i=0;i<size(a,1); i++)
for(j=0;j<size(a,2)-1; j++)
b(i,j)=a(i,j+1)-a(i,j);
Try this.
SELECT col1,
col2 - col1 col2,
col3 - col2 col3,
col4 - col3 col4
FROM (SELECT 1 col1,
3 col2,
5 col3,
7 col4) a
Update: Same query will work for more than one row
SELECT col1,
col2 - col1 col2,
col3 - col2 col3,
col4 - col3 col4
FROM (SELECT 1 col1,3 col2,5 col3,7 col4
UNION ALL
SELECT 1 col1,3 col2,9 col3,11 col4
UNION ALL
SELECT 1 col1,3 col2,5 col3,7 col4) a
Related
Is there any way to select the greatest and second greatest numbers from multiple columns?
Example:
Col1 = 0;
Col2 = 4;
Col3 = 6;
Col4 = 3;
My greatest would be 6 followed by a second greatest of 4.
I'm trying implement something like this:
SELECT GREATEST(Col1, Col2, Col3, Col4) AS High,
GREATEST(Col1, Col2, Col3, Col4) AS Low
WHERE Low < High FROM tbl;
For 4 columns you can do it like this:
select
greatest(col1, col2, col3, col4) high,
greatest(col1, col2, col3) +
greatest(col1, col2, col4) +
greatest(col1, col3, col4) +
greatest(col2, col3, col4) -
3 * greatest(col1, col2, col3, col4) low
from tablename
You add all the greatest of all the combinations of 3 columns and subtract the greatest of all multiplied by 3 because out of the 4 combinations the 3 will result to the greatest of all.
See the demo.
Results:
| high | low |
| ---- | --- |
| 6 | 4 |
Forpas solution is clever, but it doesn't work with non-numeric data. A more conventional solution works when the values are different and not null:
select greatest(a, b, c, d),
(case greatest(a, b, c, d)
when a then greatest(b, c, d)
when b then greatest(a, c, d)
when c then greatest(a, b, d)
else d
end)
from t;
I have one table with these value
col1 col2 col3
---------------------- ------------------- -------------------------
1 0 ADD SERVICE ACTIVITY 1
0 1 ADD SERVICE ACTIVITY 1
0 8 Docment testing 2 (C07)
I want result like:
col1 col2 col3
---------------------- ------------------- -------------------------
1 1 ADD SERVICE ACTIVITY 1
0 8 Docment testing 2 (C07)
It seems that you want to group your data by column col3. I'm not sure whether results for col1 and col2 are sums or max values.
--for MAX:
SELECT MAX(COL1) as col1, MAX(COL2) as col2, COL3
/*-- for SUM:
SELECT SUM(COL1) as col1, SUM(COL2) as col2, COL3
*/
FROM test
GROUP BY COL3;
I was trying to create a new column in SQL which has calculated values from other columns in the same table.
Please see the case below
col1 col2
A 1
A 2
B 1
B 2
B 3
I need to create a new column 'col3' that finds the max of col2 grouped by col1. So the result i want is
col1 col2 col3
A 1 2
A 2 2
B 1 3
B 2 3
B 3 3
I would like to know if a query can be something along the below lines
ALTER TABLE table1 ADD col3 AS (SELECT max(col2) GROUPBY col1)
You don't need an ALTER statement and can get the extra column in your SELECT query like
select col1, col2, max(col2) as col3
from tbl1
group by col1
I am trying to accomplish something simple, but cant get to think straight. I have a case where 1 row can have different values in 2 different columns. But if thats the case then instead of displaying just 1 row for these 2 values, I need to display 2 rows for 1 column value each..for example.
ID Col1 col2 col3 col4
46054 2011W3974 164505 1 2
58765 2014W3777 275908 1 NULL
52311 2013W1877 247047 1 NULL
63032 2015W3317 295279 1 NULL
57552 2014W2813 274810 1 NULL
44584 2011W2622 173985 1 2
This needs to be split into 2 rows for row 1 and 6 into 2 rows like below:
46054 2011W3974 164505 1 NULL
46054 2011W3974 164505 NULL 2
58765 2014W3777 275908 1 NULL
52311 2013W1877 247047 1 NULL
63032 2015W3317 295279 1 NULL
57552 2014W2813 274810 1 NULL
44584 2011W2622 173985 1 NULL
44584 2011W2622 173985 NULL 2
What is the best possible way to do this. I looked at SPLIT XML function, but I dont think that will be helpful here. I also played with ranking functions, but since this is 2 columns, I dont think that will work either. Please suggest
Thanks,
RV
I'd properly just union it together:
SELECT Id, Col1, Col2, Col3, NULL AS Col4
FROM <Your Table>
WHERE col4 is NULL
UNION
SELECT Id, Col1, Col2, NULL, Col4
FROM <Your Table>
WHERE Col4 = 2
Just use Union not union All.
SELECT Id, Col1, Col2, Col3, NULL AS Col4
FROM YourTable
WHERE isnull(Col4 , 0) = 0
UNION
SELECT Id, Col1, Col2, NULL as Col3, Col4
FROM YourTable
WHERE isnull(Col3 , 0) = 0
When you use Group By how do you keep the other fields in sync when using aggregates
Here I am trying to find the min value of col4 when col2 = xxx
select col1, col2, col3, min(col4)
from table
where col2 = 'xxx'
group by col3
I can get the minimum value in col4 but col1 is not correct but col2, col3, col4 are.
Can anyone show me how to do this ?
Thanks
You are using non-standard MySQL extension to GROUP BY.
This query in fact reads as "for each distinct value of col3, select the minimal value of col4 along with the values of col1 and col2 from a single row of table having this value of col3 in no particular order"
Like, if we have the following data:
col1 col2 col3 col4
---- --- --- ----
A A 1 1
B B 1 2
C C 2 3
D D 2 4
, this query:
SELECT col1, col2, col3, MIN(col4)
FROM mytable
GROUP BY
col3
will return either of the following:
col1 col2 col3 col4
---- --- --- ----
A A 1 1
C C 2 3
col1 col2 col3 col4
---- --- --- ----
B B 1 1
C C 2 3
col1 col2 col3 col4
---- --- --- ----
A A 1 1
D D 2 3
col1 col2 col3 col4
---- --- --- ----
B B 1 1
D D 2 3
i. e. it can return any value of col1 and col2 found in the rows that contribute to the corresponding group.
This is equivalent of FIRST_VALUE analytic function, but in no particular order.
Update:
To select values of col1 and col2 corresponding to the minimal value of col4 within each group, use this:
SELECT col1, co2, col3, col4
FROM (
SELECT col1, col2, col3, col4,
COALESCE(#col3 = col3, FALSE) AS grp,
#col3 := col3 AS nv
FROM (
SELECT #col3 := NULL
) vars, table
WHERE col2 = 'xxx'
ORDER BY
col3, col4
) q
WHERE NOT grp
select a.col3, a.col2, a.col1, a.col4
from table as a natural join
(select col3, min(col4) as col4 from table
where col2='xxx'
group by col3 ) as b
where a.col2 = 'xxx' -- sometimes this helps the optimizer even though it's redundant
you can get into a bit of trouble here when there may be multiple rows with the same col3, col4, and col2, but different col1s -- pretty straightforward to fix with rownums and such, but that gets db-specific.
I guess you want the col1-3 corresponding to the min(col4) for each
col3?
Something like:
select X.col1, X.col2, X.col3, X.col4 from table X
join (select col3, min(col4) as mcol4 from table where col2='xxx' group by col3) as Y
on X.col3=Y.col3 and X.col4=Y.mcol4
where X.col2='xxx';