This question already has answers here:
Closed 10 years ago.
There are two tables
Table 1
select * from manage_tcp;
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 2 | 3389 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
| 9 | 3389 | up |
| 8 | 3389 | up |
| 7 | 1433 | up |
| 6 | 3389 | up |
| 5 | 3389 | up |
| 4 | 3389 | up |
| 10 | 1433 | up |
| 11 | 1433 | up |
| 12 | 3389 | up |
| 13 | 1433 | up |
| 14 | 3389 | up |
| 15 | 1433 | up |
| 16 | 3389 | up |
| 17 | 1433 | up |
| 18 | 3389 | up |
| 19 | 1433 | up |
| 20 | 3389 | up |
| 21 | 1433 | up |
| 24 | 1433 | up |
| 23 | 3389 | up |
| 25 | 3389 | up |
| 26 | 1433 | up |
| 29 | 1433 | up |
| 28 | 3389 | up |
| 30 | 3389 | up |
| 31 | 80 | up |
| 32 | 3389 | up |
| 33 | 80 | up |
| 34 | 3389 | up |
| 35 | 80 | up |
+----+----------+--------+
34 rows in set (0.00 sec)
Table 2
mysql> select * from manage_host;
+----+------------+-------------+--------+-------+--------------+----------+------+
| id | uptime | type | statut | group | thresold_ref | thresold | mail |
+----+------------+-------------+--------+-------+--------------+----------+------+
| 2 | 277248529 | win2003.png | up | 4 | 1 | 0 | NULL |
| 3 | 277277471 | win2003.png | prob | 4 | 1 | 0 | NULL |
| 4 | 346159833 | win2003.png | up | 4 | 1 | 0 | NULL |
| 5 | 930205491 | win2003.png | up | 5 | 1 | 0 | NULL |
| 6 | 3805663007 | win2003.png | up | 5 | 1 | 0 | NULL |
| 7 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 8 | 29413867 | win2003.png | up | 5 | 1 | 0 | NULL |
| 9 | 981986401 | win2003.png | up | 5 | 1 | 0 | NULL |
| 10 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 11 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 12 | 2230787611 | win2003.png | up | 7 | 1 | 0 | NULL |
| 13 | -1 | win2003.png | up | 7 | 1 | 0 | NULL |
| 14 | 1004161923 | win2003.png | up | 8 | 1 | 0 | NULL |
| 15 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 16 | 1592294954 | win2003.png | up | 8 | 1 | 0 | NULL |
| 17 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 18 | 1449216250 | win2003.png | up | 4 | 1 | 0 | NULL |
| 19 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 20 | 3461945234 | win2003.png | up | 4 | 1 | 0 | NULL |
| 21 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 23 | 3461946968 | win2003.png | up | 4 | 1 | 0 | NULL |
| 24 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 25 | 3462022562 | win2003.png | up | 4 | 1 | 0 | NULL |
| 26 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 35 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 28 | 3461879984 | win2003.png | up | 4 | 1 | 0 | NULL |
| 29 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 30 | 1872950109 | win2003.png | up | 6 | 1 | 0 | NULL |
| 31 | 1872950125 | win2003.png | up | 6 | 1 | 0 | NULL |
| 32 | 1955232033 | win2003.png | up | 6 | 1 | 0 | NULL |
| 34 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 33 | 1955232049 | win2003.png | up | 6 | 1 | 0 | NULL |
+----+------------+-------------+--------+-------+--------------+----------+------+
32 rows in set (0.00 sec)
I'd like to compare those two tables above and to get the results in table 1 but not in table 2 like: (this is the result i want)
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
I've tried
SELECT a.*,b.*
FROM manage_tcp as a
LEFT JOIN manage_host as b
USING(id,statut)
WHERE b.id is null;
but I got
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
it's not what I want.
Thanks a lot!
Try this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
This will give you the intersection of the tables manage_tcp and manage_host based on id and status columns.
get the results in table 1 but not in table 2
If you need the difference between the tow tables, use NOT EXISTS instead of EXISTS like this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE NOT EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
Typically, this is the implementations of the INTERSECT and EXCEPT operators which are defined by the SQL standard as the set intersection and difference operators. But, unfortunately, most database system doesn't implement these operators. MS SQL Server does support these operators but not MYSQL .
Try this: (assume the comparison column are id and statut)
SELECT a.id, a.services, a.statut
FROM manage_tcp a
WHERE NOT EXISTS(SELECT b.id FROM manage_host b WHERE b.id=a.id AND b.statut=a.statut)
UNION
SELECT a.id, (SELECT services FROM manage_tcp WHERE id=a.id ORDER BY id LIMIT 1), a.statut
FROM manage_tcp a
GROUP BY a.id, a.statut HAVING COUNT(1)>1
The first query will produce result from table a where not in table b.
The second, after UNION, will produce result for id that appear twice in table a.
SELECT manage_tcp.* FROM
(
SELECT a.id,a.statut,a.cnt
FROM (
SELECT id, statut, COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id, a.statut
HAVING a.cnt > COUNT(1)
) AS diff
LEFT JOIN manage_tcp USING(id,statut)
or
SELECT a.id,a.statut,a.services,a.cnt
FROM (
SELECT id,statut,services,COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id,a.statut
HAVING a.cnt > COUNT(1)
Related
Edit
Please consider this before marking as duplicate
The common CTE or repeated joins or similar solutions does not fully satisfy the premise of the problem. Those solutions work for one and only one root (product) while the question at hand, asks for all roots within a table or query to be traversed and flattened, without a loop.
Problem Definition
I have three tables that define the characteristics of some products:
specifications
+----|------------------|----------|--------+
| id | name | type | status |
|----|------------------|----------|--------|
| 1 | height | float | 0 |
| 2 | width | float | 0 |
| 3 | length | float | 0 |
| 4 | weight | float | 0 |
| 5 | colour | int | 0 |
| 6 | material | int | 0 |
| 7 | manufacturer | int | 0 |
| 8 | durability | float | 0 |
| 9 | battery_type | int | 0 |
| 10 | battery_capacity | float | 0 |
| 11 | connectivity | set<int> | 0 |
| 12 | page | int | 0 |
| 13 | name | string | 0 |
| 14 | description | string | 0 |
+----|------------------|----------|--------+
groups
+----|-------------------|--------+
| id | name | status |
|----|-------------------|--------|
| 1 | cellphone | 0 |
| 2 | notebook | 0 |
| 3 | portable | 0 |
| 4 | workstation | 0 |
| 5 | pc | 0 |
| 6 | computer | 0 |
| 7 | electronic_device | 0 |
| 8 | book | 0 |
| 9 | sizeable | 0 |
| 10 | volumable | 0 |
| 11 | general | 0 |
+----|-------------------|--------+
specification_groups
+----|----------|------------------|--------+
| id | group_id | specification_id | status |
|----|----------|------------------|--------|
| 1 | 11 | 13 | 0 |
| 2 | 11 | 14 | 0 |
| 3 | 11 | 5 | 0 |
| 4 | 10 | 1 | 0 |
| 5 | 9 | 2 | 0 |
| 6 | 9 | 3 | 0 |
| 7 | 8 | 12 | 0 |
| 8 | 3 | 6 | 0 |
| 9 | 3 | 9 | 0 |
| 10 | 3 | 10 | 0 |
| 11 | 7 | 7 | 0 |
| 12 | 7 | 11 | 0 |
+----|----------|------------------|--------+
group_groups
+----|----------|--------------------|--------+
| id | group_id | group_reference_id | status |
|----|----------|--------------------|--------|
| 1 | 3 | 1 | 0 |
| 2 | 3 | 2 | 0 |
| 3 | 3 | 8 | 0 |
| 4 | 6 | 4 | 0 |
| 5 | 6 | 5 | 0 |
| 6 | 7 | 1 | 0 |
| 7 | 7 | 2 | 0 |
| 8 | 7 | 4 | 0 |
| 9 | 7 | 5 | 0 |
| 10 | 9 | 7 | 0 |
| 11 | 9 | 8 | 0 |
| 12 | 10 | 7 | 0 |
| 12 | 11 | 7 | 0 |
| 12 | 11 | 8 | 0 |
+----|----------|--------------------|--------+
product_groups
+----|--------|-------|--------+
| id | name | group | status |
|----|--------|-------|--------|
| 1 | phone1 | 1 | 0 |
| 2 | book1 | 8 | 0 |
+----|--------|-------|--------+
Ideally, I want to get all specification attributes for a product which the status along all tree-lines would be 0, but just to know what groups does a product is in is acceptable.
A result may look like this:
result
+---------|-------------|--------------|-------------------|----------|--------+
| row_num | product_id | product_name | product_group | group_id | status |
|---------|-------------|--------------|-------------------|----------|--------|
| 1 | 1 | phone1 | cellphone | 1 | 0 |
| 2 | 1 | phone1 | portable | 3 | 0 |
| 3 | 1 | phone1 | electronic_device | 7 | 0 |
| 4 | 1 | phone1 | sizable | 9 | 0 |
| 5 | 1 | phone1 | volumable | 10 | 0 |
| 6 | 1 | phone1 | general | 11 | 0 |
| 7 | 2 | book1 | book | 8 | 0 |
| 8 | 2 | book1 | portable | 3 | 0 |
| 9 | 2 | book1 | sizable | 9 | 0 |
| 10 | 2 | book1 | general | 11 | 0 |
+---------|-------------|--------------|-------------------|----------|--------+
Here's the answer if anyone else wants to do the same. The trick was to use the non-recursive initial selection parameters as constants in the recursive one:
with recursive
q as (
select * from product_groups as pg where ur.`status` = 0
)
, ancestors as (
(
select q.id as `product_id`, q.product_name as `product_name`, gg.group_id as `group_id`, gg.group_reference_id as `group_reference_id`
from group_groups as gg
join q on gg.group_id = q.group_id
where gg.`status` = 0
)
union
(
select q.id as `product_id`, q.product_name as `product_name`, null as `group_id`, q.group_id as `group_reference_id`
from q
)
union
(
select a.id as `product_id`, a.product_name as `product_name`, gg.group_id as `group_id`, gg.group_reference_id as `group_reference_id`
from group_groups as gg, ancestors as a
where gg.group_id = a.group_reference_id and gg.`status` = 0
)
)
select distinct
t.id as `product_id`, t.`group_id` as `group_id`, t.specification_id as `specification_id`, t.product_name as `product_name`, t.group_name as `group_name`, s.name as `specification_name`
from
(
select a.product_id, a.group_reference_id as `group_id`, sg.specification_id as `specification_id`, a.product_name, g.name as `group_name`
from ancestors as a
right join specification_groups as sg on a.group_reference_id = sg.group_id
where a.product_id is not null and sg.`status` = 0
) as t
join specifications as s on t.specification_id = s.id
order by product_id asc, group_id, asc, specification_id asc
;
I am trying to create a trigger in MySQL database. I have a table (myData) with 3 columns. Date, Values, and Status. What I am trying to achieve is, when a new value comes, if it is higher than the last value, It should insert 1 in the Status column. If it is less than the last value, It should insert 0 in the status column. I couldn't find a logic to do it. Any suggestions, please?
BEGIN
IF new.Values > // what should be here?
THEN
INSERT INTO //
END
Instead consider the following:
SELECT * FROM my_table;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 12 |
| 3 | 13 |
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 7 | 8 |
| 8 | 3 |
| 9 | 10 |
| 10 | 1 |
| 11 | 18 |
| 12 | 4 |
| 13 | 6 |
| 14 | 0 |
| 15 | 2 |
| 16 | 8 |
| 17 | 14 |
| 18 | 7 |
| 19 | 15 |
| 20 | 11 |
| 21 | 12 |
| 22 | 7 |
| 23 | 20 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 28 | 12 |
| 29 | 3 |
| 30 | 18 |
| 31 | 1 |
| 32 | 12 |
+----+-------+
SELECT a.*
, b.value >= a.value n
FROM my_table a
LEFT
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id;
+----+-------+------+
| id | value | n |
+----+-------+------+
| 1 | 2 | NULL |
| 2 | 12 | 0 |
| 3 | 13 | 0 |
| 4 | 9 | 1 |
| 5 | 7 | 1 |
| 6 | 6 | 1 |
| 7 | 8 | 0 |
| 8 | 3 | 1 |
| 9 | 10 | 0 |
| 10 | 1 | 1 |
| 11 | 18 | 0 |
| 12 | 4 | 1 |
| 13 | 6 | 0 |
| 14 | 0 | 1 |
| 15 | 2 | 0 |
| 16 | 8 | 0 |
| 17 | 14 | 0 |
| 18 | 7 | 1 |
| 19 | 15 | 0 |
| 20 | 11 | 1 |
| 21 | 12 | 0 |
| 22 | 7 | 1 |
| 23 | 20 | 0 |
| 24 | 17 | 1 |
| 25 | 8 | 1 |
| 26 | 6 | 1 |
| 27 | 6 | 1 |
| 28 | 12 | 0 |
| 29 | 3 | 1 |
| 30 | 18 | 0 |
| 31 | 1 | 1 |
| 32 | 12 | 0 |
+----+-------+------+
If all you want is the rows where n=1, then the query is actually even simpler...
SELECT a.*
FROM my_table a
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id
AND b.value >= a.value;
+----+-------+
| id | value |
+----+-------+
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 8 | 3 |
| 10 | 1 |
| 12 | 4 |
| 14 | 0 |
| 18 | 7 |
| 20 | 11 |
| 22 | 7 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 29 | 3 |
| 31 | 1 |
+----+-------+
I am trying to find account info for which the sum of available_balance is between two values. I tried to the following but it is not working as expected:
SELECT sum(a.avail_balance) `sum`
FROM account a
WHERE `sum` BETWEEN 5000 AND 10000
Result:ERROR 1054 (42S22): Unknown column 'sum' in 'where clause'
How can I accomplish my intended result?
My table:
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| account_id | product_cd | cust_id | open_date | close_date | last_activity_date | status | open_branch_id | open_emp_id | avail_balance | pending_balance |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
| 1 | CHK | 1 | 2000-01-15 | NULL | 2005-01-04 | ACTIVE | 2 | 10 | 1057.75 | 1057.75 |
| 2 | SAV | 1 | 2000-01-15 | NULL | 2004-12-19 | ACTIVE | 2 | 10 | 500.00 | 500.00 |
| 3 | CD | 1 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 2 | 10 | 3000.00 | 3000.00 |
| 4 | CHK | 2 | 2001-03-12 | NULL | 2004-12-27 | ACTIVE | 2 | 10 | 2258.02 | 2258.02 |
| 5 | SAV | 2 | 2001-03-12 | NULL | 2004-12-11 | ACTIVE | 2 | 10 | 200.00 | 200.00 |
| 7 | CHK | 3 | 2002-11-23 | NULL | 2004-11-30 | ACTIVE | 3 | 13 | 1057.75 | 1057.75 |
| 8 | MM | 3 | 2002-12-15 | NULL | 2004-12-05 | ACTIVE | 3 | 13 | 2212.50 | 2212.50 |
| 10 | CHK | 4 | 2003-09-12 | NULL | 2005-01-03 | ACTIVE | 1 | 1 | 534.12 | 534.12 |
| 11 | SAV | 4 | 2000-01-15 | NULL | 2004-10-24 | ACTIVE | 1 | 1 | 767.77 | 767.77 |
| 12 | MM | 4 | 2004-09-30 | NULL | 2004-11-11 | ACTIVE | 1 | 1 | 5487.09 | 5487.09 |
| 13 | CHK | 5 | 2004-01-27 | NULL | 2005-01-05 | ACTIVE | 4 | 16 | 2237.97 | 2897.97 |
| 14 | CHK | 6 | 2002-08-24 | NULL | 2004-11-29 | ACTIVE | 1 | 1 | 122.37 | 122.37 |
| 15 | CD | 6 | 2004-12-28 | NULL | 2004-12-28 | ACTIVE | 1 | 1 | 10000.00 | 10000.00 |
| 17 | CD | 7 | 2004-01-12 | NULL | 2004-01-12 | ACTIVE | 2 | 10 | 5000.00 | 5000.00 |
| 18 | CHK | 8 | 2001-05-23 | NULL | 2005-01-03 | ACTIVE | 4 | 16 | 3487.19 | 3487.19 |
| 19 | SAV | 8 | 2001-05-23 | NULL | 2004-10-12 | ACTIVE | 4 | 16 | 387.99 | 387.99 |
| 21 | CHK | 9 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 1 | 1 | 125.67 | 125.67 |
| 22 | MM | 9 | 2004-10-28 | NULL | 2004-10-28 | ACTIVE | 1 | 1 | 9345.55 | 9845.55 |
| 23 | CD | 9 | 2004-06-30 | NULL | 2004-06-30 | ACTIVE | 1 | 1 | 1500.00 | 1500.00 |
| 24 | CHK | 10 | 2002-09-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 23575.12 | 23575.12 |
| 25 | BUS | 10 | 2002-10-01 | NULL | 2004-08-28 | ACTIVE | 4 | 16 | 0.00 | 0.00 |
| 27 | BUS | 11 | 2004-03-22 | NULL | 2004-11-14 | ACTIVE | 2 | 10 | 9345.55 | 9345.55 |
| 28 | CHK | 12 | 2003-07-30 | NULL | 2004-12-15 | ACTIVE | 4 | 16 | 38552.05 | 38552.05 |
| 29 | SBL | 13 | 2004-02-22 | NULL | 2004-12-17 | ACTIVE | 3 | 13 | 50000.00 | 50000.00 |
+------------+------------+---------+------------+------------+--------------------+--------+----------------+-------------+---------------+-----------------+
You can only use aggregates for comparison in the HAVING clause:
GROUP BY ...
HAVING SUM(avail_balance) BETWEEN 5000 AND 10000
The HAVING clause requires you to define a GROUP BY clause.
Assuming, you want to SUM the balance for each cust_id, you need something like
SELECT sum(a.avail_balance) `sum`
FROM account a
GROUP BY `cust_id`
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000
SELECT sum(a.avail_balance)'sum'
FROM account a
WHERE a.avail_balance between 5000 and 10000
Aggregate functions(AVG,COUNT,MIN,MAX,SUM) for comparison can be achieved by using the "HAVING" clause which requires "GROUP BY" clause(relevant category column depending on the requirement).
As rightly pointed by Pankaj Gadge, cust_id is the most suitable for the requirement of the problem statement
SELECT SUM(a.avail_balance) 'Sum'
FROM account a
GROUP BY cust_id
HAVING SUM(a.avail_balance) BETWEEN 5000 AND 10000;
aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause
ex: group by column_name
having sum(avail_balance) between 5000 and 10000
Greetings I am trying to sum up expense value for each transaction.
Association table.
**Assoc table schema**
| PK_id | FK_transaction | FK_Expense |
|-------|----------------|------------|
| 1 | 1 | 85 |
| 2 | 2 | 81 |
| 3 | 3 | 77 |
| 4 | 4 | 83 |
| 5 | 5 | 84 |
| 6 | 6 | 105 |
| 7 | 7 | 104 |
| 8 | 8 | 71 |
| 9 | 8 | 88 |
| 10 | 8 | 90 |
Transaction table
**Transaction table schema**
| PK_id | type | value | confirmed_value |
|-------|------|-------|--------------------|
| 1 | 1 | 3.2 | 0 |
| 2 | 1 | 23.2 | 0 |
| 3 | 1 | 33.2 | 0 |
| 4 | 1 | 43.2 | 11.00 |
| 5 | 1 | 53.2 | 0 |
| 6 | 1 | 63.2 | 0 |
| 7 | 1 | 73.2 | 0 |
| 8 | 1 | 83.2 | 66.00 |
| 9 | 1 | 93.2 | 0 |
| 10 | 1 | 133.2 | 77.00 |
| 11 | 1 | 123.2 | 0 |
Expences Table
| PK_id | value |
|-------|-------|
| 85 | 3.2 |
| 81 | 23.2 |
| 77 | 33.2 |
| 83 | 43.2 |
| 84 | 53.2 |
| 105 | 63.2 |
| 104 | 73.2 |
| 71 | 83.2 |
| 88 | 93.2 |
| 90 | 133.2 |
Result ::
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 83.2 | 66 |
| 8 | 93.2 | 66 |
| 8 | 133.2 | 66 |
Desired - before calculations ( just to give u the idea )
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 309.6 | 66 |
Full desired result
count matching values COUNT(confirmed_value = value )
count entries that not match COUNT(confirmed_value != value )
/\ Calculate value of confirmed values
?? sum(transactions.confirmed_value)
and calculate value itself
Should be a row result ( example )
=======================================================================================================================
MATCHED | NOT_MATCHED | SUM(of values) | COUNT(of confirmed equal to value ) | COUNT(of confirmed ! equal to value )
=======================================================================================================================
1 | 10 | 3003.22 | 3 | 10
=======================================================================================================================
SQL Fiddle : http://sqlfiddle.com/#!9/2ab102/9
Thanks for any tips
Try this query -
SELECT transactions.PK_id,
SUM(expenses.value),
transactions.confirmed_value
FROM `assoc`
JOIN `expenses` ON `assoc`.`FK_Expense` = `expenses`.`PK_id`
JOIN `transactions` ON `assoc`.`FK_transaction` = `transactions`.`PK_id`
GROUP BY transactions.PK_id,
transactions.confirmed_value
Fiddle Demo - http://sqlfiddle.com/#!9/2ab102/14
mysql> select calendar.calId,calendar.cdate,lsUsers.userId,
apptActual.OPDID,count(*)
from calendar
LEFT JOIN apptActual on calendar.calId=apptActual.calendarId
LEFT JOIN lsUsers on lsUsers.userId=apptActual.OPDID
group by 1,2,3,4 order by 3
desc limit 10;
Result is:
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | NULL | NULL | 1 |
| 1287 | 2019-07-10 | NULL | NULL | 1 |
| 1295 | 2019-07-18 | NULL | NULL | 1 |
| 1303 | 2019-07-26 | NULL | NULL | 1 |
| 1311 | 2019-08-03 | NULL | NULL | 1 |
+-------+------------+--------+-------+----------+
10 rows in set (0.34 sec)
By using above query I am getting result as above, but I need result result like below (all userId's has to come.)
+-------+------------+--------+-------+----------+
| calId | cdate | userId | OPDID | count(*) |
+-------+------------+--------+-------+----------+
| 76 | 2016-03-16 | 11 | 11 | 1 |
| 75 | 2016-03-15 | 11 | 11 | 1 |
| 77 | 2016-03-17 | 9 | 9 | 1 |
| 75 | 2016-03-15 | 8 | 8 | 1 |
| 75 | 2016-03-15 | 1 | 1 | 1 |
| 1279 | 2019-07-02 | 4 | NULL | 1 |
| 1287 | 2019-07-10 | 21| NULL | 1 |
| 1295 | 2019-07-18 | 3 | NULL | 1 |
| 1303 | 2019-07-26 | 7 | NULL | 1 |
| 1311 | 2019-08-03 | 5 | NULL | 1 |
+-------+------------+--------+-------+----------+
Please suggest me how to get result as above!
If you join lsUsers on userId = appActual.OPDID you will get what you ask for: usedId (NULL) = OPDID (NULL). Is there any other way to join lsUsers?