Combine two statements with LIMITS using UNION - sql

Is there a way to combine these two statements into one without having duplicate entries?
SELECT * FROM Seq where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP)
order by TimeP limit 50
SELECT * FROM Seq where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)
order by TimeI limit 50
My first, obvious attempt is not supported by SQLITE (Syntax error: Limit clause should come after UNION not before):
SELECT * FROM Seq where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP)
order by TimeP limit 50
UNION
SELECT * FROM Seq where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)
order by TimeI limit 50

Use subqueries and perform the limit within them.
SELECT *
FROM ( SELECT *
FROM Seq
WHERE JULIANDAY('2012-05-25 19:02:00') <= JULIANDAY(TimeP)
ORDER BY TimeP
LIMIT 50
)
UNION
SELECT *
FROM ( SELECT *
FROM Seq
WHERE JULIANDAY('2012-05-29 06:20:50') <= JULIANDAY(TimeI)
ORDER BY TimeI
LIMIT 50
)

Queries are processed in stages:
FROM clause and all the joins;
WHERE clause and all the predicates. So if you whant to see NULL values in the result set, you should never filter OUTER-joined table columns in the WHERE section, as this will turn your query into INNER join;
GROUP BY and HAVING clause;
Query combinations: UNION, INTERSECT, EXCEPT or MINUS
ORDER BY
LIMIT
Therefore, as others pointed out, it is syntatically wrong to use ORDER BY and LIMIT before UNION clause. You should use subqueries:
SELECT *
FROM (SELECT * FROM Seq
WHERE JULIANDAY('2012-05-25 19:02:00') <= JULIANDAY(TimeP)
ORDER BY TimeP LIMIT 50) AS tab1
UNION
SELECT *
FROM (SELECT * FROM Seq
WHERE JULIANDAY('2012-05-29 06:20:50') <= JULIANDAY(TimeI)
ORDER BY TimeI LIMIT 50) AS tab2;

SELECT * from
(SELECT *
FROM Seq
where JULIANDAY('2012-05-25 19:02:00')<=JULIANDAY(TimeP)
order by TimeP limit 50)
UNION
SELECT * from
(SELECT *
FROM Seq
where JULIANDAY('2012-05-29 06:20:50')<=JULIANDAY(TimeI)
order by TimeI limit 50)

I have a table buysell_product. I want to select top 5 based on views_1 column and another top 5 using views_2 column, then merge the two. Columns are the same in both queries. Expecting this to work, it does not:
SELECT id, name, views_1, views_2
FROM buysell_product
ORDER BY views_1 DESC
LIMIT 5
UNION
SELECT id, name, views, views_2
FROM buysell_product as b
ORDER BY views_2 DESC
LIMIT 5
Error:
Execution finished with errors.
Result: ORDER BY clause should come after UNION not before
At line 1:
...
They work separately but I need to merge them:
SELECT * FROM (
SELECT id, views_1, views_2, name
FROM buysell_product
ORDER BY views_1 DESC
LIMIT 5
)
UNION
SELECT * FROM (
SELECT id, views_1, views_2, name
FROM buysell_product as b
ORDER BY views_2 DESC
LIMIT 5
)
Result:
id
views_1
views_2
name
2
41
16
Excellent 2013 ford ecosport
3
72
10
Excellent Hyundai creta
5
39
39
iPhone 11 128gb
7
12
84
Excellent Hyundai creta sx
9
37
84
Volkswagen Polo 1.2 GT AMT 2017
44
34
81
Usupso Massage Anti Skid Slippers
45
15
75
Garlic Powder - 100Gm
57
35
11
Iphone 13 and 14
67
15
73
Universal Touch Screen Capacitive Stylus

Related

stratified sample on ranges

