Display mysql results in one row - mysql

mysql> select * from facts;
+----+---------+------------+---------+
| id | fact_id | fact_value | host_id |
+----+---------+------------+---------+
| 1 | 1 | rh5 | 1 |
| 2 | 1 | rh4 | 2 |
| 3 | 2 | virtual | 1 |
| 4 | 2 | virtual | 2 |
| 5 | 3 | rack 2 | 1 |
+----+---------+------------+---------+
mysql> select * from hosts;
+---------+-----------+
| host_id | host_name |
+---------+-----------+
| 1 | bellagio |
| 2 | mirage |
+---------+-----------+
The query I used does the following:
mysql> select host_name,fact_value from hosts as a left join facts as b on
> b.host_id=a.host_id and b.fact_id in (1,2,3);
+-----------+------------+
| host_name | fact_value |
+-----------+------------+
| bellagio | rh5 |
| bellagio | virtual |
| bellagio | rack 2 |
| mirage | rh4 |
| mirage | virtual |
+-----------+------------+
I want the results to print one row for each host, notice how it prints each fact_value on a separate row. The reason why i have the IN clause is that this table has over 40 possible columns for each host. I only want a handful (I select 3 in this example).
Here's what i'm looking for.
+-----------+--------+-----------+-----------+
| host_name | value1 | value 2 | value 3 |
+-----------+--------+-----------+-----------+
| bellagio | rh5 | virtual | rack 2 |
| mirage | rh4 | virtual | NULL |
+-----------+--------+-----------+-----------+

SELECT GROUP_CONCAT(fact_value)...
...
GROUP BY host_name

You could try creating a view on each of the value columns and then joining the columns.
create view [first_values] as
select fact_value, host_id
from facts where fact_id = 1;
create view [second_values] as
select fact_value, host_id
from facts where fact_id = 2;
create view [third_values] as
select fact_value, host_id
from facts where fact_id = 3;
now join the columns:
select h.host_name, f.fact_value as value1, s.fact_value as value2, t.fact_value as value3
from hosts as h
left join [first_values] as f on h.host_id = f.host_id
left join [second_values] as s on h.host_id = s.host_id
left join [third_values] as t on h.host_id = t.host_id;

This works ... granted some of the field names have changed since I last posted.
select distinct t0.host_id, t1.name, t2.value as os_type, t3.value as ip_addr, t4.value as rack, t5.value as host_owner, t6.value as host_memory,
t7.value as host_swap, t8.value as host_make, t9.value as host_model
FROM fact_values t0 left outer join hosts t1 on (t0.host_id=t1.id)
left outer join fact_values t2 on (t0.host_id=t2.host_id and t2.fact_name_id = 30)
left outer join fact_values t3 on (t0.host_id = t3.host_id and t3.fact_name_id = 13)
left outer join fact_values t4 on (t0.host_id = t4.host_id and t4.fact_name_id = 65)
left outer join fact_values t5 on (t0.host_id = t5.host_id and t5.fact_name_id = 81)
left outer join fact_values t6 on (t0.host_id = t6.host_id and t6.fact_name_id = 18)
left outer join fact_values t7 on (t0.host_id = t7.host_id and t7.fact_name_id = 51)
left outer join fact_values t8 on (t0.host_id = t8.host_id and t8.fact_name_id = 36)
left outer join fact_values t9 on (t0.host_id = t9.host_id and t9.fact_name_id = 47)
WHERE t1.name = '$hostname'

SELECT u.user_id,u2.acc_no AS pacc,u3.acc_no AS sacc
FROM table_name AS u
LEFT JOIN table_name AS u2 ON u2.user_id=u.user_id AND u2.acc_type='P' AND u2.lab_id = ? AND u2.category = 'SOMETHING'
LEFT JOIN table_name AS u3 ON u3.user_id=u.user_id AND u3.acc_type='S' AND u3.lab_id = ? AND u3.category = 'SOMETHING'
GROUP BY u.user_id
I have used the abouve query to get multiple rows to single row from the same table . Which is worked fine with me ..

Related

Calculate sum on the records while joining two tables with a third table

