I have the following table:
column1 | column2 | column3
1 3 4
5 7 6
how do I sum the values of say, column 2 and 3, to return the sum?
The expected result is:
res
7
13
You can do maths within a select statement, so the following will work:
SELECT column2 + column3 AS res FROM table
This works in postgresql.
select sum(col2+col3) from (
select col1, col2,col3,row_number() over() as rows from column_sum ) as foo
group by rows order by rows;
Related
using BigQuery, I would like to be able to divide one column, column1, into two separate columns, column2, and column3 with 50% of all records in column1 in column2 and 50% of all records in column1 in column 3. Ex column1 has 8 records of the number 2. I'd like to create a column2 with 4 records of the number 2 and column3 with 4 records of the number 2.
Is there a query to write this in BigQuery?
Column1
2
2
2
2
2
2
2
2
Column2
2
2
2
2
Column3
2
2
2
2
try:
SELECT
Column1 AS Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) <= (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
SELECT
Column1 AS Column3
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY ROW_NUMBER() OVER (ORDER BY Column1) > (
SELECT COUNT(*)/2
FROM `my-project.my-dataset.my-table`
);
This will give you 2 results: One for each Column2 and Column3 with the first and second half of the data respectively order by Column1 (to use analytical functions you always have to specify an ORDER BY inside an OVER clause)
For random order try:
CREATE TEMP TABLE a AS (
SELECT Column1 as Column2
FROM `my-project.my-dataset.my-table`
WHERE 1=1
QUALIFY
ROW_NUMBER() OVER (ORDER BY RAND()) <= (SELECT COUNT(*)/2 FROM `my-project.my-dataset.my-table`)
);
SELECT Column1 as Column3
FROM `my-project.my-dataset.my-table`
WHERE Column1 NOT IN (SELECT * FROM a);
SELECT * FROM a
In this case you'll get 3 results: first one is the temporary table creation and the other 2 are the columns 2 and 3.
I am working on Ms Sql Server 2008 R2.
now i want one column which has results of 3 different columns of same table..
Let me explain with Figure.
Table: mainTable
Id Column1 Column2 Column3
-------------------------------------
1 urla urlb urlc
2 urld urle urlf
3 urlg urlh urli
Now i want one column
Table Name: ResultTable
Id ColumnResult
-------------
1 urla
2 urlb
3 urlc
4 urld
5 urle
6 urlf
7 urlg
8 urlh
9 urli
Thanks in Advance.
Sahil Patel
You can use UNION ALL:
SELECT
Id = ROW_NUMBER() OVER(ORDER BY ColumnResult),
Column1
FROM (
SELECT Id, Column1 AS ColumnResult FROM mainTable UNION ALL
SELECT Id, Column2 FROM mainTable UNION ALL
SELECT Id, Column3 FROM mainTable
)t
I have created a table in SQL and am trying to retrieve a ROW that returns the Total of all rows in a the value column and names the description value 'Total'. Is this done in the stored procedure?
EX: Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
I want the table to end looking like the following:
Table1
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
4 6/30/14 Total 30.3
Can you please help with how to do this?
Thank you.
Here is an sql statement that does what you ask:
SELECT Column1, Column2, Desc, ValueColumn
FROM
(
SELECT 1 as rolluporder, Column1, Column2, Desc, ValueColumn
FROM TABLE
UNION ALL
SELECT 2 as rolluporder, null as Column1, null as Column2,
'Total' AS Desc, SUM(ValueColumn) as ValueColumn
FROM TABLE
) T
ORDER BY rolluporder, Column1
This looks a little different but I expect it is what you really want:
Column1 Column2 Desc ValueColumn
1 6/30/14 One 11.1
2 6/30/14 Two 10.2
3 6/30/14 Three 9.0
null null Total 30.3
DECLARE #IDC AS INT
SET #IDC = (
SELECT TOP 1 Column1
FROM [yourtable]
ORDER BY 1 DESC
)
SELECT *
FROM [yourtable]
UNION ALL
SELECT #IDC, Column2, 'Total', SUM(ValueColumn)
FROM [yourtable]
GROUP BY Column2
I have 3 columns as
Column1 column2 column3
GU1 1 a
GU1 2 a
GU1 3 a
GU2 4 b
GU3 5 c
GU4 6 a
GU4 7 b
I would like to filter out the column1 where the values are having multiple column3 values
In this example, I want to pull GU4 where it is having both a & b in the column3.
Use distinct in your count
select column1
from your_table
group by column1
having count(distinct column3) = 1
I am using oracle 10g EE database.I have one table mytable and has two columns and data is as follows:
Note: I want to find out data based on same value in 2nd column only, it does not matter whether there exists same or different value in first column.
10 is repeated 3 times for A, B and C and these 3 are required output
similarly 20 is repeated 2 times for C and D and these are also required output
column1 column2
-------------- ---------------
A 10 //required
A 10 //required
B 10 //required
C 20//required
D 20//required
E 30--------not required as 30 is only here and not duplicated
F 40--------not required as 40 is only here and not duplicated
following output is required i.e. same value in 2nd column having same or different values in 1st column
column1 column2
-------------- ---------------
A 10
A 10
B 10
C 20
D 20
SELECT column1,
column2
FROM <table> t1
WHERE column2 IN (SELECT column2
FROM <table> t2
GROUP BY column2
HAVING count(*) > 1);
It sounds like you want
SELECT *
FROM table_name t1
WHERE column2 IN( SELECT column2
FROM table_name t2
GROUP BY column2
HAVING COUNT(*) > 1 )
This appears to work with your sample data
SQL> with table_name as (
2 select 'A' column1, 10 column2 from dual union all
3 select 'A', 10 from dual union all
4 select 'B', 10 from dual union all
5 select 'C', 20 from dual union all
6 select 'D', 30 from dual)
7 SELECT *
8 FROM table_name t1
9 WHERE column2 IN( SELECT column2
10 FROM table_name t2
11 GROUP BY column2
12 HAVING COUNT(*) > 1 );
C COLUMN2
- ----------
B 10
A 10
A 10
select * from table where column2 in ( select column2 from table group by coulmn2 having count(*)>1);
should work for you.
Thanks
Abhi