with clause in union query - sql

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

Related

Slowness in SQL query with subquery

My SQL query becomes too slow when a subquery is added in WHERE clause even though the individual run times of the queries is less than 1 minute.
The query has the following skeleton
SELECT COL1, COL2, COL3, COL4, COL5, COL6, sum(COL7) FROM TABLE1
WHERE Col1 = 'something' AND COl2 = date AND Col3 = (SELECT MAX(COLUMN1) FROM TABLE2)
GROUP BY COL1, COL2, COL3, COL4, COL5, COL6
This query is running on SYBASE IQ.
Data for table 1 is 60M+ rows and post application of filter conditions is just 60 rows that usually takes 50 sec to run if subquery is replaced with hardcoded value.
Data for table 2 is 200 rows and post application of filter condition is just one integer value that individually takes 1 sec to run.
Move the subquery to the FROM clause:
SELECT t1.COL1, t1.COL2, t1.COL3, t1.COL4, t1.COL5, t1.COL6, sum(t1.COL7)
FROM TABLE1 t1 JOIN
(SELECT MAX(COLUMN1) as max_column1 FROM TABLE2) t2
ON t2.max_column1 = t1.date
WHERE Col1 = 'something' AND COl2 = date
GROUP BY COL1, COL2, COL3, COL4, COL5, COL6 ;
Then you want indexes on:
table1(col1, col2, date)
table2(column1).
You could move the subquery to the select and then use a join clause and see if that helps.
SELECT max(COLUMN1), COL1, COL2, COL3, COL4, COL5, COL6, sum(COL7)
FROM TABLE1 t1 join TABLE2 t2
on t1.Col3 = t2.COLUMN1
WHERE Col1 = 'something' AND COl2 = date
GROUP BY COLUMN1, COL1, COL2, COL3, COL4, COL5, COL6
Not that you are using parameters but it sounds weirdly like parameter sniffing.

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

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