I have script query select like this. I want to search value max and min 'jumlah' from query select.
SELECT
"B"."company" AS "B__company",
"User"."company" AS "User__company",
"Transaction"."create_date" AS "Transaction__create_date",
extract(day from "Transaction"."create_date"),
sum(cast("PLNPostpaid"."jumlahrek" as integer)) as jumlah,
sum("Transaction"."price_buy") as jumbuy,
sum(("Transaction"."price_sell")-("Transaction"."price_buy")) as admin
FROM "public"."transactions" AS "Transaction"
LEFT OUTER JOIN "public"."users" AS "User" ON ("User"."id" = "Transaction"."user_id")
FULL OUTER JOIN "public"."users" AS "B" ON ("B"."id" = "User"."bank")
LEFT JOIN "public"."inboxes" AS "Inbox" ON ("Inbox"."id" = "Transaction"."inbox_id")
LEFT JOIN "public"."pln_postpaids" AS "PLNPostpaid" ON ("Inbox"."id" = "PLNPostpaid"."inbox_id")
WHERE "Transaction"."create_date" >= '01-May-2015 00:00:00' AND "Transaction"."create_date" <= '31-May-2015 23:59:59'
AND "Transaction"."product_id"=100
GROUP BY extract(day from "Transaction"."create_date"), "Transaction"."create_date", "B"."company", "User"."company"
and I want to implement to php. Can anyone help me?
I think you need a subquery to select min and max.
For example,
WITH all_data AS (
-- YOUR INITIAL REQUEST GOES HERE
)
SELECT MIN (jumlah) as min_jumlah, MAX(jumlah) as max_jumlah
FROM all_data
It's unclear what syntax your code represents. If you compile the query from php, then you shouldn't use quotes like this. The whole string containing the query should be double quoted, but you don't need these double quotes inside.
Related
I'm having some problems with a query that finds the next ID of an orders with certain filters on it - Like it should be from a specific city, etc.
Currently it's used for a function, where it'll either spit out the previous or the next ID based on the current order. So it can either be min(id) or max(id), where max(id) is obviously faster, since it has to go through less rows.
The query is working just fine, but it's rather slow, since it's going through 123953 rows to find the ID. Is there any way I could optimize this?
Function example:
SELECT $minmax(orders.orders_id) AS new_id FROM orders LEFT JOIN orders_status ON orders.orders_status = orders_status.orders_status_id $where_join WHERE orders_status.language_id = '$languages_id' AND orders.orders_date_finished != '1900-01-01 00:00:00' AND orders.orders_id $largersmaller $ordersid $where;
Live example
SELECT min(orders.orders_id)
FROM orders
LEFT JOIN orders_status ON orders.orders_status = orders_status.orders_status_id
WHERE orders_status.language_id = '4'
AND orders.orders_date_finished != '1900-01-01 00:00:00'
AND orders.orders_id < 4868771
LIMIT 1
so concluding:
SELECT orders.orders_id
FROM orders
JOIN orders_status ON orders.orders_status = orders_status.orders_status_id
WHERE orders_status.language_id = '4'
AND orders.orders_date_finished != '1900-01-01 00:00:00'
AND orders.orders_id < 4868771
ORDER BY orders.orders_id ASC
LIMIT 1
Extra:
to get the MAX value, use DESC where ASC is now.
And looking at your question: be sure to escape the values like $language_id etcetera. I suppose they could come from some html form?
(or use prepared statements)
I have a query given below
SELECT A.order_no, A.order_date,
COUNT(B.reaction_no) as tot_reaction_no,
SUM(CASE
WHEN (B.purification != '') THEN 1
ELSE 0
END) as tot_purification
FROM order_header A
LEFT JOIN order_reactions B ON A.order_no = B.order_no
WHERE A.order_date BETWEEN '2015-10-01 00:00:00' AND '2016-09-01 00:00:00'
AND A.order_no = '23746'
GROUP BY A.order_no
this will results as shown in the picture. But the result is wrong because some of the entries are duplicates. So I have to remove the duplicate and print the count. Count required is the count of "column" from the table 1.
I think you need to leave out the A.order_date in your select or you should add it to the group by clause. That gives you a different result though.
You may also use a subquery in your select clause:
SELECT A.order_no, A.order_date,
COUNT(B.reaction_no) as tot_reaction_no,
(SELECT count(*) FROM order_reactions as or WHERE or.order_no=A.order_no AND purification!='') as tot_purification,
(SELECT count(*) FROM order_reactions as or2 WHERE or.order_no=A.order_no) as tot_reaction_no
FROM order_header A
WHERE A.order_date BETWEEN '2015-10-01 00:00:00' AND '2016-09-01 00:00:00'
AND A.order_no = '23746'
This is just from the top of my head, since your screenshots are not showing the full tables I'm not sure this is 100% right, but it might point you in the right direction.
I would propose the query
SELECT COUNT(DISTINCT clone_name) AS tot_purification, COUNT(*) AS tot_reaction_no FROM Table2 WHERE `purification`='Column' AND `order_no`=23746;
Please excuse errors in quotation, MySQL is very confusing when it comes to quotation imo.
EDIT:
Added AS tot_purification
I'm not sure what you are expecting as tot_reaction_no, so I just counted all rows where Order and purification match as described in my WHERE clause.
Its because you are grouping only on A.order_no, make following changes in the query and try again:
Replace the line:
GROUP BY A.order_no;
to
GROUP BY A.order_no, A.clone_name;
I have three tables named issue_details, nature_payments, and rci_records. Now I have this query which joins this three tables.
SELECT issue_details.issue_date AS Date,
issue_details.check_no AS Check_No,
payees.payee_name AS Name_payee,
nature_payments.nature_payment AS Nature_of_Payment,
issue_details.issue_amount AS Checks_issued,
issue_details.nca_balance AS Nca_balance
FROM
issue_details
INNER JOIN
nature_payments ON
issue_details.nature_id = nature_payments.nature_id
INNER JOIN
payees ON
issue_details.payee_id = payees.payee_id
ORDER BY Date Asc, Check_no ASC
On my column in Nca_balance, this is a computed differences of every issuances of check. But you may not know what really the process of how I got the difference but to make it simple, let's say that I have another query
that dynamically get also the difference of this nca_balance column. Here is the query:
SELECT r.*,
(#tot := #tot - issue_amount) as bank_balance
FROM (SELECT #tot := SUM(nca_amount) as nca_total FROM nca
WHERE account_type = 'DBP-TRUST' AND
year(issue_date) = year('2015-01-11') AND
month(issue_date) = month('2015-01-11')
)
vars CROSS JOIN issue_details r
WHERE r.account_type = 'DBP-TRUST' AND
r.issue_date = '2015-01-11'
ORDER BY r.issue_date, r.check_no
I know it you may not get my point but I just want to replace the first query of the line
issue_details.nca_balance AS Nca_balance
with my own computation on my second query.
Please help me combine those two query into a single query. Thanks
I have a MySQL statement that I need a little bit of help with. I am pulling the data correctly but I just need a some help excluding the records that are not need. If the candidate remaining attempts equal 0 I do not want to pull that record, but as you can see the I am using a subquery to generate the number of remining attempts a candidate may have.
Is there a way to use the remaining_attempts field in the WHERE statement to exclude the un-wanted records?
SELECT (
SELECT count(*) AS Number_Attempts_Used
FROM candidate_exam_record
WHERE candidate_exam_record.idcandidate_eligibilty = candidate_eligibilty.idcandidate_eligibilty
) AS remaining_attempts
, (
SELECT CASE WHEN count(*) > '0' THEN candidate_exam_record.idcandidate_eligibilty END
FROM candidate_exam_record
WHERE candidate_exam_record.idcandidate_eligibilty = candidate_eligibilty.idcandidate_eligibilty) AS EL_ID
FROM candidate_eligibilty
INNER JOIN candidate ON candidate_eligibilty.idcandidate = candidate.idcandidate
INNER JOIN exam ON candidate_eligibilty.exam_id = exam.exam_id
INNER JOIN jurisdiction ON exam.jurisdiction_id = jurisdiction.jurisdiction_id
WHERE jurisdiction.jurisdiction_id = 'xxxx'
AND candidate_eligibilty.eligibility_end_date >= '2013-02-19'
AND remaining_attempts > '0'
It's messy but it looks OK to me. Try changing '0' to just plain old 0. You're storing it as an INT I assume, correct?
I need help with an advanced SQL-query (MSSQL 2000).
I have a table called Result that lists athletics 100 and 200 meter race-times. A runner can have several racetimes but I want to show only the best time from each runner at each event.
The Result-table contains five columns, Result_id, athlete_id, result_time, result_date, event_code. So athlete_id must be unique when I list the values and result_time must be the fastest (lowest) value. Also I want to be able to choose if event_code should be "= 1" or "= 2", since 100 and 200 meter resulttimes are mixed in the same table.
I asked a similiar question a few days ago, but without the event_code condition.
This is the answer we came up with.
select r.*
from result r
inner join (
select athelete_id, min(result_time) as FastestTime
from result
group by athelete_id
) rm on r.athelete_id = rm.athelete_id and r.result_time = rm.FastestTime
Any ideas how I can add the event_code condition to this snippet?
Try this:
select r.*
from result r
inner join (
select athelete_id, min(result_time) as FastestTime
from result
where event_code = 1
group by athelete_id
) rm on r.athelete_id = rm.athelete_id and r.result_time = rm.FastestTime
Include the event code in the output of the subquery. Then you can show all events at the same time or choose them in an outer where clause:
select r.*
from result r inner join
(select athlete_id, event_code, min(result_time) as FastestTime
from result
group by athlete_id, event_code
) rm
on r.athelete_id = rm.athlete_id and
r.result_time = rm.FastestTime and
r.event_code = rm.event_code
-- where event_code = xx
The last line is an optional WHERE clause in case you want just one event at a time.