How use MAX in mysql to get follow result - mysql

This is the query
SELECT research_id,
IF(product_id = 4, IF(value REGEXP '^-?[0-9]+$' > 0, value, IF(value = 'Yes', 1, 0)), 0) AS val1,
IF(product_id = 8, IF(value REGEXP '^-?[0-9]+$' > 0, value, IF(value = 'Yes', 1, 0)), 0) AS val2
FROM research_product_details rpd
LEFT JOIN products p
ON rpd.product_id = p.id
WHERE (product_id = 4 AND value >= 50) OR (product_id = 8 AND value >= 50)
ORDER BY research_id ASC , product_id ASC
And i got this result from this query
i want it like follow

As #Jkike mentioned in his comment, one way to achieve what you want is to simply wrap your current query and GROUP BY the research_id column, selecting the max value for the val1 and val2 columns:
SELECT t.research_id AS research_id, MAX(t.val1) AS val1, MAX(t.val2) AS val2
FROM
(
SELECT research_id,
IF(product_id = 4, IF(value REGEXP '^-?[0-9]+$' > 0, value, IF(value = 'Yes', 1, 0)), 0) AS val1,
IF(product_id = 8, IF(value REGEXP '^-?[0-9]+$' > 0, value, IF(value = 'Yes', 1, 0)), 0) AS val2
FROM research_product_details rpd
LEFT JOIN products p
ON rpd.product_id = p.id
WHERE (product_id = 4 AND value >= 50) OR (product_id = 8 AND value >= 50)
ORDER BY research_id ASC , product_id ASC
) AS t
GROUP BY t.research_id

Related

MySql get balance of accounts

i want to calculate the balance of 3 accounts.
I have 2 tables:
accounts with id, name and start-balance
transactions with value, charge-account, type and paid
To calculate the balance i have to add the start-balance (from accounts) with alle the transaction-values where charge-account = account-id, paid = 1 and type = 1. Then i have to subtract (correct word?) all the transaction-values where charge-account = account-id, paid = 1 and type = 0
At the end, if everything would work i just want to see what balance the accounts have right now.
i tried this query but i get wrong results, it looks like it adds the start-balance multiple times...
SELECT
SUM(IF(a.id = 1, IF(t.type = 1 AND t.charge_account = 1, t.value, 0) - IF(t.type = 0 AND t.charge_account = 1, t.value, 0), 0) + a.start-balance) as "balanc_1",
SUM(IF(a.id = 2, IF(t.type = 1 AND t.charge_account = 2, t.value, 0) - IF(t.type = 0 AND t.charge_account = 2, t.value, 0), 0) + a.start-balance) as "balance_2",
SUM(IF(a.id = 3, IF(t.type = 1 AND t.charge_account = 3, t.value, 0) - IF(t.type = 0 AND t.charge_account = 3, t.value, 0), 0) + a.start-balance) as "balance_3"
FROM test.transactions t, test.accounts a
WHERE t.paid = 1;
transactions:
accounts:
how it should be like:
SELECT a.id,
MAX ( a.`start-balance` ) +
SUM ( CASE WHEN t.type = 1 then t.value
WHEN t.type = 2 then -t.value
ELSE 0
END ) as balance
FROM accounts a
JOIN transactions t
ON a.id = t.`charge-account`
WHERE a.id IN (1,2,3)
AND t.paid = 1
GROUP BY id
You need to use UNION and then group by account id
select accountid, sum(amount ) as amount from (
select accountid, startamount as amount from accounts
union
select accountid, transactionamount from transactions WHERE ....
) t
group by accountid

MySQL getting and Invalid use of group function

