Output: https://www.dropbox.com/s/q9bjrzndbzj0l2i/test3.PNG?dl=0
How do I incorporate a query into the current code if I want to get only the Position (row_number) = 3 if lets say the Stud_ID = 4?
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s;
Thanks!!
Use a subselect over your query and filter it with your desired row number
SELECT *
FROM (
SELECT #row_num := #row_num + 1 AS `Position`, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s
CROSS JOIN (SELECT #row_num := 0) AS s1
) AS t
WHERE t.Position = 3
Related
Is there a way to get the last 3 records (red border)? Please check the screenshot.
Screenshot:
MySQL query.
SELECT icm_external_data.client_id,
icm_external_data.gsheet_tab AS source,
icm_external_data.type,
icm_external_data.name AS lead_name,
COUNT(DISTINCT icm_leads.email) AS on_month_leads,
date_format(icm_leads.date_created,'%M') AS month,
date_format(icm_leads.date_created,'%Y') AS YEAR,
#running_total := #running_total + COUNT(DISTINCT icm_leads.email) AS cumulative_sum
FROM icm_leads
LEFT JOIN icm_external_data ON icm_external_data.gsheet_id = icm_leads.gsheet_id
JOIN (SELECT #running_total := 0) r
WHERE icm_external_data.id = '29'
AND icm_leads.gsheet_id = 'xxx'
AND (icm_leads.tab_name = 'xxx' OR icm_leads.tab_name = 'xxx')
AND icm_leads.email NOT LIKE '%xxx%'
AND (icm_leads.`type` = 'forms' OR icm_leads.`type` = 'calls')
GROUP BY year(icm_leads.date_created),month(icm_leads.date_created)
You seem to want:
order by min(icm_leads.date_created) desc
limit 3 offset 1
Use the ORDER BY and LIMIT keywords.
SELECT icm_external_data.client_id,
icm_external_data.gsheet_tab AS source,
icm_external_data.type,
icm_external_data.name AS lead_name,
COUNT(DISTINCT icm_leads.email) AS on_month_leads,
date_format(icm_leads.date_created,'%M') AS month,
date_format(icm_leads.date_created,'%Y') AS YEAR,
#running_total := #running_total + COUNT(DISTINCT icm_leads.email) AS cumulative_sum
FROM icm_leads
LEFT JOIN icm_external_data ON icm_external_data.gsheet_id = icm_leads.gsheet_id
JOIN (SELECT #running_total := 0) r
WHERE icm_external_data.id = '29'
AND icm_leads.gsheet_id = 'xxx'
AND (icm_leads.tab_name = 'xxx' OR icm_leads.tab_name = 'xxx')
AND icm_leads.email NOT LIKE '%xxx%'
AND (icm_leads.`type` = 'forms' OR icm_leads.`type` = 'calls')
GROUP BY year(icm_leads.date_created),month(icm_leads.date_created)
ORDER BY icm_leads.date_created DESC
LIMIT 3 OFFSET 1
I have this kind of query that need to limit by 3 in each 'region' group but it doesnt run as I expected. The 'row_number' seems not arranged accordingly.
There must be some syntax I missed out or I didnt know. If anyone can help I dump the sql Here. MYSQL version 5.0
My query :
set #type = '';
set #num = 0;
SELECT locinvaisle.Area as ar,locinvaisle.Region as rg,custlist.CustomerName as cn,custlist.Custtype ct,
SUM(data2.quantity/1000) as mtcur,
#num := if(#type = locinvaisle.Region, #num + 1, 1) as dummy_1,
#type := locinvaisle.Region as dummy_2,
#num as row_number
FROM data2
INNER JOIN custlist ON data2.customeracc = custlist.Customeraccount
INNER JOIN locinvaisle ON data2.location = locinvaisle.Location
WHERE
date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01') AND date1 <= DATE_FORMAT('2018-06-11', '%Y-%m-31')
AND
data2.unit = 'KG'
AND
data2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')
AND
locinvaisle.Area = 'peninsular'
AND
custlist.Custtype = 'others'
GROUP BY locinvaisle.Region,custlist.CustomerName
HAVING row_number < 3
ORDER BY locinvaisle.Region,mtcur desc
Results :
Desired Results(From dummy database) :
You have to do the limiting as a full subquery ("derived table") before the group by is applied. Something along these lines:
SELECT
ar, rg, cn, ct, sum(quantity)
FROM (
SELECT
#row_num :=IF(#prev_value = locinvaisle.Region, #row_num + 1, 1)AS RowNumber
, locinvaisle.Area as ar
, locinvaisle.Region as rg
, custlist.CustomerName as cn
, custlist.Custtype ct
, data2.quantity
, #prev_value := locinvaisle.Region as dummy_2,
FROM data2
INNER JOIN custlist ON data2.customeracc = custlist.Customeraccount
INNER JOIN locinvaisle ON data2.location = locinvaisle.Location
CROSS JOIN (SELECT #row_num :=1, #prev_value :='') vars
WHERE date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01') AND date1 <= DATE_FORMAT('2018-06-11', '%Y-%m-31')
AND data2.unit = 'KG'
AND data2.customeracc not in (select Customeraccount from custlist WHERE Custcat = 'bcsb')
AND locinvaisle.Area = 'peninsular'
AND custlist.Custtype = 'others'
ORDER BY locinvaisle.Region,mtcur desc
) d
WHERE rowNumber <= 3
GROUP BY ar, rg, cn, ct
ORDER BY ar, rg, cn, ct
NOTE: You need to apply an ORDER BY within the subquery to facilitate the rownumber calculation, but despite this there is no guarantee that the final output will be in the desired order unless you apply a final order by clause.
Also please note that in future versions of MySQL (V8 and above) there should be a row_number() function and over() clause that can be used instead of the variables seen above.
In MySQL, you need to be careful when using variables. Two things are important:
Variables should be assigned and referenced in the same expression.
You need to be careful about the ordering of the result set.
So, try something like this:
SELECT x.*,
(#rn := IF(#r = la.Region, #num + 1,
IF(#r := la.Region, 1, 1)
)
) as rn
FROM (SELECT la.Area as ar, la.Region as rg, c.CustomerName as cn, c.Custtype ct,
SUM(data2.quantity/1000) as mtcur
FROM data2 d INNER JOIN
custlist c
ON d.customeracc = c.Customeraccount INNER JOIN
locinvaisle la
ON d.location = la.Location
WHERE date1 >= DATE_FORMAT('2018-06-11', '%Y-01-01') AND
date1 <= DATE_FORMAT('2018-06-11', '%Y-%m-31') AND
d.unit = 'KG' AND
d.customeracc not in (select Customeraccount from custlist cl WHERE cl Custcat = 'bcsb') AND
la.Area = 'peninsular' AND
c.Custtype = 'others'
GROUP BY la.Region, c.CustomerName
) x CROSS JOIN
(SELECT #r := 0, #rn := 0) params
HAVING rn <= 3;
trying to run below query, expecting to find a match. Since my two sub-queries are same, i am expecting to find a match, which is not happening here.
select TAB_1.RUL_IDD, TAB_2.RUL_IDD
FROM
( select aaa.RUL_ID RUL_IDD from TEST_1 aaa
inner join
(
select dt,#curRank := #curRank + 1 AS rank
from (
select distinct DATE(BTCH_RUN_DTTM) dt
FROM TEST_1
where ALRT_FLG_IND = 'Y'
and PASS_IND = 'N'
and SCH_RUN_IND = 'Y'
order by DATE(BTCH_RUN_DTTM) desc
) p,
(SELECT #curRank := 0) r
order by dt desc
) zz
where DATE(aaa.BTCH_RUN_DTTM) = zz.dt
and zz.rank=1
and aaa.ALRT_FLG_IND = 'Y'
and aaa.PASS_IND = 'N'
and aaa.SCH_RUN_IND = 'Y'
) TAB_1
left outer JOIN
(
select bbb.RUL_ID as RUL_IDD from TEST_1 bbb
inner join
(
select dt,#curRank := #curRank + 1 AS rank
from (
select distinct DATE(BTCH_RUN_DTTM) dt
FROM TEST_1
where ALRT_FLG_IND = 'Y'
and PASS_IND = 'N'
and SCH_RUN_IND = 'Y'
order by DATE(BTCH_RUN_DTTM) desc
) p,
(SELECT #curRank := 0) r
order by dt desc
) zzz
on DATE(bbb.BTCH_RUN_DTTM) = zzz.dt
and zzz.rank=1
and bbb.ALRT_FLG_IND = 'Y'
and bbb.PASS_IND = 'N'
and bbb.SCH_RUN_IND = 'Y'
) TAB_2
on TAB_2.RUL_IDD = TAB_1.RUL_IDD;
Result:
enter image description here
Not getting why its not giving the expected result. TAB_2.RUL_IDD should also have same value as TAB_1.RUL_IDD. Can experts help me over here?
I am trying to generate row number for each row selected from my database but it seems that the row number follows the sequence of the table before it's arranged (order by).
Actual table
https://www.dropbox.com/s/otstzak20yxcgt6/test1.PNG?dl=0
After query
https://www.dropbox.com/s/i9jaoy04vq6u2zh/test2.PNG?dl=0
Code
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
Pls help. Looking forward to receiving replies from yall. Thanks!
Try an inner select, so the row number will be generated after the ORDER BY like so:
SET #row_num = 0;
SELECT #row_num := #row_num + 1 as Position, s.*
FROM
(
SELECT
Student.Stud_ID, Student.Stud_Name, Student.Stud_Class, SUM(Grade.Percentage) AS Points
FROM Student, Student_Subject, Grade
WHERE Student.Stud_ID = Student_Subject.Stud_ID
AND Student_Subject.Stud_Subj_ID = Grade.Stud_Subj_ID
AND Student.Stud_Form = '1'
AND Grade.Quarter = '1'
GROUP BY Student.Stud_ID
ORDER BY Points DESC
) AS s;
I have this Query:
SET #row_num = 0;
SELECT
(SELECT #row_num := #row_num + 1) AS itempurchase_code,
(SELECT supplier_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY supplier_code ORDER BY COUNT(*) DESC LIMIT 1) AS supplier_code,
(SELECT user_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY user_code ORDER BY COUNT(*) DESC LIMIT 1) AS user_code,
22 AS status_code,
item_invoicecodesupplier AS item_invoicecodesupplier,
(SELECT itempurchase_date FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY itempurchase_date ORDER BY COUNT(*) DESC LIMIT 1) AS itempurchase_date
FROM qa_items_purchases b
GROUP BY (item_invoicecodesupplier)
ORDER BY itempurchase_code;
I get this Result:
If you look there is no (2) itempurchase_code column, What i can do to show the numbers in sequence?
You must add the row number in an outer query if your query contains a GROUP BY.
SET #row_num = 0;
SELECT (SELECT #row_num := #row_num + 1) AS itempurchase_code, *
FROM
(
SELECT ... -- your original query goes here
) AS T1
ORDER BY itempurchase_code