I have three tables:
mysql> select * from a;
+----+---------+
| ID | Name |
+----+---------+
| 1 | John |
| 2 | Alice |
+----+---------+
mysql> select * from b;
+------+------------+----------+
| UID | date | received |
+------+------------+----------+
| 1 | 2017-10-02 | 5 |
| 1 | 2017-09-30 | 1 |
| 1 | 2017-09-29 | 4 |
+------+------------+----------+
mysql> select * from c;
+------+------------+------+
| UID | date | sent |
+------+------------+------+
| 1 | 2017-09-25 | 7 |
| 1 | 2017-09-30 | 2 |
| 1 | 2017-09-29 | 3 |
+------+------------+------+
If I try to calculate the total number of sent for John, it would be 12. And for received, it would be 10.
But if I try to join all three tables, the result is weird. Here is my query to join three tables:
mysql> select sum(sent), sum(received) from a
-> join c on c.UID = a.ID
-> join b on b.UID = a.ID
-> where a.ID = 1;
+-----------+---------------+
| sum(sent) | sum(received) |
+-----------+---------------+
| 36 | 30 |
+-----------+---------------+
But I need correct numbers (12 and 10, respectively). How can I have correct numbers?
You should join the aggregated result and not the raw tables
select a.uid, t1.received, t2.sent
from a
inner join (
select uid, sum(received) received
from b
group by uid
) t1 on t1.uid = a.id
inner join (
select uid, sum(sent) sent
from c
group by uid
) t2 on t2.uid = a.id
where a.id = 1
You could try below
select bx.id, recieved, sum(c.sent) sent from
(
SELECT a.id, sum(b.received) recieved
from a
INNER JOIN b
ON a.id=b.uid
group by a.id
) bx
INNER JOIN c
ON c.uid=bx.id
group by bx.id, bx.recieved;
>>>Demo<<<
This gets rid of the subquery, but introduces something else you might not want:
( SELECT uid, 'Received' AS direction, SUM(received) AS HowMany
WHERE uid = 1
GROUP BY uid )
UNION ALL
( SELECT uid, 'Sent' AS direction, SUM(sent) AS HowMany
WHERE uid = 1
GROUP BY uid )

Attach max from group to every row of results ?

I have similar to the following database structure:
ApplicationService table :
+-----+--------------+----------+---------------+
| id | name | status | application_id
| 1 | Service 1 | 1 | 24
| 2 | Service 2 | 2 | 24
| 3 | Service 3 | 3 | 25
+-----+--------------+----------+----------------
And there is other table with status definitions:
CustomerStatus
+------------+--------------+----------+-----------+
| status_id | name | level | is_closed
| 1 | Status 1 | 2 | 1
| 2 | Status 2 | 1 | 0
| 3 | Status 3 | 3 | 1
+------------+--------------+----------+----------
The status for each row of the ApplicationServices is calculated as the max level status within the records grouped by application_id.
So to get all records from ApplicationServices with statuses would result in something like this:
+-----+--------------+----------+----------------+-------------+-----------
| id | name | status | application_id | status_name | is_closed
| 1 | Service 1 | 1 | 24 | Status 1 | 1
| 2 | Service 2 | 2 | 24 | Status 1 | 1
| 3 | Service 3 | 3 | 25 | Status 3 | 1
+-----+--------------+----------+----------------+-------------+-----------
Is there an efficient way to attach the results with max(level) grouped by application_id to every row of the result set ?
Try this:
SELECT A.id, A.name, A.status, A.application_id, CS.name AS status_name, CS.is_closed
FROM ApplicationService A
INNER JOIN (SELECT AA.application_id, MAX(CS.level) AS maxLevel
FROM ApplicationService AA
INNER JOIN CustomerStatus CS ON AA.status = CS.status_id
GROUP BY AA.application_id
) AS AA ON A.application_id = AA.application_id
INNER JOIN CustomerStatus CS ON AA.maxLevel = CS.level;
See working SQLfiddle here.
You will probably have to perform a sub-query. The subquery would simply be joining the two tables together, grouping/collapsing by application_id and then fetching the maximum is_closed value:
SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id
The max is_closed value should then be accessible by the max_is_closed alias when you incorporate the subquery:
SELECT
t1.id AS id,
t1.name AS name,
t1.status AS `status`,
t1.application_id AS application_id,
t2.name AS status_name,
t3.max_is_closed
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
LEFT JOIN
(SELECT
t1.status AS `status`,
t1.application_id AS `app_id`,
MAX(t2.is_closed) AS `max_is_closed`
FROM applicationstatus AS t1
LEFT JOIN customerstatus AS t2 ON
t1.status = t2.status_id
GROUP BY t1.application_id) AS t3 ON
t1.application_id = t3.app_id
The output from the query:

MySQL select rows where its columns sum equal value