I'm trying to count the result of tc.personnel_id if there's more than 1 result, then the output is 1. And then finally, it will get the sum. But I am getting an "Invalid use of group function" error when i execute this code. Any help will be appreciated.
SELECT tv.id,
tv.vesselName,
SUM(IF(tpi.returningCrew = 0, 1,
IF(
COUNT(tc.personnel_id) > 1, 1, 0)
)
) AS ex_crew,
SUM(IF(tpi.returningCrew = 1, 1, 0)) AS new_hire
FROM tbl_vessels AS tv
LEFT JOIN tbl_personnel_info AS tpi
ON tv.id = tpi.lastJoinedVsl
LEFT JOIN tbl_contracts AS tc
ON tpi.id = tc.personnel_id
LEFT JOIN tbl_management AS tm
ON tm.id = tv.management
WHERE tpi.emp_status = 'ON-BOARD'
AND tc.status = 'ACTIVE'
AND tv.trade_route = 'WORLD WIDE'
AND tm.management = 'TOKYO'
GROUP BY vesselName;
Aggregate functions can not be nested. So something like SUM(.. COUNT(..) ..) is not possible. You might try something like this:
SELECT tv.id,
tv.vesselName,
CASE WHEN COUNT(tc.personnel_id) > 1
THEN SUM(IF(tpi.returningCrew = 0, 1, 1)
ELSE SUM(IF(tpi.returningCrew = 0, 1, 0)
END AS ex_crew,
SUM(IF(tpi.returningCrew = 1, 1, 0)) AS new_hire
FROM [..]
SUM(IF(tpi.returningCrew = 0, 1, 1) can be rewriten to SUM(1) or to COUNT(*).
SUM(IF(tpi.returningCrew = 0, 1, 0) cann also be rewritten to SUM(tpi.returningCrew = 0)
And SUM(IF(tpi.returningCrew = 1, 1, 0)) to SUM(tpi.returningCrew = 1)
So the final select could be:
SELECT tv.id,
tv.vesselName,
CASE WHEN COUNT(tc.personnel_id) > 1
THEN COUNT(*)
ELSE SUM(tpi.returningCrew = 0)
END AS ex_crew,
SUM(tpi.returningCrew = 1) AS new_hire
FROM [..]

Different outputs for each condition.get together group by all values - single mysql query

Here how my tables look like:
CREATE TABLE my_table(id INT,project_id VARCHAR(6),order_id VARCHAR(6),user_id VARCHAR(6),owner_id VARCHAR(6));
INSERT INTO my_table
VALUES
(1, 211541, 8614, 1605, 0),
(2, 211541, 8614, 16079, 1605),
(3, 210446, 0, 12312, 0),
(4, 208216, 0, 16467, 14499),
(5, 208216, 0, 14499, 0),
(6, 208216, 0, 14499, 0),
(7, 208216, 0, 16467, 14499),
(8, 209377, 0, 7556, 0),
(9, 209324, 0, 7556, 0),
(10,201038, 8602, 9390, 101);
I have to check split Multiple condtion:
Query Execution this kind of way.
order_id != 0
Initially goes to project_id,
(i.e)
1.project_id - 211541 then first condition (owner_id = 0) , select user_id
note:
- if not get user_id(empty result) - goes to second condition.
- if get user_id - do not go to second condtion.
2.project_id - 211541 - second condtion (owner_id != 0), select owner_id.
i got
my_user_id
1605
101
order_id = 0
(i.e)
1.project_id - 208216 then first condition (owner_id = 0) , select group by user_id
note:
- if not get user_id(empty result) - goes to second condition.
- if get user_id - do not go to second condtion.
2.project_id - 208216 - second condtion (owner_id != 0), select group by owner_id.
i got
my_user_id
123121449975567556
Finally, i need this answer - group by my_user_id
my_user_id
160510112312144997556
Note:
I need single query.
why not just use an IF?
SELECT
IF (order_id = 0, user_id, owner_id) AS new_val
FROM my_table
GROUP BY new_val
when looking at it more it seems like you need a few more ifs.. something like this?
SELECT
if(order_id <> 0,
if(owner_id = 0, user_id, owner_id),
if(user_id = 0, owner_id, user_id)
) AS new_val
FROM my_table
group by new_val
this is what i understand from your conditions
ill number them and then put them with the if conditions i'll build in a second
if the order_id is not 0, -- 1
check to see if the owner_id is 0,
if owner_id = 0 -- 2
then pull in user_id -- 3
else owner_id is not 0
and you pull in owner_id -- 4
to write this more like code..
if(order_id <> 0, if(owner_id <> 0, owner_id, user_id), some condition for when order_id is 0)
other case
if the order_id is 0
pull in user_id (grouped)
if user_id = 0 -- 5
then pull in owner_id -- 6
else user_id is not 0
and pull in user_id -- 7
to put this with the other part replace the some other condition for when its 0.
-- 1 2 3 4 5 6 7
if(order_id <> 0, if(owner_id = 0, user_id, owner_id), if(user_id = 0, owner_id, user_id))
now to format it so its readable
if(order_id <> 0, -- if its not 0
if(owner_id = 0, user_id, owner_id), -- true condition
if(user_id = 0, owner_id, user_id) -- false condition
)
am I correct?
You can use 'if'.
Eg
#result=if(1!=2,'yes','no');
Which would give result a value of 'yes'.
These can be nested to create complex conditions:
SET #value_a='no';
set #value_b='';
set #value_c=’test’;
SET #value_d=’’;
set #result=
(
select if(#value_a!=’no’,(SELECT column1 FROM table1 WHERE id= #value_a),
if(#value_b!='',#value_b,
if(#value_c!='',(select column2 from table2 where id=#value_a),
if(#value_d!='',(select column3 from table3 where id=#value_a),
‘I am a default value’
)
)
)
)
);
Giving whatever select column2 from table2 where id=#value_a gives.

Is it possible to make that instead of NULL will be displayed 0?

Code:
SELECT
count(*) as count,
sum(Status = 'test1') as count_test1,
sum(Status = 'test2') as count_test2
FROM Table
WHERE Type = 'test'
in result i see:
count count_test1 count_test2
0 NULL NULL
Tell me please is it possible to make that instead of NULL will be displayed 0?
P.S.: That results was:
count count_test1 count_test2
0 0 0
you can use COALESCE
COALESCE(sum(Status = 'test1'), 0)
or IFNULL
IFNULL(sum(Status = 'test1'), 0)
SELECT
count(*) as count,
IFNULL(sum(Status = 'test1'), 0) as count_test1,
IFNULL(sum(Status = 'test2'), 0) as count_test2
FROM Table
WHERE Type = 'test'
You can use the COALESCE function which take an array and return the first element not null
SELECT
count(*) as count,
COALESCE(sum(Status = 'test1'), 0) as count_test1,
COALESCE(sum(Status = 'test2'), 0) as count_test2
FROM Table
WHERE Type = 'test'

Subsequence in MySQL/CakePHP

In my mysql table I have a field which is a 4 letter Myers-Briggs personality type. I would like to search through the table and match when the personality type matches the one in the query by having 2 aspects in common. The way I understand this, it is really just finding the longest common subsequence of the two and testing that it is >= 2
Example:
'ISTJ' would match with 'INFJ', because the length of the common subsequence is 'IJ' >= 2
and
'ISTJ' would not match with 'INFP', because the length of the common subsequence is 'I' <= 2
Is there a way to do this in a mysql query? I am using CakePHP for the querying, so if you know how to do this with Cake that would also be helpful.
The Myer-Briggs personality types are positional. This means that you can compare character by character.
Here is one method, where you just have to put in the comparison string once:
select t.*
from (select t.*,
(case when substring(t.MyerBriggs, 1, 1) = substring(const.comp, 1, 1)
then 1 else 0
end) as MB1,
(case when substring(t.MyerBriggs, 2, 1) = substring(const.comp, 2, 1)
then 1 else 0
end) as MB2,
(case when substring(t.MyerBriggs, 3, 1) = substring(const.comp, 3, 1)
then 1 else 0
end) as MB3,
(case when substring(t.MyerBriggs, 4, 1) = substring(const.comp, 4, 1)
then 1 else 0
end) as MB4
from t cross join
(select 'INFJ' as comp) const
)
where (MB1+MB2+MB3+MB4) >= 2
You can actually simplify this in MySQL as:
select t.*
from t cross join
(select 'INFJ' as comp) const
where (if(substring(t.MyerBriggs, 1, 1) = substring(const.comp, 1, 1), 1, 0) +
if(substring(t.MyerBriggs, 2, 1) = substring(const.comp, 2, 1), 1, 0) +
if(substring(t.MyerBriggs, 3, 1) = substring(const.comp, 3, 1), 1, 0) +
if(substring(t.MyerBriggs, 4, 1) = substring(const.comp, 4, 1), 1, 0)
) >= 2
If I understand the Myers-Briggs thingy properly, there are two possibilities for each of the four categorisation axis, and the order of the letters is constant (and therefore carries no meaning).
In this case, you could use four two-state columns like the below, instead of one string:
CREATE TABLE profile (
user_id INT,
EI ENUM ('E', 'I'),
SN ENUM ('S', 'N'),
TF ENUM ('T', 'F'),
JP ENUM ('J', 'P')
);
Profile 'ISTJ' would be inserted like this:
INSERT INTO profile VALUE (1, 'I', 'S', 'T', 'J');
Matching with profile 'INFJ' would look like this:
SELECT * FROM profile WHERE
(EI = 'I') + (SN = 'N') + (TF = 'F') + (JP = 'J') >= 2