sql sort numeric then alphabetically - php

in this example :
10-20
20-40
50-60
v
k
r
a
12 month
1 month
how can i sort it in this order ?:
10-20
20-40
50-60
a
k
r
v
1 month
12 month
i use abs(value) but in the alphabetical case doesn't work

If you can get away with doing some processing in PHP, you could use natsort:
Standard sorting
Array
(
[3] => img1.png
[1] => img10.png
[0] => img12.png
[2] => img2.png
)
Natural order sorting
Array
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
Otherwise, there's another question on SO which asks the same thing: Natural Sort in MySQL

OK, thanks to the commenter, now a working version. This sorts on two cases in the order by clause:
select *
from (
select '10-20' as col1
union all select '20-40'
union all select '50-60'
union all select 'v'
union all select 'k'
union all select 'r'
union all select 'a'
union all select '12 month'
union all select '1 month'
) s1
order by
case
when col1 rlike '[0-9][0-9]-[0-9][0-9]' then 1
when col1 rlike '[0-9]+ month' then 3
else 2
end
, case
when col1 rlike '[0-9][0-9]-[0-9][0-9]' then cast(col1 as decimal)
when col1 rlike '[0-9]+ month' then cast(col1 as decimal)
else col1
end
The first case puts categories in order: 00-00 first, then other stuff, and at the end the months. The second case converts the columns to decimal if possible.

Related

Group concat data values by year