I have table_1, that has data such as:
Range Start Range End Frequency
10 20 90
20 30 68
30 40 314
40 40 191 (here, it means we have just 40 as data point repeating 191 times)
table_2:
group value
10 56.1
10 88.3
20 53
20 20
30 55
I need to get the stratified sample on the basis of range from table_1, the table_2 can have millions of rows but the result should be restricted to just 10k points.
Tried below query:
SELECT
d.*
FROM
(
SELECT
ROW_NUMBER() OVER(
PARTITION BY group
ORDER BY group
) AS seqnum,
COUNT(*) OVER() AS ct,
COUNT(*) OVER(PARTITION BY group) AS cpt,
group, value
FROM
table_2 d
) d
WHERE
seqnum < 10000 * ( cpt * 1.0 / ct )
but a bit confused with the analytics functions usage here.
Expecting 10k records as a stratified sample from table_2:
Result table:
group value
10 56.1
20 53
20 20
30 55
It means you need atleast one record of each group and more records on random basis then try this:
SELECT GROUP, VALUE FROM
(SELECT T2.GROUP, T2.VALUE,
ROW_NUMBER()
OVER (PARTITION BY T2.GROUP ORDER BY NULL) AS RN
FROM TABLE_1 T1
JOIN TABLE_2 T2
ON(T1.RANGE = T2.GROUP))
WHERE RN = 1 OR
CASE WHEN RN > 1
AND RN = CEIL(DBMS_RANDOM.VALUE(1,RN))
THEN 1 END = 1
FETCH FIRST 10000 ROWS ONLY;
Here, Rownum is taken on random basis for each group and then result is taking rownum 1 and other rownum if they fulfill random condition.
Cheers!!
If I understand what you want - which is by no means certain - then I think you want to get a maximum of 10000 rows, with the number of group values proportional to the frequencies. So you can get the number of rows you want from each range with:
select range_start, range_end, frequency,
frequency/sum(frequency) over () as proportion,
floor(10000 * frequency/sum(frequency) over ()) as limit
from table_1;
RANGE_START RANGE_END FREQUENCY PROPORTION LIMIT
----------- ---------- ---------- ---------- ----------
10 20 90 .135746606 1357
20 30 68 .102564103 1025
30 40 314 .473604827 4736
40 40 191 .288084465 2880
Those limits don't quite add up to 10000; you could go slightly above with ceil instead of floor.
You can then assign a nominal row number to each entry in table_2 based on which range it is in, and then restrict the number of rows from that range via that limit:
with cte1 (range_start, range_end, limit) as (
select range_start, range_end, floor(10000 * frequency/sum(frequency) over ())
from table_1
),
cte2 (grp, value, limit, rn) as (
select t2.grp, t2.value, cte1.limit,
row_number() over (partition by cte1.range_start order by t2.value) as rn
from cte1
join table_2 t2
on (cte1.range_end > cte1.range_start and t2.grp >= cte1.range_start and t2.grp < cte1.range_end)
or (cte1.range_end = cte1.range_start and t2.grp = cte1.range_start)
)
select grp, value
from cte2
where rn <= limit;
...
9998 rows selected.
I've used order by t2.value in the row_number() call because it isn't clear how you want to pick which rows in the range you actually want; you might want to order by dbms_random.value or something else.
db<>fiddle with some artificial data.

Oracle random segments select

