I'm trying to execute following sql query
SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73
I'm not able to do this as WHERE condition is not working and it gives me the value of largest number in entire table.
What I'm doing wrong?
Your Query Is Correct
SELECT MAX(amount) AS LargestPrice FROM au_bids WHERE product = 73
Check Live Demo
http://sqlfiddle.com/#!9/11bbf1/2
your query is corrct
mysql> select * from Customers;
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1 | Arun |
| 2 | kumar |
| 4 | bbbbb |
| 5 | mmmmm |
| 6 | kkkk |
| 3 | eeeeee |
+-------------+---------------+
6 rows in set (0.00 sec)
mysql> select max(customer_id) from Customers where customer_id=2;
+------------------+
| max(customer_id) |
+------------------+
| 2 |
+------------------+
1 row in set (0.00 sec)
Related
I've a student table like below -
CREATE TABLE `test`.`student` (
`student_id` INT NOT NULL,
`student_marks` INT NULL);
I want to find the student whose sum of marks is greater than 500, but I am not able to filter the records, however I develop below query.
mysql> select * from student;
+------------+---------------+
| student_id | student_marks |
+------------+---------------+
| 1 | 220 |
| 2 | 100 |
| 3 | 280 |
| 2 | 430 |
| 1 | 300 |
+------------+---------------+
5 rows in set (0.00 sec)
mysql> select student_id, sum(student_marks) from student group by student_id;
+------------+--------------------+
| student_id | sum(student_marks) |
+------------+--------------------+
| 1 | 520 |
| 2 | 530 |
| 3 | 280 |
+------------+--------------------+
3 rows in set (0.00 sec)
You can use the HAVING clause: select student_id, sum(student_marks) from student group by student_id having sum(student_marks) > 500;
We can do like below -
mysql> select * from ( select student_id, sum(student_marks) as marks_of_student from student group by student_id) as f where f.marks_of_student > 500;
+------------+------------------+
| student_id | marks_of_student |
+------------+------------------+
| 1 | 520 |
| 2 | 530 |
+------------+------------------+
2 rows in set (0.00 sec)
I have normalize my table to separate beetwen tb_header and tb_detail.
This is the tb_repair detail.
mysql> SELECT DETAIL_ID, REPAIR_ESTIMATE_ID, REMARKS, MANHOUR FROM tb_repair_detail;
+-----------+--------------------+----------------------+---------+
| DETAIL_ID | REPAIR_ESTIMATE_ID | REMARKS | MANHOUR |
+-----------+--------------------+----------------------+---------+
| 9 | 50 | Anda | 12.00 |
| 10 | 50 | Jika | 10.00 |
| 11 | 51 | ACRYLIC | 12.00 |
| 12 | 51 | Pembersihan exterior | 10.00 |
| 13 | 51 | Repairing | 10.00 |
+-----------+--------------------+----------------------+---------+
5 rows in set (0.00 sec)
Now, for more readable to user, now I want to create a view that would be like this :
mysql> SELECT a.REPAIR_ESTIMATE_ID , a.EIR_REF ,
-> (SELECT b.NAMA FROM tb_customer_master b WHERE a.COSTUMER_ID = b.COSTUMER_ID) AS "NAMA_CUSTOMER"
-> from tb_master_repair_estimate a ;
+--------------------+---------+----------------------+
| REPAIR_ESTIMATE_ID | EIR_REF | NAMA_CUSTOMER |
+--------------------+---------+----------------------+
| 50 | 1545053 | APL |
| 51 | 1545052 | APL |
+--------------------+---------+----------------------+
2 rows in set (0.00 sec)
My question is, I want to put the manhour total based REPAIR_ESTIMATE_ID How to put this manhour total in this view ? I know the query to sum() each REPAIR ESTIMATE_ID like
mysql> SELECT SUM(MANHOUR) AS TOTAL FROM tb_repair_detail a WHERE a.REPAIR_ESTIMATE_ID = 51
mysql> SELECT SUM(MANHOUR) AS TOTAL FROM tb_repair_detail a WHERE a.REPAIR_ESTIMATE_ID = 50;
+-------+
| TOTAL |
+-------+
| 22.00 |
+-------+
+-------+
| TOTAL |
+-------+
| 32.00 |
+-------+
1 row in set (0.00 sec)
But, how I get them into those view ?
SELECT a.repair_estimate_id, a.eir_ref, b.nama AS nama_customer, c.total
FROM tb_master_repair_estimate a, tb_customer_master b,
(SELECT repair_estimate_id, SUM(manhour) AS total
FROM tb_repair_detail
GROUP BY repair_estimate_id) c
WHERE a.repair_estimate_id = c.repair_estimate_id
AND a.COSTUMER_ID = b.COSTUMER_ID
should, I think, give the desired result set. Make that your view's query and you ought to be set.
Note the clause GROUP BY repair_estimate_id which causes the subquery to compute SUM(manhour) for each distinct value of repair_estimate_id.
select distinct (pc.id,pc.PolicyPremiumID) ,pc.policyPremiumCatID,b.nin, b.firstname+' '+coalesce((b.middleInitial),'')+' '+b.lastname as fullname, b.gender,ppc.amount from
bio_data b, medicalInsurance m, policy p, policyPremium pp,premiumCategories pc,policyPremiumCategory ppc
where b.nin=m.patientBin and m.policyID=p.id and (p.id=pp.policyID and pp.policyID=p.id) and pp.id=pc.policyPremiumID and pc.policyPremiumcatID=ppc.id
and p.id=82
in the above query, I want to have a distinct of those two columns
it is duplicating instead of 7 return values it bring 14
Can some please help? Thanks in advance!!!
Use the following method:
Sample select query which list all values:
mysql> select * from new_table;
+---------+------------+
| premium | sumassured |
+---------+------------+
| 1000 | 100000 |
| 2000 | 200000 |
| 3000 | 300000 |
| 1000 | 100000 |
| 1000 | 100000 |
| 3000 | 300000 |
| 4000 | 400000 |
+---------+------------+
7 rows in set (0.00 sec)
Select Query which lists Distinct Value from multiple Tables:
mysql> select distinct premium,sumassured from new_table;
+---------+------------+
| premium | sumassured |
+---------+------------+
| 1000 | 100000 |
| 2000 | 200000 |
| 3000 | 300000 |
| 4000 | 400000 |
+---------+------------+
4 rows in set (0.00 sec)
You can use the same for N number of Rows.
I have following MySQL query result, I want only UserName first part before # it need 10000013 part only, I want to remove example.com part, it is possible by TRIM?
mysql> SELECT UserName,DAY(AcctStartTime), COUNT(ResponseCode='200') FROM table201412 WHERE UserName='10000013#example.com' GROUP BY DATE(AcctStartTime);
+---------------------------+--------------------+------------------------------+
| UserName | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013#example.com | 1 | 3 |
| 10000013#example.com | 2 | 5 |
| 10000013#example.com | 3 | 3 |
+----------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)
I need following result:
+---------------------------+--------------------+------------------------------+
| UserName | DAY(AcctStartTime) | COUNT(ResponseCode='200') |
+---------------------------+--------------------+------------------------------+
| 10000013 | 1 | 3 |
| 10000013 | 2 | 5 |
| 10000013 | 3 | 3 |
+---------------------------+--------------------+------------------------------+
10 rows in set (0.00 sec)
The easiest way is to use substring_index():
select substring_index(UserName, '#', 1) as EmailName
In your query, that would be:
SELECT substring_index(UserName, '#', 1) as EmailName, DAY(AcctStartTime), COUNT(ResponseCode='200')
FROM table201412
WHERE UserName='10000013#example.com'
GROUP BY DATE(AcctStartTime);
I have 4 tables:
ARTICOLE
BAR
BUCATARIE
MAGAZIE
mysql> select * from ARTICOLE;
| OID | ART |
| 1 | TEST |
| 2 | TESTQ |
| 3 | MYART |
| 4 | MYARTBUC |
4 rows in set (0.00 sec)
mysql> select * from BAR;
| OID | ART | CANT |
| 1 | TEST | 3.00000 |
| 2 | TESTQ | 1.00000 |
| 3 | MYART | 20.00000 |
3 rows in set (0.00 sec)
mysql> select * from BUCATARIE;
| OID | ART | CANT |
| 1 | TEST | 5.00000 |
| 2 | MYARTBUC | 10.00000 |
2 rows in set (0.00 sec)
mysql> select * from MAGAZIE;
Empty set (0.00 sec)
the below query
mysql> select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT) from ARTICOLE a,BUCATARIE buc,BAR bar,MAGAZIE mag where a.ART=bar.ART and a.ART=bar.ART and a.ART=mag.ART group by a.ART;
return:
Empty set (0.00 sec)
how must be query to return:
| ART | sum(bar.CANT) | sum(buc.CANT) | sum(mag.CANT) |
TEST | 3.00000 | 5.00000 | NULL |
TESTQ | 1.00000 | NULL | NULL |
MYART | 20.00000 | NULL | NULL|
MYARTBUC | NULL | 10.00000 | NULL |
????
Any help appreciated.
You need to make use of LEFT JOIN to include results from other tables without filtering out records that don't match:
select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT)
from ARTICOLE a
left join BUCATARIE buc on buc.ART = a.ART
left join BAR bar on bar.ART = a.ART
left join MAGAZIE mag on mag.ART = a.ART
group by a.ART;
Sample results:
ART SUM(BAR.CANT) SUM(BUC.CANT) SUM(MAG.CANT)
MYART 20
MYARTBUC 10
TEST 3 5
TESTQ 1
Demo: http://www.sqlfiddle.com/#!2/6efb2/2