I have a query that will sum total loss of data by month within a current year.
SELECT label, m, coalesce(sum(total_loss), 0) as data
FROM (
SELECT label, m
FROM
(
SELECT YEAR(CURDATE()) label UNION ALL SELECT YEAR(CURDATE())-1
) years,
(
SELECT 01 m UNION ALL SELECT 02 UNION ALL SELECT 03 UNION ALL SELECT 04
UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07 UNION ALL SELECT 08
UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) months
) ym
LEFT JOIN incident ON ym.label = YEAR(incident.incident_date) AND ym.m = MONTH(incident.incident_date)
GROUP BY label, m
The data output is
Array
(
[0] => Array
(
[label] => 2017
[m] => 1
[data] => 0
)
[1] => Array
(
[label] => 2017
[m] => 2
[data] => 0
)
[2] => Array
(
[label] => 2017
[m] => 3
[data] => 0
)
[3] => Array
(
[label] => 2017
[m] => 4
[data] => 0
) etc..
This will output all 12 months, and data does not exist will be 0.
What I'm looking for is some sort of group concat that will output something like this:
{
data: [0, 0, 0, 0, 0, 1100, 0, 0, 0, 0, 0, 0],
label: '2017'},
Is it possible to adjust with group concat or write a for each loop to create the data above?
Try grouping only by year, and then use GROUP_CONCAT on the sums across all months:
SELECT
label,
GROUP_CONCAT(COALESCE(SUM(total_loss), 0)) AS data
FROM
(
SELECT label, m
FROM
(
SELECT YEAR(CURDATE()) label UNION ALL SELECT YEAR(CURDATE())-1
) years,
(
SELECT 01 m UNION ALL SELECT 02 UNION ALL SELECT 03 UNION ALL SELECT 04
UNION ALL SELECT 05 UNION ALL SELECT 06 UNION ALL SELECT 07 UNION ALL SELECT 08
UNION ALL SELECT 09 UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
) months
) ym
LEFT JOIN incident
ON ym.label = YEAR(incident.incident_date) AND
ym.m = MONTH(incident.incident_date)
GROUP BY label;

Issue with mysql joins

I have two tables
Meetings:
m_id ProjectName
1 Test
2 Test2
Meeting_next:
id fk_m_id Meetingdate status
1 1 9-1-2018 0
1 1 10-1-2018 0
1 1 13-1-2018 1
I want to join this two tables when I left join it I will get duplicate value
Expected output
Array
(
[0] => Array
(
[m_id] => 1
[ProjectName] => test
[meetingDate] =>13-1-2018
)
[1] => Array
(
[m_id] => 2
[ProjectName] => test2
[meetingDate] =>
)
)
I tried -
select * from meetings left join meeting_next on meetings.m_id= meeting_next.fk_m_id where meeting_next.status=1 order by m_id desc
myOutput:
Array
(
[0] => Array
(
[m_id] => 1
[ProjectName] => test
[meetingDate] =>13-1-2018
) )
Bad luck I got only first Project name. I need second too. Please help me. Any help would be appreciated.
Your WHERE condition filters the number of rows to only the row of the first project.
If you want to show both projects, even if there are no meetings with status 1, you need to move the condition to the join condition:
select *
from meetings
left join meeting_next
on meetings.m_id= meeting_next.fk_m_id
and meeting_next.status=1
order by m_id desc
Now you will get all rows from meetings with only the matching entries from meeting_next.

How can I merge the dates on this ledger?

I have tow tables for reciepts and expenture, am using union to merge them AND generate reports
SELECT MIN(date) AS trx, MAX(date), COUNT(*), SUM(amount), CONCAT(' - ','-'),
$f as _
FROM expenditure WHERE date >= '$start_date' AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
UNION
SELECT MIN(date) AS trx, MAX(date), COUNT(*),CONCAT(' - ','-'), SUM(amount),
$f as _
FROM receipts
WHERE date >= '$start_date' AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
ORDER BY trx
I get the following results
Array (
[0] => Array ( [trx] => 2012-03-06 [MAX(date)] => 2012-03-06 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-06 )
[1] => Array ( [trx] => 2012-03-08 [MAX(date)] => 2012-03-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-03-08 )
[2] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 2 [SUM(amount)] => 60000 [CONCAT(' - ','-')] => - - [_] => 2012-06-06 )
[3] => Array ( [trx] => 2012-06-06 [MAX(date)] => 2012-06-06 [COUNT(*)] => 1 [SUM(amount)] => - - [CONCAT(' - ','-')] => 487200 [_] => 2012-06-06 )
[4] => Array ( [trx] => 2012-06-08 [MAX(date)] => 2012-06-08 [COUNT(*)] => 1 [SUM(amount)] => 120000 [CONCAT(' - ','-')] => - - [_] => 2012-06-08 )
[5] => Array ( [trx] => 2012-06-29 [MAX(date)] => 2012-06-29 [COUNT(*)] => 2 [SUM(amount)] => 320000 [CONCAT(' - ','-')] => - - [_] => 2012-06-29 ) )
problem
If you notice on 3 and four there are two records for one day.
[trx] => 2012-06-06
Because on that day the client carried out both a credit and debit. is there way to just only combine rows like 3 and 4 into one row. SELECT DISTINCT dindnt work either or is it posible for mysql to sum records from both tables that occur on the same date.
You can use the subselect for your custom aliases to group
$query = "
SELECT q.*
FROM (
SELECT
MIN(date) AS trx,
MAX(date) as max_date,
COUNT(*),
SUM(amount),
CONCAT(' - ','-'),
$f as _
FROM
expenditure
WHERE
date >= '$start_date'
AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
UNION
SELECT
MIN(date) AS trx,
MAX(date),
COUNT(*),
CONCAT(' - ','-'),
SUM(amount),
$f as _
FROM
receipts
WHERE
date >= '$start_date'
AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _ ORDER BY trx
) q GROUP BY q.max_date
";
Above query will group the results by MAX(date) as max_date
And for the sum of amount on same date you can modify as
$query = "
SELECT
q.*,
SUM(q.amount) AS grand_sum
FROM (
SELECT
MIN(date) AS trx,
MAX(date) as max_date,
COUNT(*),
SUM(amount) AS amount,
CONCAT(' - ','-'),
$f as _
FROM
expenditure
WHERE
date >= '$start_date'
AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _
UNION
SELECT
MIN(date) AS trx,
MAX(date),
COUNT(*),
CONCAT(' - ','-'),
SUM(amount),
$f as _
FROM
receipts
WHERE
date >= '$start_date'
AND date <= '$end_date'
AND client_id like '{$_SESSION['client']['id']}'
GROUP BY _ ORDER BY trx
) q GROUP BY q.max_date
";
This grand_sum column has the sum of amount on the same date
You can UNION the expenditures and receipts tables first, then select from the unioned result. Note that the unioned result has separate columns for receipt.value and expenditure.value.
SELECT
MIN(date) as trx,
MAX(date),
COUNT(exp_amount) + COUNT(rct_amount) AS Tran_Count,
COALESCE(SUM(exp_amount), ' - -') AS Exp_Total,
COALESCE(SUM(rct_amount), ' - -') AS Rct_Total,
_
FROM (
SELECT whatever AS _, date, amount AS exp_amount, NULL AS rct_amount
FROM expenditure
WHERE date >= start AND date <= end AND client_id LIKE 'whatever'
UNION SELECT whatever, date, NULL, amount
FROM receipts
WHERE date >= start AND date <= end AND client_id LIKE 'whatever'
) exp_rct
GROUP BY _
ORDER BY trx

