MySQL selecting distinct value not working properly - mysql

I am trying to retrieve distinct value using this query from my salary table
SELECT DISTINCT gross,sl_month,sl_year
FROM wp_attn_emp_salary where `emp_id` = 22 order by gross DESC
But it returns duplicate value too. please, check the image.
Query Result
My expecting result is like this
Array
(
[0] => stdClass Object
(
[gross] => 9595
[sl_month] => 2
[sl_year] => 2021
)
[1] => stdClass Object
(
[gross] => 9226
[sl_month] => 1
[sl_year] => 2020
)
[2] => stdClass Object
(
[gross] => 8875
[sl_month] => 2
[sl_year] => 2019
)
[3] => stdClass Object
(
[gross] => 8420
[sl_month] => 1
[sl_year] => 2019
)
[4] => stdClass Object
(
[gross] => 8405
[sl_month] => 12
[sl_year] => 2018
)
[5] => stdClass Object
(
[gross] => 6464
[sl_month] => 1
[sl_year] => 2018
)
)
I need the sl_month,sl_year in descending order too.

You misunderstand how DISTINCT works in SQL. It returns a row if the whole row (i.e. all columns) is distinct from any other row in the result. These two rows are distinct from each other:
gross
sl_month
sl_year
9595
1
2021
9595
2
2021
From the desired result you described, it looks like you want a greatest-n-per-group query.
SELECT gross, sl_month, sl_year
FROM (
SELECT gross, sl_month, sl_year,
ROW_NUMBER() OVER (PARTITION BY gross ORDER BY sl_year, sl_month) AS rownum
FROM wp_attn_emp_salary
WHERE emp_id = 22
) AS t
WHERE t.rownum = 1
ORDER BY sl_month DESC, sl_year DESC;
Note: Window functions require MYSQL 8.0, which was released in 2018 so you should be using it by now. If you need a solution that runs on an earlier version of MySQL, search past answers with the greatest-n-per-group tag.

Related

MYSQL: How to sort number DESC with plus value as high

I need some help to create desire output with pure MYSQL. My question is By fire SQL below:
SELECT
r.questions_id
,r.Empty_Peg_Count AS 'Empty_Pegs'
FROM
Training_Core.results AS r
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC;
I'm getting below output:
Array
(
[0] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2
)
[1] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2+
)
[2] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 1
)
)
I want value with + sign as high priority and it should show first. my desired output is as below:
Array
(
[0] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2+
)
[1] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 2
)
[2] => Array
(
[questions_id] => 256332653
[Empty_Pegs] => 1
)
)
Can anyone help me how to get this result with SQL query?
Thanks an advance!
You can use a second order by rule:
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC
,r.Empty_Peg_Count LIKE '%+%' DESC;
This will push the entries with a + to the top.
You can use RIGHT to check if the last character is a + and use that as a secondary ORDER BY field:
SELECT
r.questions_id
,r.Empty_Peg_Count AS 'Empty_Pegs'
FROM
Training_Core.results AS r
ORDER BY
CAST(r.Empty_Peg_Count AS SIGNED INTEGER) DESC
, RIGHT(r.Empty_Peg_Count, 1) = '+' DESC
You can apply sort using the following
SELECT * FROM `data` ORDER BY
CASE
WHEN right(`Empty_Pegs`,1) = '+' THEN (left(`Empty_Pegs`,1)*10)
ELSE `Empty_Pegs`
END DESC;

how to combine mysql query in CodeIgniter

I need to combine those two array into a single array.
1st query result
Array ( [0] => stdClass Object ( [p_type] => Bank [tsales] => 131000 [tdues] => 55000 [tpaid] => 75000 ) [1] => stdClass Object ( [p_type] => Cash [tsales] => 104000 [tdues] => 50000 [tpaid] => 54000 ) [2] => stdClass Object ( [p_type] => bKash [tsales] => 25000 [tdues] => 10000 [tpaid] => 5000 ) [3] => stdClass Object ( [p_type] => Pause [tsales] => 4000 [tdues] => 1000 [tpaid] => 2000 ) )
2nd query result
Array ( [0] => stdClass Object ( [dpayment] => 1000 ) [1] => stdClass Object ( [dpayment] => 5000 ) [2] => stdClass Object ( [dpayment] => 1000 ) [3] => stdClass Object ( [dpayment] => 5000 ) )
try to use array_merge. That should be very easy.
Your query result shows that you using $query->result() to get the value,
Instead of using result(), use $query->row_array();
then you get single dimensional array for both queries
then use array_merge() that would be easy

