MYSQL Order By Sequence - mysql

I need help with mysql order by, i have this query
SELECT
videos_views.videos_views_id,
videos_views.videos_views_date,
SUM(IF(MONTH(videos_views.videos_views_date) = 2, videos_views.videos_views_total, 0)) AS total_view,
videos.videos_id,
videos.videos_title,
videos.videos_description,
videos.videos_author_list_id,
author_list.author_list_name,
author_list.author_list_id
FROM videos
LEFT OUTER JOIN author_list
ON videos.videos_author_list_id = author_list.author_list_id
LEFT OUTER JOIN videos_views
ON videos.videos_id = videos_views.videos_views_id
WHERE author_list.author_list_video_type = 1
AND videos.videos_id >= 51108
GROUP BY videos_views.videos_views_id,
videos.videos_id
ORDER BY CASE WHEN MONTH(videos_views.videos_views_date) = 2 THEN SUM(videos_views.videos_views_total) END DESC,
CASE WHEN MONTH(videos_views.videos_views_date) <> 2 THEN videos.videos_id END DESC
LIMIT 11
And it returns the following results
If i use the first order by using DESC or ASC returns the same result above.
And i use the second order by using ASC returns the result bellow:
And I need the query to return the values in the following order bellow
Please Help me

Your results suggest that you want:
order by total_views desc, videos_id desc
You can reference column aliases (safely) in the order by.

Related

Error while compiling statement: FAILED: SemanticException [Error 10002]