I have following tables:
A:
+----+-----------+-------+----------+
| ID | PaymentID | Price | Quantity |
+----+-----------+-------+----------+
| 1 | 1 | 128 | 1 |
| 2 | 2 | 10 | 2 |
| 3 | 2 | 11 | 1 |
| 4 | 3 | 100 | 2 |
+----+-----------+-------+----------+
B:
+-----------+------------+
| PaymentID | TotalPrice |
+-----------+------------+
| 1 | 128 |
| 2 | 31 |
| 3 | 201 |
+-----------+------------+
And query:
SELECT a.ID
FROM a
LEFT JOIN b ON b.PaymentID = a.PaymentID
WHERE b.TotalPrice = (a.Price * a.Quantity)
It works fine when a.PaymentID is unique, but some transactions in table A are separated and paid (table B) together. Query above return a.ID = 1 but I need to return a.ID = 1,2,3.
a.PaymentID(1): 128 * 1 = 128 MATCH
a.PaymentID(2): 10 * 2 + 11 * 1 = 31 MATCH
a.PaymentID(3): 100 * 2 = 200 NOT MATCH
SQL Fiddle
You are trying to join sum of Price and amount from table a to table b along with the PaymentId, and using it onto a joining clause which would be calculated per row based not on aggregate based.
You may need to first find the aggregate part and then join something as
select
a.ID
from a
left join (
select sum(Price*Quantity) as tot,PaymentID
from a group by PaymentID
)x on x.PaymentID = a.PaymentID
join b on b.PaymentID = a.PaymentID and x.tot = b.TotalPrice
http://www.sqlfiddle.com/#!9/3b261/45
Try this statement:
SELECT a.ID, b.totalprice
FROM a
LEFT JOIN b ON b.PaymentID = a.PaymentID
group by b.paymentID
having TotalPrice = sum(a.Price * a.Quantity)
SQLFIDDLE
UPDATE: After clarification:
select a.id from a where paymentId in(
select paymentID from(
SELECT a.paymentID as paymentID, b.totalprice
FROM a
LEFT JOIN b ON b.PaymentID = a.PaymentID
group by b.paymentID
having TotalPrice = sum(a.Price * a.Quantity)) as c )

MySQL join and sum issue / combine queries

I have created a points example to demonstrate my issue.
A members can reward other members points, I am trying to build a query which will display the points a user has and points user has given.
Database Example
Members,
+----+----------+
| id | username |
+----+----------+
| 1 | user1 |
| 2 | user2 |
| 3 | user3 |
+----+----------+
Points,
+-------+--------+--------+
| IDFor | IDFrom | Pointz |
+-------+--------+--------+
| 1 | 2 | 5 |
| 1 | 2 | 5 |
| 3 | 1 | 2 |
+-------+--------+--------+
The return I am looking for is,
+-----------+--------+-------+
| username | Pointz | Given |
+-----------+--------+-------+
| user1 | 10 | 2 |
| user2 | 0 | 10 |
| user3 | 2 | 0 |
+-----------+--------+-------+
Both my queries return,
+-----------+--------+-------+
| username | Pointz | Given |
+-----------+--------+-------+
| user1 | 10 | 4 |
| user2 | 0 | 10 |
| user3 | 2 | 0 |
+-----------+--------+-------+
http://sqlfiddle.com/#!2/32ae6/6
SELECT a.`username`, sum(a.`Pointz`), sum(b.`Pointz`) FROM
(SELECT * FROM `members`
LEFT JOIN `Example`.`Points` AS p ON `members`.`id` = p.`IDFor` ) AS a
LEFT JOIN
(SELECT * FROM `members`
LEFT JOIN `Example`.`Points` AS n ON `members`.`id` = n.`IDFrom` ) AS b
ON a.id = b.id
GROUP BY a.`id`
http://sqlfiddle.com/#!2/32ae6/7
SELECT `members`.`username`,sum(p.`Pointz`),sum(n.`Pointz`)
FROM `members`
LEFT JOIN `Example`.`Points` as p ON p.`IDFor` = `members`.`id`
LEFT JOIN `Example`.`Points` as n ON n.`IDFrom` = `members`.`id`
GROUP BY `members`.`id`
Seems to be a common question that pops up but have not found a solution, all my other attempts from similar questions have not ended well, Thanks all.
This is what u want
select a.username, ifnull(Pointz, 0) Pointz, ifnull(Given, 0) Given from
(SELECT id, `username`, sum(`Pointz`) Pointz FROM `members`
LEFT JOIN `Points` ON `members`.`id` = `Points`.`IDFor` group by id) a
left join
(SELECT id, `username`, sum(`Pointz`) Given FROM `members`
LEFT JOIN `Points` ON `members`.`id` = `Points`.`IDFrom` group by id) b
on a.id = b.id
Try:
SELECT m.username,
COALESCE( pr.preceived, 0 ) as Pointz,
COALESCE( pg.pgiven, 0 ) as Given
FROM members m
LEFT JOIN ( SELECT IDFor, sum(pointz) as preceived
FROM Points
GROUP BY IDFor ) pr
ON m.id = pr.IDFor
LEFT JOIN ( SELECT IDFrom, sum(pointz) as pgiven
FROM Points
GROUP BY IDFrom ) pg
ON m.id = pg.IDFrom
http://sqlfiddle.com/#!2/32ae6/48
Try this:
SELECT m.`username`,
ifnull((SELECT sum(`Pointz`) FROM Points p
WHERE p.IDFor = m.id ), 0),
ifnull((SELECT sum(`Pointz`) FROM Points p
WHERE p.IDFrom = m.id ), 0)
FROM Members m
SQLFiddle