MySQL secondary query is too performance intensive

I have a query where I want to pull in ID's from another table based on the ID of the item selected in another table. I'm currently doing this with an additional query based on the results that I get from the main query. It's resulting in many many additional queries. Is there a way to condense this into 1 query?
SELECT music.id,
SUM(linked_tags.weight) AS total_weight
FROM (music)
INNER JOIN linked_tags ON linked_tags.track_id = music.id
AND linked_tags.tag_id IN (7,56,59)
GROUP BY music.id
ORDER BY total_weight DESC
Then the additional query comes from running the results from the main query through a foreach loop, where 2713 is the ID of an item in the music table.
SELECT tag_id,
weight
FROM (linked_tags)
JOIN tags_en ON tags_en.id = linked_tags.tag_id
WHERE track_id = '2713'
This results in this object, where all_tags is the data that comes from the 2nd query:
[id] => 1500
[name] => Some Track Name
[total_weight] => 10
[all_tags] => Array
(
[0] => 20
[1] => 28
[2] => 4
[3] => 13
[4] => 16
[5] => 7
[6] => 42
[7] => 56
[8] => 61
)
Is there a way to pull this all into 1 query?
You can combine them directly using join:
select tag_id, weight
from (SELECT music.id,
SUM(linked_tags.weight) AS total_weight
FROM music join
linked_tags
ON linked_tags.track_id = music.id AND linked_tags.tag_id IN (7,56,59)
GROUP BY music.id
) m join
linked_tags
on m.id = linked_tags.track_id join
tags_en
ON tags_en.id = linked_tags.tag_id;
EDIT:
If I understand the query correctly, you are trying to get all tags on "tracks" (or "music") that have one or more tags in the set of (7,56,59). And, you want to get the sum of the weights of those three tags.
You can do this in one pass, if you don't mind have the tags in a comma-delimited list:
SELECT m.id,
SUM(case when lt.tag_id IN (7,56,59) then lt.weight end) AS total_weight,
sum(lt.tag_id IN (7, 56, 59)) as NumSpecialTags,
group_concat(lt.tag_id) as AllTags
FROM music m join
linked_tags lt
ON lt.track_id = m.id
GROUP BY m.id
having NumSpecialTags > 0
order by total_weight desc;
You then have to parse the AllTags list at the application layer.

CodeIgniter SQL query - how to sum values for each month?

I have the following table:
//table_1
record_id user_id plant_id date cost
1 1 1 2011-03-01 10
2 1 1 2011-03-02 10
3 1 1 2011-04-10 5
4 1 2 2011-04-15 5
I would like to build a query (if possible using CI Active Records, but MySQL is fine) in which I generate the following result:
[1] => [1] => [March 2011] [20]
=> [April 2011] [5]
[2] => [March 2011] [0]
=> [April 2011] [5]
I have tried using $this->db->group_by but I think I'm not using it correctly.
If anyone could give me a pointer or roadmap to get this done it would be much appreciated -- thanks!
Sample table
drop table if exists t;
create table t( record_id int, user_id int, plant_id int, date datetime, cost float);
insert t select
1 ,1, 1 ,'2011-03-01', 10 union all select
2 ,1, 1 ,'2011-03-02', 10 union all select
3 ,1, 1 ,'2011-04-10', 5 union all select
4 ,1, 2 ,'2011-04-15', 5;
Because you want to see the row with 0, you need to do a cross join between the year-month and all user-plants.
select up.user_id, up.plant_id, ym2, ifnull(sum(t.cost),0) totalcost
from (select distinct date_format(date, '%Y-%m') ym, date_format(date, '%M %Y') ym2 from t) dates
cross join (select distinct t.user_id, t.plant_id from t) up
left join t on date_format(t.date, '%Y-%m') = dates.ym
and up.user_id=t.user_id
and up.plant_id=t.plant_id
group by up.user_id, up.plant_id, ym2, ym
order by up.user_id, up.plant_id, date(concat(ym,'-1'));
The fact that Month Year does not sort correctly also requires the complex treatment of the dates for ordering purposes at the end.
This is a pretty intense way to do it and it would be far preferable to store the month-year as a column itself if you need to make these queries frequently, but this is basically how it works:
SELECT CONCAT(MONTHNAME(date), ' ', YEAR(date)) AS monthyear, COUNT(*) AS count GROUP BY YEAR(date), MONTH(date), plant_id;
That should get you the resultset you're looking for.

Categories