select d.order_type from migu_td_aaa_order_log_d d where exists(select 1
from migu_user r where r.user_id = '156210106' and r.user_num =
d.serv_number) and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
why the above sql failed ,indicates :
FAILED: SemanticException [Error 10002]: Line 4:11 Invalid column reference 'opr_time'
but the below one works :
select temp.order_type from (
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' ) order by
d.opr_time desc limit 1) temp;
this one works fine ,too ,and much more efficient than the second one:
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1
I only need to get order_type field,so even though the second one works,but it cost much more time.
Can anyone help me?
Thanks a lot!
Your first query does not work because, in the first select statement, you are just getting one column (d.order_type), but you are trying to order by another column (d.opr_time), which you have not included in your select statement
select d.order_type from ...
...
order by d.opr_time desc limit 1
Note that if you added the column d.opr_time to your first query, it would work:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Your second query works because, in the subquery, you have selected all the columns of d (d.*), so when you order by opr_time, that column is present. (Same for the third query).
select temp.order_type from (
select d.* ... order by d.opr_time ...
EDITED:
According to the Hive documentation:
When using group by clause, the select statement can only include
columns included in the group by clause. Of course, you can have as
many aggregation functions (e.g. count) in the select statement as
well.
So, this query:
select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1
Shouldn't work either, because the select clause has an additional column (d.order_type) that is not included in the group by clause.
I hope this helps.
P.S. This answer about SQL execution order might be useful.
1.
Hive currently have an order by limitation.
The current status of this issue is PATCH AVAILABLE.
see -
"Can't order by an unselected column"
https://issues.apache.org/jira/browse/HIVE-15160
2.
You might want to get familiar with LEFT SEMI JOIN which is a cleaner syntax for EXISTS
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Joins#LanguageManualJoins-JoinSyntax
3.
using min / max over a struct / named_struct can be used instead of order by ... asc / desc and limit 1
Here is an alternative solution:
select max(named_struct('opr_time',d.opr_time,'order_type',d.order_type)).order_type
from migu_td_aaa_order_log_d d
left semi join migu_user r
on r.user_num =
d.serv_number
and r.user_id = '156210106'
where d.product_id in ('2028594290','2028596512','2028597138')
;
P.s.
You seriously want to consider to treat IDs (user_id, product_id) as numeric and not as strings.

MySQL Group by, I want to group by 'shrn_trpr_nm' and Order By

SELECT
ptl_id,chnl_id, shrn_srno, shrn_trpr_nm
FROM schd_shrn_infm
WHERE
ptl_id = 'PTL_51'
AND chnl_id = 'CHNL_1'
AND schd_srno = 100000001480
ORDER BY
shrn_trpr_nm
LIMIT 10
I want grouop by shrn_trpr_nm, what should I do?
To also return the other columns, you're going to have to get an aggregated version of them, for example max():
SELECT
max(ptl_id) ptl_id, -- use max()
max(chnl_id) chnl_id,
max(shrn_srno) shrn_srno,
shrn_trpr_nm
FROM schd_shrn_infm
WHERE ptl_id = 'PTL_51'
AND chnl_id = 'CHNL_1'
AND schd_srno = 100000001480
GROUP BY shrn_trpr_nm -- add this line
ORDER BY shrn_trpr_nm
LIMIT 10
Since the other are columns fixed via the WHERE clause, max() will be fine.
can't understated where is the problem
just put the field in the order and in the group
SELECT
ptl_id,chnl_id, shrn_srno, shrn_trpr_nm
FROM WHERE
ptl_id = 'PTL_51'
AND chnl_id = 'CHNL_1'
AND schd_srno = 100000001480
GROUP BY
shrn_trpr_nm
ORDER BY
shrn_trpr_nm
LIMIT 10

Multiple Order By in MySql Query

I am developing a voting system.
I am facing issue in ordering.
Basically i want to get top ranking and maximum nominee first. I used "totalUserVoted" and "totalRating" in Descending order on both condition but my query ordering "totalUserVoted".
I am expecting the result in this order.
Here is my sql query.
SELECT
(SELECT (((SUM(`design`)*4)+(SUM(`usability`)*3)+(SUM(`creativity`)*2)+(SUM(`content`))*1))/ count(`nominee_id`) / 10
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalRating,
(SELECT count(`nominee_id`)
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalUserVoted,
`sk_award_nominee`.*,
`sk_user`.`username`,
`sk_user`.`email`,
`sk_user_profile`.`f_name`,
`sk_user_profile`.`m_name`,
`sk_user_profile`.`l_name`,
`sk_user_profile`.`address`
FROM `sk_award_nominee`
LEFT JOIN `sk_user` ON `sk_user`.`user_id`=`sk_award_nominee`.`user_id`
LEFT JOIN `sk_user_profile` ON `sk_award_nominee`.`user_id`=`sk_user_profile`.`user_id`
WHERE `sk_award_nominee`.`status` = 1
AND DATE(approval_date) = '2016-02-22'
ORDER BY `totalUserVoted` DESC,
`totalRating` DESC
Something like this ?
ORDER BY totalUserVoted DESC, totalRating DESC
PHP MySQL Order by Two Columns

Group and order by a column but donot include that column in results

I've been trying to figure out how I can modify this query so that the result set does not include the numHits. I want the same results in the same order, just not have the numHits included.
SELECT
`newel_inventoryKeywordIdDictionaryId`.`inventoryId`
,COUNT(`newel_inventoryKeywordIdDictionaryId`.`inventoryId`) as numHits
FROM
`newel_inventoryKeywordIdDictionaryId`
, `newel_inventoryDictionary`
WHERE
`newel_inventoryKeywordIdDictionaryId`.`dicId` = `newel_inventoryDictionary`.`dicId`
AND (
`newel_inventoryDictionary`.`word` = 'alabaster' OR `newel_inventoryDictionary`.`word` = 'chess'
)
GROUP BY inventoryId
ORDER BY numHits DESC;
sample results:
inventoryId, numHits
6928, 2
6929, 2
6924, 2
6925, 2
13772, 2
6926, 2
18203, 1
6931, 1
13863, 1
18402, 1
Desired Results:
inventoryId
6928
6929
6924
6925
13772
6926
18203
6931
13863
18402
Move the column from SELECT clause to ORDER BY clause:
SELECT
`newel_inventoryKeywordIdDictionaryId`.`inventoryId`
FROM
`newel_inventoryKeywordIdDictionaryId`
, `newel_inventoryDictionary`
WHERE
`newel_inventoryKeywordIdDictionaryId`.`dicId` = `newel_inventoryDictionary`.`dicId`
AND (
`newel_inventoryDictionary`.`word` = 'alabaster' OR `newel_inventoryDictionary`.`word` = 'chess'
)
GROUP BY inventoryId
ORDER BY COUNT(`newel_inventoryKeywordIdDictionaryId`.`inventoryId`) DESC;
SELECT
`newel_inventoryKeywordIdDictionaryId`.`inventoryId`
FROM
`newel_inventoryKeywordIdDictionaryId`
, `newel_inventoryDictionary`
WHERE
`newel_inventoryKeywordIdDictionaryId`.`dicId` = `newel_inventoryDictionary`.`dicId`
AND (
`newel_inventoryDictionary`.`word` = 'alabaster' OR `newel_inventoryDictionary`.`word` = 'chess'
)
GROUP BY inventoryId
ORDER BY COUNT(`newel_inventoryKeywordIdDictionaryId`.`inventoryId`) DESC;
You just need to put the aggregation in the ORDER BY. However, you should also:
Use explicit join syntax. Never use commas in the from clause.
Use table aliases. They make queries easier to write and to read.
Use in instead of a bunch of or statements.
Here is an improved version of the query:
SELECT kdi.inventoryId
FROM newel_inventoryKeywordIdDictionaryId kdi JOIN
newel_inventoryDictionary id
ON kdi.dicId = id.dictId
WHERE id.word IN ('alabaster', 'chess')
GROUP BY kdi.inventoryId
ORDER BY COUNT(*) DESC;

I am trying this QUERY, and return this weird error,please help me

select tbl_c_food_veg.pk_veg_id,tbl_c_food_veg.var_desc,tbl_c_food_veg.var_title ,tbl_c_food_veg_img.var_img,( select tbl_c_food_non_veg.pk_non_veg_id,tbl_c_food_non_veg.var_desc,tbl_c_food_non_veg.var_title,tbl_c_food_non_veg_img.var_img from tbl_c_food_non_veg left join tbl_c_food_non_veg_img on tbl_c_food_non_veg.pk_non_veg_id=tbl_c_food_non_veg_img.fk_non_veg_id where
tbl_c_food_non_veg.fk_cat_id=8 and tbl_c_food_non_veg.pk_non_veg_id!=0 group by tbl_c_food_non_veg_img.fk_non_veg_id order by tbl_c_food_non_veg.pk_non_veg_id desc limit 2 ) as non_veg,( select tbl_c_food_drinks.pk_drinks_id, tbl_c_food_drinks.var_desc, tbl_c_food_drinks.var_title, tbl_c_food_drinks_img.var_img from tbl_c_food_drinks left join tbl_c_food_drinks_img on tbl_c_food_drinks.pk_drinks_id=tbl_c_food_drinks_img .fk_drinks_id where
tbl_c_food_drinks.fk_cat_id=8 and tbl_c_food_drinks.pk_drinks_id!=0 group by tbl_c_food_drinks_img.fk_drinks_id order by tbl_c_food_drinks.pk_drinks_id desc limit 2 ) as drinks from tbl_c_food_veg left join tbl_c_food_veg_img on tbl_c_food_veg.pk_veg_id=tbl_c_food_veg_img.fk_veg_id where
tbl_c_food_veg.fk_cat_id=8 and tbl_c_food_veg.pk_veg_id!=0 group by tbl_c_food_veg_img.fk_veg_id order by tbl_c_food_veg.pk_veg_id desc limit 2
In your nested sub queries for retrieving non_veg and drinks you use limit 2 so most likely the subquery will return 2 rows, but you can only use one row. Change the limit to limit 1
and try using aliases as Barranka says it wil be much easier to read your query