Laravel 4 : How to make a sub-query with Query Builder?

I have two tables. There is a table place that contains data as shown below:
id name
-----------
1 A1
2 A2
3 A3
4 B1
Other table place_conditions contains data as sown below:
id placeID desription
----------------------------
1 1 Windy
2 1 Dry
3 3 Wet
4 3 Rainy
4 4 Dry
I like to made a query in laravel 4 that make result like this below:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => A1
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Windy
)
[1] => stdClass Object
(
[description] => Dry
)
)
)
[1] => stdClass Object
(
[id] => 2
[name] => A2
[place_cond] => Array
(
)
)
[3] => stdClass Object
(
[id] => 3
[name] => A3
[place_cond] => Array
(
[0] => stdClass Object
(
[description] => Wet
)
[1] => stdClass Object
(
[description] => Rainy
)
)
)
)
Here part of my query (it still error) :
$query = DB::table('place');
if(Input::has('search')){
$query->where(function($a){
$a->where('name','LIKE','%'.Input::get('search').'%');
});
}
/* $query->select('place.*',DB::raw('SELECT Description as place_cond FROM place_conditions WHERE place_conditions.placeID = place.id')); // sub query . Arrggghhhhhh... whyyyy.. stillllll.. errorrrrrrr.. */
$data = $query->get();
Is possible to produce results like the above in laravel 4? Can you help me? Thank you

Return multileve array from MySql

I'm trying to return some data from a query in this format (or at least very similar)
Array
(
[0] => Array
(
[id] => 1
[name] => Gym Opening Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
[1] => Array
(
[id] => 1
[name] => Resteruant Times
[times] => Array
(
[0] => Array
(
[id] => 1
[label] => Mon-Thu
[time] => 6:00am : 10:00pm
)
[1] => Array
(
[id] => 2
[label] => Fri
[time] => 6:00am : 09:00pm
)
[2] => Array
(
[id] => 3
[label] => Sat & Sun
[time] => 6:00am : 10:00pm
)
)
)
)
I have this query so far, however, it throws an error Error executing PDO query: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
SELECT
otg.groupName,
otg.id,
(SELECT
ot.id,
ot.label,
ot.time
FROM
opening_times ot
WHERE
ot.groupId = otg.id) as times
FROM
opening_time_groups as otg
WHERE
otg.venueId = $venueId
AND otg.active = 1
What have I gotten wrong? Or is this something I simply can't do in 1 query?
You sub query is returning more than one row. A sub query in the SELECT must only return a single row.
However it would seem that you could do this simple with a normal join, then sort the array out in the script that calls it:-
SELECT
otg.groupName,
otg.id,
ot.id,
ot.label,
ot.time
FROM opening_time_groups as otg
LEFT OUTER JOIN opening_times ot
ON ot.groupId = otg.id) as times
WHERE otg.venueId = $venueId
AND otg.active = 1

mysql sub query count returning gloabl count and ignoring where

At the moment "matrix_mct_versions" is a table with 73 entries. When I run this query the "version_count" always returns 73, ie the full number of rows. When I run the sub select query on its own i get the real count as per the com_ID param sent. I cannot see what I am doing wrong with this.. can anyone help?
SELECT
a_ID as com_ID,
option_number,
comment,
word_count,
gender,
sample,
(
SELECT
count(a_ID)
FROM
matrix_mct_versions
WHERE
com_ID = com_ID
) as version_count
FROM
matrix_mct
WHERE
attribute_number = :attribute_number AND
grade_number = :grade_number AND
attribute_type = :attribute_type
ORDER BY
option_number
Returns results like this:
[0] => Array
(
[com_ID] => 678
[option_number] => 1
[comment] => TODO primary function missing for controller y
[word_count] => 7
[gender] => 2
[sample] => 0
[version_count] => 73
)
[1] => Array
(
[com_ID] => 679
[option_number] => 2
[comment] => TODO make this green
[word_count] => 4
[gender] => 2
[sample] => 0
[version_count] => 73
)
[2] => Array
(
[com_ID] => 680
[option_number] => 3
[comment] => TODO make this better
[word_count] => 4
[gender] => 2
[sample] => 0
[version_count] => 73
)
At least one problem is your subquery. It is not correlated. I think you mean:
(SELECT count(a_ID)
FROM matrix_mct_versions
WHERE matrix_mct_versions.com_ID = matrix_mct.com_ID
) as version_count