I would like to select the following segment.
Random 5500 rows including the following segments:
Subcategorie (sex): - 3300 men
- 2200 women
Subcategorie (age): - 2140 between 18-34 years
- 2100 between 35-54 years
- 1260 between 55-99 years
How could I solve this in a select statement?
The problem is, you use the word "random" but you have a very precise break down of cohorts by age and sex. A truly random single query won't produce such exact quotas. So your query must necessarily be complicated: you need to divide the whole table into subsets which meet your constraints then randomly select from those subsets. Something like this...
select * from (
select * from whatever
where sex = 'M'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 1284
union all
select * from (
select * from whatever
where sex = 'M'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 1260
union all select * from (
select * from whatever
where sex = 'M'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 756
union all
select * from (
select * from whatever
where sex = 'F'
and age between 18 and 34
order by dbms_random.value
)
where rownum <= 856
union all
select * from (
select * from whatever
where sex = 'F'
and age between 35 and 54
order by dbms_random.value
)
where rownum <= 840
union all select * from (
select * from whatever
where sex = 'F'
and age between 55 and 99
order by dbms_random.value
)
where rownum <= 504
This may perform poorly, depending on the usual factors - size of table, indexing, etc - but it will produce those exact cohorts.
In case it's not obvious, the rownum bounds are the number of hits in each age group multiplied by the ratio of men to women (3:2).

How can I select top 3 for each group based on another column in sqlite?

I'm trying to get top 3 most profitable UserIDs in each country in one table using sqlite. I'm not sure where to use LIMIT 3.
Here is the table I have:
Country | UserID | Profit
US 1 100
US 12 98
US 13 10
US 5 8
US 2 5
IR 9 95
IR 3 90
IR 8 70
IR 4 56
IR 15 40
the result should look like this:
Country | UserID | Profit
US 1 100
US 12 98
US 13 10
IR 9 95
IR 3 90
IR 8 70
One pretty simple method is:
select t.*
from t
where t.profit >= (select t2.profit
from t t2
where t2.country = t.country
order by t2.profit desc
limit 1 offset 2
);
This assumes at least three records for each country. You can get around that with coalesce():
select t.*
from t
where t.profit >= coalesce((select t2.profit
from t t2
where t2.country = t.country
order by t2.profit desc
limit 1 offset 2
), t.profit
);
Since SQLite doesn't support windows function, so you can write a subquery be a seqnum by Country, then get top 3
You can try this query.
select t.Country,t.UserID,t.Profit
from(
select t.*,
(select count(*)
from T t2
where t2.Country = t.Country and t2.Profit >= t.Profit
) as seqnum
from T t
)t
where t.seqnum <=3
sqlfiddle:https://www.db-fiddle.com/f/tmNhRLGG2oKqCKXJEDsjfe/0
LIMIT won't be usefull as it applies to a whole result set.
I would create an auxiliary column "CountryRank" like this:
SELECT *, (SELECT COUNT() FROM Data AS d WHERE d.Country=Data.Country AND d.Profit>Data.Country)+1 AS CountryRank
FROM Data;
And query on that result:
SELECT Country, UserID, Profit
FROM (
SELECT *, (SELECT COUNT() FROM Data AS d WHERE d.Country=Data.Country AND d.Profit>Data.Profit)+1 AS CountryRank FROM Data)
WHERE CountryRank<=3
ORDER BY Country, CountryRank;

Accumulate a summarized column

I could need some help with a SQL statement. So I have the table "cont" which looks like that:
cont_id name weight
----------- ---------- -----------
1 1 10
2 1 20
3 2 40
4 2 15
5 2 20
6 3 15
7 3 40
8 4 60
9 5 10
10 6 5
I then summed up the weight column and grouped it by the name:
name wsum
---------- -----------
2 75
4 60
3 55
1 30
5 10
6 5
And the result should have a accumulated column and should look like that:
name wsum acc_wsum
---------- ----------- ------------
2 75 75
4 60 135
3 55 190
1 30 220
5 10 230
6 5 235
But I didn't manage to get the last statement working..
edit: this Statement did it (thanks Gordon)
select t.*,
(select sum(wsum) from (select name, SUM(weight) wsum
from cont
group by name)
t2 where t2.wsum > t.wsum or (t2.wsum = t.wsum and t2.name <= t.name)) as acc_wsum
from (select name, SUM(weight) wsum
from cont
group by name) t
order by wsum desc
So, the best way to do this is using cumulative sum:
select t.*,
sum(wsum) over (order by wsum desc) as acc_wsum
from (<your summarized query>) t
The order by clause makes this cumulative.
If you don't have that capability (in SQL Server 2012 and Oracle), a correlated subquery is an easy way to do it, assuming the summed weights are distinct values:
select t.*,
(select sum(wsum) from (<your summarized query>) t2 where t2.wsum >= t.wsum) as acc_wsum
from (<your summarized query>) t
This should work in all dialects of SQL. To work with situations where the accumulated weights might have duplicates:
select t.*,
(select sum(wsum) from (<your summarized query>) t2 where t2.wsum > t.wsum or (t2.wsum = t.wsum and t2.name <= t.name) as acc_wsum
from (<your summarized query>) t
try this
;WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY wsum) rownum
FROM #table1
)
SELECT
c1.name,
c1.wsum,
acc_wsum= (SELECT SUM(c2.wsum)
FROM cte c2
WHERE c2.rownum <= c1.rownum)
FROM CTE c1;
or you can join instead of using subquery
;WITH CTE
AS
(
SELECT *,
ROW_NUMBER() OVER(ORDER BY usercount) rownum
FROM #table1
)
SELECT
c1.name,
c1.wsum,
acc_wsum= SUM(c2.wsum)
FROM CTE c1
INNER JOIN CTE c2 ON c2.rownum <= c1.rownum
GROUP BY c1.name, c1.wsum;

Sql Server Max function on multiple columns?

Below is my table data
Mat Phy Chem
20 30 40
25 35 35
45 30 30
45 40 35
I want to retrieve top 3 max rows of all the three columns in a single row.
O/P
Mat Phy Chem
45 40 40
25 35 35
20 30 30
I have used below query but was unsuccessful, please help...
Select distinct top 3 max(mat) from studata group by mat order by max(mat) desc
Union all
Select distinct top 3 max(phy) from studata group by phy order by max(phy) desc
Union all
Select distinct top 3 max(chem) from studata group by chem order by max(chem) desc
WITH q AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY mat DESC) AS rn_mat,
ROW_NUMBER() OVER (ORDER BY phy DESC) AS rn_phy,
ROW_NUMBER() OVER (ORDER BY chem DESC) AS rn_chem
FROM mytable
)
SELECT q_mat.mat, q_phy.phy, q_chem.chem
FROM q q_mat
JOIN q q_phy
ON q_phy.rn_phy = q_mat.rn_mat
JOIN q q_chem
ON q_chem.rn_chem = q_phy.rn_phy
WHERE q_mat.rn_mat <= 3
Does this work?
select distinct top 3 Mat
from studata
order by Mat desc
select distinct top 3 Phy
from studata
order by Phy desc
select distinct top 3 Chem
from studata
order by Chem desc
The reason you're probably getting a problem is because max is a function (it will only ever return 1 thing) so Select distinct top 3 max(mat) is nonsense.