'Is there any alternate way to simply the queries
Is there any way to combine 2 SQL queries into a single query?
The table names used in both the SQL queries are same. The condition in the WHERE clause changes in both the queries.
Query 1:
SELECT ID
,SUM(time_duration) AS total_timespent
,COUNT(pno) AS ts_cnt
FROM (SELECT LEAD(create_ts) OVER (PARTITION BY ID ORDER BY CAST(pno AS INT)) AS R1,
DATEDIFF('second',create_ts::timestamp ,LEAD::timestamp) AS time_dur
FROM table_name
)A
WHERE item_no = '5672'
AND page_url_v11 IS NOT NULL
GROUP BY ID
Query 2:
SELECT ID
,SUM(time_duration) AS search_timespent
,COUNT(pno) AS search_cnt
from (SELECT LEAD(create_ts) OVER (PARTITION BY ID ORDER BY CAST(pno AS INT)) AS R1,
DATEDIFF('second',create_ts::timestamp ,LEAD::timestamp) AS time_dur
FROM table_name
)A
WHERE item_no = '5646'
GROUP BY ID
Solution 1:[1]
You could use Union operation to join statements together.
Solution 2:[2]
If you want duplicates use UNION ALL ,
If you don't want duplicates use UNION
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BugFreeDuck |
| Solution 2 | DontDownvote |
