I am trying to group by two columns they are dependent each other
Example:
id date status
1 2016-03-04 0
2 2016-03-05 0
3 2016-03-04 1
4 2016-03-04 0
5 2016-03-05 1
Result:
id date status
1 2016-03-04 0
2 2016-03-04 0
3 2016-03-04 1
4 2016-03-05 0
5 2016-03-05 1
I tried order by datewise and stautswise (ex show 0 status record first after that another date)
pls help me
I think you are just looking for a proper order by:
select t.*
from t
order by date, status;
If you want to calculate the id, then variables will help:
select (#rn := #rn + 1) as id, t.*
from t cross join
(select #rn := 0) params
order by date, status;
As i am getting your requirement it work ......
select min(e1.id),min(e1.date),e1.status
from example
inner join result r1 on e1.id =r1.id
group by e1.status
if its not your requirement then please let me know i ll update as per your
requirement.
Use only Order By Clause with both column
SELECT *
FROM tblname
Order By datecol,status
http://sqlfiddle.com/#!9/f8b55/1
Related
I have the following table.
id
user_id
file_id
completed
updated
1
161
10
1
2022-10-11
2
164
11
1
2022-10-12
3
161
10
1
2022-10-12
4
167
10
1
2022-10-10
5
167
10
1
2022-10-11
6
167
10
1
2022-10-12
I want to select the row for each user having the max updated date for each file_id.
SELECT * FROM user_file
WHERE updated = (SELECT uf.updated FROM user_file uf GROUP BY uf.user_id,uf.file_id)
I have come up with this query but it returns an error "Subquery returns more than 1 row"
Maybe you want to check these out?
SQL select only rows with max value on a column
Retrieving the last record in each group - MySQL
Personally prefer the window function solution
SELECT a.*
FROM (SELECT id, user_id, file_id, ...
ROW_NUMBER() OVER (PARTITION BY file_id ORDER BY updated DESC) ranked_order
FROM user_file) a
WHERE a.ranked_order = 1
Okay so lets say I have a basic table
thing
id
user_id
created_at
And some data
id user_id created_at
1 1 2016-09-06
2 1 2016-09-06
3 1 2016-09-06
4 1 2016-09-07
5 1 2016-09-08
6 1 2016-09-08
7 1 2016-09-08
What I want to achive is selecting max two rows per USER per DATE of created_at. I'm only displaying data from one user, but I hope you get the point.
So the results of the select should be
id user_id created_at
1 1 2016-09-06
2 1 2016-09-06
4 1 2016-09-07
5 1 2016-09-08
6 1 2016-09-08
I know I somehow have to use the LIMIT keyword, but I'm not so sure how. I'm also pretty sure I have to use a subquery and group by the date.
I hope you understand the problem and please do ask some questions if there's something difficult to understand.
One way is to use variables:
SELECT id, user_id, created_at
FROM (
SELECT id, user_id, created_at,
#rn := IF(#dt = created_at, #rn + 1,
IF(#dt := created_at, 1, 1)) AS rn
FROM mytable
CROSS JOIN (SELECT #rn := 0, #dt := '1900-01-01') AS var
ORDER BY created_at) AS t
WHERE t.rn <= 2
Demo here
I am trying to get the second last records use mysql.
I did some research, some sample has fix gap between numbers or date. But my situation is that the contract_id is not always +1 from the previous one. Anyone ideas? Thank you so much.
merchant_id contract_id start_date
10 501 2016-05-01
10 506 2016-06-01
13 456 2015-12-01
13 462 2016-01-01
14 620 2016-06-01
14 642 2016-07-01
14 656 2016-07-05
merchant_id Second_last_contract_id
10 501
13 456
14 642
contract_id != previous contract_id + X. (The X is not fixed)
'start_date' tell us the contracts creating order.
Here's one option using user-defined variables to establish a row number per group of merchants and then filtering on the 2nd in each group ordered by contracts:
select *
from (
select *,
#rn:=if(#prevMerchantId=merchantid,
#rn+1,
if(#prevMerchantId:=merchantid, 1, 1)
) as rn
from yourtable cross join (select #rn:=0, #prevMerchantId:=null) t
order by merchantId, contractid desc
) t
where rn = 2
SQL Fiddle Demo
Here's another option, filtering the results of GROUP_CONCAT() using SUBSTRING_INDEX():
SELECT merchant_id,
SUBSTRING_INDEX(SUBSTRING_INDEX(
GROUP_CONCAT(contract_id ORDER BY start_date DESC),
',', 2), ',', -1) AS Second_last_contract_id
FROM the_table
GROUP BY merchant_id
See it on sqlfiddle.
I have data in a table like this:
fgid qty ntid
1 100 10
2 90 10
6 200 11
1 80 11
1 120 12
6 100 12
6 30 13
And i make query :
SELECT fgid, SUM(qty) AS total_qty, COUNT(ntid) AS nt_count FROM sofg
GROUP BY fgid
AND the result is :
fgid total_qty nt_count
1 300 3
2 90 1
6 330 3
Then i want to make the result like this :
no fgid total_qty nt_count
1 1 300 3
2 2 90 1
3 6 330 3
How to do that with a query? where 'no' is (like) autoincrement number.
Try this query.
SELECT
#rownum := #rownum + 1 rownum,
t.*
FROM (SELECT #rownum:=0) r,
(
SELECT fgid, SUM(qty) AS total_qty, COUNT(ntid) AS nt_count FROM sofg GROUP BY fgid
) t;
Basically the same as Dhinakaran's answer, but there's no need to put the whole main query into a subquery. There's no difference to his answer appart from maybe being more pleasing to the eye, but please accept Dhinakaran's answer, as he was faster.
SELECT
#rownum:=#rownum + 1 as rownumber,
fgid,
SUM(qty) AS total_qty,
COUNT(ntid) AS nt_count
FROM sofg
, (select #rownum:=0) v
GROUP BY fgid
My Data is
ID SCORE
1 55
1 -1
1 25
1 -1
1 -1
1 35
2 25
2 -1
2 65
2 55
2 21
2 -1
Now i want to add/sum the score of each id ignoring -1 and i am trying with this code which is not working
SELECT SUM(CASE when SCORE>-1 THEN SUM(SCORE) ELSE 0 END)
FROM jbit WHERE htno='$id'
Here i am already using WHERE so how can i use another WHERE, if i use multiple Where in single query it may effect other processes.. please help me out
Help me out friends
if there is only two column then there is no need to use SUM, you can try below
SELECT id, IF(SCORE=-1,0,SCORE) AS scoreSum
FROM table1
GROUP BY id
Working DEMO
alternative ( not tested )
SELECT id, SUM(IF(SCORE=-1,0,SCORE)) AS scoreSum
FROM table1
WHERE htno =$id
GROUP BY id
SELECT SUM(score) AS score_sum
FROM jbit
WHERE htno='$id'
AND score <> -1 ;
SELECT SUM(`SCORE`)
FROM `jbit`
WHERE `htno` = '$id'
`SCORE` > 0;
Though, I'd suggest you to change '$id' to just $id if the column type of htno is INTEGER.
SELECT SUM(score) FROM <tablename> WHERE score != -1