join two tables(one of them is part of the other)

I have two tables Table_A and Table_B and i want to join those tables to optain Table_c as the result:
Table_A:
+-----------+-----------+---------+
| tableA_id | tableB_id | v_id |
+-----------+-----------+---------+
| 1 | 2 | 27 |
| 2 | 3 | 27 |
| 3 | 3 | 28 |
| 4 | 1 | 26 |
| 5 | 2 | 26 |
| 6 | 3 | 26 |
| 7 | 1 | 24 |
| 8 | 1 | 25 |
+-----------+-----------+---------+
Table_B:
+-----------+-----------+
| tableB_id | s_name |
+-----------+-----------+
| 1 | s1 |
| 2 | s2 |
| 3 | s3 |
+-----------+-----------+
Table_c:
+-----------+-----------+-----------++--------+
| tableB_id | s_name | tableA_id | v_id |
+-----------+-----------+-----------+---------+
| 1 | s1 | null | null |
| 2 | s2 | 1 | 27 |
| 3 | s3 | 2 | 27 |
+-----------+-----------+-----------+---------+
I tried different queries, but i couldn't reach the desired output.
This is MYSQL query: *Edit: reverse table order.
SELECT s.tableB_id, s.s_name, v.tableA_id, v.v_id
FROM Table_B as s
left OUTER JOIN Table_A as v
ON v.v_id=27
EDIT:
The result should be all Table_B data on left and assign to it table_A data if any or make it null. How can i make this?
Last Edit:
Here is a solution i came up with:
SELECT s.tableB_id , s.s_name, v.tableA_id, v.v_id
FROM Table_B s , Table_A v
WHERE v.v_id=27 AND v.tableB_id = s.tableB_id
UNION
SeLECT s.tableB_id , s.s_name, null as tableA_id, null as v_id
FROM Table_B s
WHERE s.tableB_id NOT IN (SELECT s.tableB_id
FROM Table_B s , Table_A v
WHERE v.v_id=27 AND v.tableB_id = s.tableB_id )
You could try:
SELECT tb.tableB_id, tb.s_name, ta.tableA_id, ta.v_id
FROM Table_B tb LEFT JOIN Table_A ta
ON tb.tableB_id = ta.tableB_id
To choose only one you could use:
SELECT tb.tableB_id, tb.s_name, ta.tableA_id, ta.v_id
FROM Table_B tb LEFT JOIN
(SELECT * FROM Table_A
WHERE v_id = 27) ta
ON tb.tableB_id = ta.tableB_id
SELECT distinct s.tableB_id, s.s_name, v.tableA_id, v.v_id
FROM Table_A as v
left OUTER JOIN Table_B as s
ON v.tableB_id =s.tableB_id and v.v_id = 27;
use this query hope fully you will get result
SELECT s.tableB_id, s.s_name, v.tableA_id, v.v_id
FROM Table_A as v
left OUTER JOIN Table_B as s ON s.tableB_id=v.tableB_id
WHERE v.v_id=27
Reverse the order, making it b LEFT JOIN a and add the condition for the common column (tableB_id):
SELECT s.tableB_id, s.s_name, v.tableA_id, v.v_id
FROM Table_B as s
LEFT OUTER JOIN Table_A as v
ON v.tableB_id = s.tableB_id
AND v.v_id=27 ;