Joining translate table, keeping order form indexes in SQLite - sql

I use this SQL to grab some indexes:
select follow
from unigram
where alternativeSpelling like 'test'
order by freq desc
limit 10;
I then convert them to words using this SQL, one by one:
select word
from wordIdxTranslate
where word_idx = <one of the indexes from above>
How do I combine these to a single query, whil epreserving the ranking order from the first query ("freq")?

Untested, but this should do it:
SELECT word
FROM unigram, wordIdxTranslate
WHERE
unigram.follow=wordIdxTranslate.word_idx
AND
unigram.follow IN (SELECT T1.follow
FROM unigram AS T1
WHERE T1.alternativeSpelling LIKE 'test'
ORDER BY T1.freq DESC
LIMIT 10)
ORDER BY freq DESC

One option would be to combine the queries with a join, like:
select word
from (
select follow
, freq
from unigram
where alternativeSpelling like 'test'
order by
freq desc
limit 10
) uni
join wordIdxTranslate wit
on wit.word_idx = uni.follow
order by
uni.freq desc

Related

rownum vs max in oracle

I have a couple of queries to get latest modified parent_id
with max keyword:
select max(parent_id)
from sample_table
where modified_date=(select max(modified_date)
FROM sample_table
where id = 'test') and id = 'test';
with rownum keyword:
select *
from (
select parent_id,modified_date
from sample_table
where id = 'test'
order by modified_date desc)
WHERE rownum <= 1 ;
Both the queries returning same and correct result.
Which one is better and faster query..
Your query is somewhat unpredictable because two records can have the same modified_date. So you have to apply a trick to return a single row only.
The first query is deterministic: It takes the latestd modified_date; if it returns several rows it takes the one with the highest parent_id. The second query is unpredicatable: it depends on how Oracle executes the query.
I would use the second query and modify it slightly to move the two order criteria close to each other:
select *
from (
select parent_id,modified_date
from sample_table
where id = 'test'
order by modified_date desc, parent_id desc)
WHERE rownum <= 1;
This type of query can also be better extended to return more columns, namely by adding it to the inner SELECT clause. In the other query, it's trickier.
You would say the best way is this one:
SELECT
MAX(parent_id) KEEP (DENSE_RANK FIRST ORDER BY modified_date desc, parent_id desc),
MAX(modified_date)
FROM sample_table
WHERE ID = 'test';

Squeryl order by multiple columns

I need to order by 2 fields:
SELECT * FROM item ORDER BY date ASC, sequence DESC;
Is it possible to emulate this SQL with Squeryl?
You can order by multiple fields. Just add them to the orderByclause as below:
from(table)(t => select(t) orderBy(t.date, t.sequence desc)

How to do union all with different order by conditions

I have a table with data like
I want all the records from the table but which the record having EnquiryStatus=1 and order by LastAttendendedDate should come on the top and remaining records should come after those records. I tried to select twice with where condition and tried to union all them, But with that union all not allowing me to order by on different ways. I can do it in c# by retriving the data as two table and merge them as single. But I want it in sql..
EDITS:
I want something like
Select * From EnquiryMaster A Where A.BranchID=16 and EnquiryStatus=1 ORDER BY A.CreatedDate Desc
UNION ALL
Select * From EnquiryMaster A Where A.BranchID=16 and EnquiryStatus in(0,2,3) ORDER BY EnquiryStatus,A.CreatedDate Desc
SELECT * FROM TABLE_NAME WHERE ORDER BY CASE WHEN EnquiryStatus='1' THEN LastAttendendedDate END DESC
You can use this in sql.
Try this
select * from your_table
order by case when EnquiryStatus=1 then LastAttendendedDate end DESC

SQL optimization : select distinct from one table

Suppose I have got a SQL like this
select distinct CUSTOMER_PRODUCT_ORDER as ORDER
from MASTER_LIST
where PROJECT_ID = "ABCDD"
order by ORDER ASC
how can I optimize the performance of running this sql ?
I think that with a group by would be better and more performant in most cases :
select CUSTOMER_PRODUCT_ORDER as ORDER
from MASTER_LIST
where PROJECT_ID = "ABCDD"
GROUP BY CUSTOMER_PRODUCT_ORDER
order by ORDER ASC

Sorting by some column and also by rand() in MySQL

Is it possible to sort a result set by some column and also by RAND()?
For example:
SELECT `a`, `b`, `c`
FROM `table`
ORDER BY `a` DESC, RAND()
LIMIT 0, 10
Thank you.
What you are doing is valid - it will order the results in descending order by a but randomize the order of ties.
However to do what you want you need to first use a subquery to get the latest 100 records and then afterwards sort the results of that subquery randomly using an outer query:
SELECT * FROM
(
SELECT * FROM table1
ORDER BY date DESC
LIMIT 100
) T1
ORDER BY RAND()
I know this is old but I discovered ... Example:
select photo.sort_order,
profile.description is not null as desc_order,
profile.description,
rand() as r
from
photo photo, profile profile
order by
photo.sort_order desc,
desc_order desc,
r ASC