oracle sql group by column which count them - sql

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

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.

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 Server: SELECT IF conditionally

I have a table with different columns and I need to show different based on the value of a specific column.
My table It's like this:
select col1, col2, col3, col4, col5, col6
from table1
So, for example:
if col1 = '1' --> show col3 and col4.
if col2 = '1' --> show col5 and col6
Use case expressions to chose columns to return. Note that a case's return types must be compatible, i.e. col3 and col5, and col4 and col6.
select case when col1 = 1 then col3
when col2 = 1 then col5
end,
case when col1 = 1 then col4
when col2 = 1 then col6
end
from tablename
Or, do a UNION ALL:
select col3, col4
from tablename where col1 = 1
union all
select col5, col6
from tablename where col2 = 1
Remaining questions - what do to if both col1 and col2 are 1? Or if none of them are?
Rule of thumb 1 : return to the caller the columns used for filtering.
Rule of thumb 2 : don't use union all, it returns duplicates and your result will not be a 'relation' (though in this case there would be no duplicates because col1 is a key but I still think union states the intent best).
select col1, col2, col3 AS colum_a, col4 AS colum_b
from table1
where col1 = 1
union
select col1, col2, col5 AS colum_a, col6 AS colum_b
from table1
where col2 = 1;

Divide two columns into group by function 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

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