Divide two columns into group by function SQL - sql

If I have the below select query:
select col1, col2, col3, sum(value1) as col4, col5
from table1
group by col1, col2, col3, col5
How to add col6 = col4/col5?

You cannot access the alias in the SELECT clause. So you have to repeat sum(value1):
select col1, col2, col3,
sum(value1) as col4,
col5,
sum(value1) / col5 as col6
from table1
group by col1, col2, col3, col5

Do the GROUP BY in a derived table:
select col1, col2, col3, col4, col5, col4/col5 as col6
from
(
select col1, col2, col3, sum(value1) as col4, col5
from table1
group by col1, col2, col3, col5
) dt

You can perform operations in the select statement. However, you cannot use the aliases of the SQL statement within it. So you need to do the calculation for col4 again. Just add sum(value1)/col5 as col6.
select col1, col2, col3, sum(value1) as col4, col5, sum(value1)/col5 as col6
from table1
group by col1, col2, col3, col5

Related

Delete duplicate data that some columns equal zero

I have SQL Server table that has col1, col2, col3, col4, col5, col6, col7, col8, col9, col10.
I want delete the duplicate based on col1, col2, col3.
The row that should be deleted is where col6=0 and col7=0 and col8=0.
We can use a deletable CTE here:
WITH cte AS (
SELECT *, COUNT(*) OVER (PARTITION BY col1, col2, col3) cnt
FROM yourTable
)
DELETE
FROM cte
WHERE cnt > 1 AND col6 = 0 AND col7 = 0 AND col8 = 0;
The CTE above identifies "duplicates" according to your definition, which is 2 or more records having the same values for col1, col2, and col3. Then we delete duplicates meeting the requirements on the other 3 columns.

oracle sql group by column which count them

I ran below query to fetch count of data which I can see in output but it does not working as I wish
How can I print count of col6 & col7 in output?
Am I clear?
select col1, col2,
col3, col4, decode(col5,'S','Success','F','Failed'), col6, col7, count(*)
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4
group by col1, col2, col3,col4,col5,col6,col7
See if this works
select count(col6), count(col7)
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4;
You need to use the proper aggregate function and remove the col6 and col7 from GROUP BY clause following query:
select col1, col2, col3, col4, decode(col5,'S','Success','F','Failed'),
count(col6), count(col7), count(*) -- used count for col6 and col7
from mytable
where col1 in (select FIELD1 from temp)
and col8 = 4
group by col1, col2, col3,col4,col5 -- removed col6 and col7 from here

with clause in union query

I have with clause in union query like
with t1 as(...) ---common for both query
select * from t2
union
select * from t3
how to handle same with cluase in both queries?
You can reuse a Common Table Expression
For example:
with cte as
(
select col1, col2, col3, col4, col5, col6
from sometable
where col1 = 42
)
select col1, col2, col3
from cte as t1
union all
select col4, col5, col6
from cte as t2
If you need more CTE, then a comma can be used to separate them.
with cte1 as
(
select col1, col2, col3
from sometable
where col1 = 42
group by col1, col2, col3
)
, cte2 as
(
select col4, col5, col6
from sometable
where col4 > col5
group by col4, col5, col6
)
select col1, col2, col3
from cte1 as t1
union all
select col4, col5, col6
from cte2 as t2
But in this example it would be more something for aesthetic reasons, by putting the more complicated queries at the top of the SQL.
Because it would be more straightforward to just union the queries from the CTE's together.
select col1, col2, col3
from sometable
where col1 = 42
group by col1, col2, col3
union all
select col4, col5, col6
from sometable
where col4 > col5
group by col4, col5, col6

SQL inserting data from tabl1 to tabl2

I have 2 tables in SQL
table 1: col1, col2, col3, col4, col5, col6, col7
table 2: col1, col2, col4, col5, col10 (newcol)
col10 (newcol) should be given default value 0
I need to copy data from table 1 to table2
It is a pretty simple insert using a select. If you only need to do it once, you could skip the query and use the menu option for importing data, then just follow the prompts. Otherwise:
INSERT INTO table2
(col1, col2, col4, col5, col10)
SELECT col1, col2, col4, col5, 0
FROM table1;

SQL Query select col1, col2, if col3='xx' then 'yy' else col3, col4 col5??? is it posible?

well i want to do a query as this but i dont know if it is posible
Select
col1,
col2,
col3,
if(contain(col3,'somethingx')) then 'hello' else 'world' as col4,
col5
from table1
In other words, I want to select
col1
col2
col3
if col3 contains the word 'somethingx' then I want to select 'hello' as col4
Otherwise I want to select 'world' as col5 from my table.
SELECT
col1,
col2,
col3,
CASE WHEN col3 LIKE '%somethingx%' THEN 'hello' ELSE 'world' END AS col4,
col5
FROM table1