how to select a distinct of two specific columns in sql query - sql-server-2014

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.

Related

SELECT MAX with WHERE condition

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)

find maximum matches rows and count it in mysql

i have a table name 'room' with room_id and rental.
mysql> select * from room;
+---------+--------+
| room_id | rental |
+---------+--------+
| 1 | 2000 |
| 2 | 1890 |
| 3 | 1832 |
| 4 | 1833 |
| 5 | 1850 |
| 6 | 1700 |
| 7 | 2100 |
| 8 | 2000 |
| 9 | 2000 |
| 10 | 2000 |
+----------+--------+
10 rows in set (0.00 sec)
i tried find maximum matches rows and count into number from rental column.
mysql> select count(*),rental from room group by rental having count(*) >1;
+----------+--------+
| count(*) | rental |
+----------+--------+
| 4 | 2000 |
+----------+--------+
1 row in set (0.08 sec)
but my problem is i just want only one max number from rental which has max matches values and output like above.in above query will take a condition like count(*) > 1.but i want to check all rows from rental column instead of it condition.
USE ORDER BY AND LIMIT 1
select count(*) as cnt,rental from room group by rental order by cnt DESC limit 1;

How to get 2nd highest salary of each employee in employee table which contains more than one entry in employee table

i am not asking about 2nd highest salary in employee table , i am asking about 2nd highest salary of each employee.
click here to see employee table
inner query will return employees with their highest salary and then from out query those highest salaries will be filtered out
so you will get the second highest salary
SELECT MAX(T.salery),T.NAME FROM TABLE T
INNER JOIN (SELECT MAX(salery),NAME FROM TABLE GROUP BY NAME) TT
ON TT.NAME=T.NAME AND TT.SALERY!= T.SALERY
GROUP BY T.NAME;
example
mysql> SELECT * FROM payments;
+----+------------+---------+-------+
| id | date | user_id | value |
+----+------------+---------+-------+
| 1 | 2016-06-22 | 1 | 10 |
| 2 | 2016-06-22 | 3 | 15 |
| 3 | 2016-06-22 | 4 | 20 |
| 4 | 2016-06-23 | 2 | 100 |
| 5 | 2016-06-23 | 1 | 150 |
| 6 | 2016-06-23 | 2 | 340 |
+----+------------+---------+-------+
6 rows in set (0.00 sec)
mysql> select max(value),user_id from payments group by user_id;
+------------+---------+
| max(value) | user_id |
+------------+---------+
| 150 | 1 |
| 340 | 2 |
| 15 | 3 |
| 20 | 4 |
+------------+---------+
4 rows in set (0.00 sec)
mysql> select max(T.value),TT.user_id from payments T inner join (select max(value) as val,user_id from payments group by user_id) TT on T.value!=TT.val and T.user_id=TT.user_id group by T.user_id;;
+--------------+---------+
| max(T.value) | user_id |
+--------------+---------+
| 10 | 1 |
| 100 | 2 |
+--------------+---------+
2 rows in set (0.00 sec)
I think a very quick solution could be like this:
SELECT MAX(column) FROM table WHERE column < (SELECT MAX(column) FROM table)
Hope it helps-

Put sum() function into a view

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.

selecting all duplicates

How can I update this to select all duplicates?
SELECT address FROM list
GROUP BY address HAVING count(id) > 1
Currently, I think it just returs the addresses which are duplciated. I want all duplicates.
Select * from list
where address in (
select address from list group by address
having count(*) > 1);
Look at this sample query I ran:
mysql> select * from flights;
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 6 | 1 |
| 2 | 4 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
10 rows in set (0.00 sec)
mysql> select * from flights where source in
(select source from flights group by source having count(*) > 1);
+--------+-------------+
| source | destination |
+--------+-------------+
| 1 | 2 |
| 5 | 6 |
| 6 | 1 |
| 1 | 3 |
| 5 | 2 |
| 6 | 3 |
| 6 | 5 |
| 6 | 4 |
+--------+-------------+
8 rows in set (0.00 sec)
If I'm correct, you're looking for the actual rows that contain duplicates -- so that if you have three rows with the same address, you return all three rows.
Here's how to do it:
SELECT * FROM list
WHERE address in (
SELECT address FROM list GROUP BY address HAVING count(id) > 1
);
This should generally work unless your address is a 'text' field or if your address table has more than a few thousand duplicates.
Are you looking for this?
SELECT * FROM list
WHERE id IN (
SELECT id FROM list
GROUP BY address HAVING count(id) > 1
);