I have 2 tables, first one keeps names and second is related to it on in = cid. I need to get only the highest date row from second table, once. Please look below for clearer explanation:
table1 a
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
+----+-------+
table2 c
+----+-------+------------+
| id | cid | galiojaiki |
+----+-------+------------+
| 1 | 1 | 2015-04-30 |
| 2 | 1 | 2015-09-30 |
| 3 | 1 | 2015-03-10 |
| 4 | 2 | 2015-06-30 |
| 5 | 2 | 2015-07-30 |
| 6 | 3 | 2015-05-11 |
| 7 | 4 | 2015-05-10 |
+----+-------+------------+
Expected result:
+------------+-------+
| galiojaiki | name |
+------------+-------+
| 2015-09-30 | name1 |
| 2015-07-30 | name2 |
| 2015-05-11 | name3 |
| 2015-05-11 | name4 |
+------------+-------+
My query:
SELECT a.*, c.galiojaiki FROM `y6fdt_igym_abonementai` AS a
INNER JOIN
(
SELECT max(galiojaiki) FROM y6fdt_igym_sutartys
) c
on c.cid= a.id
GROUP BY c.abonementas
How anout a simple aggregation using MAX?
Something like
SELECT a.name,
MAX(b.galiojaiki) as galiojaiki
FROM `y6fdt_igym_abonementai` AS a INNER JOIN
`y6fdt_igym_sutartys` as b ON a.ID = b.CID
GROUP BY a.name
SQL Fiddle DEMO
Related
I have a MySQL database including following tables that used to maintain transactions of some documents.
tbl_documents Table
+----+---------+------+---------+
| id | file_no | name | subject |
+----+---------+------+---------+
| 1 | A/10 | F1 | a |
| 2 | A/11 | F2 | b |
| 3 | A/12 | F3 | c |
| 4 | A/13 | F4 | d |
+----+---------+------+---------+
tbl_requests
+----+-------------+----------------+---------------+
| id | document_id | requested_date | approved_date |
+----+-------------+----------------+---------------+
| 1 | 1 | 2019-12-01 | 2019-12-02 |
| 2 | 2 | 2019-12-08 | 2019-12-08 |
+----+-------------+----------------+---------------+
tbl_issues
+----+-------------+------------+
| id | document_id | issue_date |
+----+-------------+------------+
| 1 | 1 | 2019-12-05 |
| 2 | 2 | 2019-12-10 |
+----+-------------+------------+
I want to get the following / Desired output by joining above three tables.
Desired Output
+---------+------+---------+----------------+---------------+------------+
| file_no | name | subject | requested_date | approved_date | issue_date |
+---------+------+---------+----------------+---------------+------------+
| A/10 | F1 | a | 2019-12-01 | 2019-12-02 | 2019-12-05 |
| A/11 | F2 | b | 2019-12-08 | 2019-12-08 | 2019-12-10 |
+---------+------+---------+----------------+---------------+------------+
To do that, I used the following query
select tbl_documents.file_no, tbl_documents.name, tbl_documents.subject, requested_date, approved_date, tbl_issues.issue_date
from tbl_documents
right join tbl_requests on tbl_requests.document_id=tbl_documents.id
right join tbl_issues on tbl_issues.document_id=tbl_documents.id
But didn't get the expected output. Can anyone help ?
Just use inner joins, as in:
select
d.file_no,
d.name,
d.subject,
r.requested_date,
r.approved_date,
i.issue_date
from tbl_documents d
join tbl_requests r on r.document_id = d.id
join tbl_issues i on i.document_id = d.id
I have table 'articles'
+-------------+
| articles |
+----+--------+
| id | title |
+----+--------+
| 1 | title1 |
+----+--------+
| 2 | title2 |
+----+--------+
| 3 | title3 |
+----+--------+
table 'catalogue'
+---------------------+
| catalogue |
+----+--------+-------+
| id | group | name |
+----+--------+-------+
| 1 | group1 | name1 |
+----+--------+-------+
| 2 | group1 | name2 |
+----+--------+-------+
| 3 | group2 | name3 |
+----+--------+-------+
| 4 | group2 | name4 |
+----+--------+-------+
binding table 'bindTable'
+------------+--------------+-------+
| bindTable |
+------------+--------------+-------+
| id_article | id_catalogue | value |
+------------+--------------+-------+
| 1 | 2 | 1 |
+------------+--------------+-------+
| 1 | 3 | 4 |
+------------+--------------+-------+
| 3 | 1 | 2 |
+------------+--------------+-------+
| 3 | 3 | 1 |
+------------+--------------+-------+
| 3 | 4 | 3 |
+------------+--------------+-------+
and i need to get result as in table 'result', where i can get pairs "catalogue_name : value" for selected item from table 'article'
+-----------------------------------------------------+
| result |
+------------+---------------+----------------+-------+
| article_id | article_title | catalogue_name | value |
+------------+---------------+----------------+-------+
| 1 | title1 | group1_name2, | 1 |
| | | group2_name3 | 4 |
+------------+---------------+----------------+-------+
| 3 | title3 | group1_name1, | 2 |
| | | group2_name3, | 1 |
| | | group2_name4 | 3 |
+------------+---------------+----------------+-------+
Can anyone tell me a query string with one DB query? Thank you for attention.
My vision:
SELECT b.id_article, a.title, c.group, c.name, b.value
FROM bindTable b
JOIN articles a ON a.id = b.id_articles
JOIN catalogue c ON c.id = b.id_catalogue
WHERE b.id_article = 1
but i need one row with pairs c.name&b.value for one a.id
A select with inner join
select a.article_id, a.article_title, b.catalogue_name, b.value
from bindTable as c
inner join articles as a on a.id = c. article_id
inner join catalogues as b on c. id_catalogue = b.id
This should do it.
SELECT articles.id AS article_id,
articles.title AS article_title,
CONCAT_WS('_', catalogue.group, catalogue.name) AS catalogue_name,
bindTable.value AS value
FROM bindTable
INNER JOIN articles ON bindTable.id_article = articles.id
INNER JOIN catalogue ON bindTable.id_catalogue = catalogue.id
Generated results that look like this:
------------------------
|GROUP A|GROUP B|AMOUNT|
------------------------
| A | 1 | 5 |
| A | 2 | 4 |
| B | 1 | 4 |
| B | 2 | 2 |
| B | 3 | 6 |
| C | 1 | 1 |
| C | 2 | 4 |
------------------------
Looking to sum in a new column the total of GROUP A that repeats along GROUP B:
---------------------------------------
|GROUP A|GROUP B|AMOUNT|SUM of GROUP A|
---------------------------------------
| A | 1 | 5 | 9 |
| A | 2 | 4 | 9 |
| B | 1 | 4 | 12 |
| B | 2 | 2 | 12 |
| B | 3 | 6 | 12 |
| C | 1 | 1 | 5 |
| C | 2 | 4 | 5 |
---------------------------------------
I'm using several derived and joined tables, so hoping this would be easy enough to calculate within the current SELECT statement or another derived table. Any ideas? Can't add additional rows using WITH ROLLUP modifier.
Try something like this:
SELECT t1.*, SumOfGroup_A
FROM mytable AS t1
INNER JOIN (
SELECT GROUP_A, SUM(AMOUNT) AS SumOfGroup_A
FROM mytable
GROUP BY GROUP_A
) t2 ON t1.GROUP_A = t2.GROUP_A
Demo here
I've tried the following queries but unfortunately they don't work :(.
Worth mentioning that each customer has more than one CustomerUsers
select (a.TotalJobs / b.DaysActive) from
(select count(jr.id) as TotalJobs
from jobrequests jr, customers c, customerusers cu
where jr.customeruserid=cu.id
and cu.customerid=c.id
group by c.name) as a,
(select datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.name) as b
Please see below the tables
Jobs:
+----+--------------+
| ID | JobRequestID |
+----+--------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 3 |
| 11 | 4 |
| 12 | 4 |
| 13 | 5 |
| 14 | 5 |
| 15 | 6 |
| 16 | 7 |
| 17 | 8 |
| 18 | 8 |
| 19 | 9 |
| 20 | 10 |
+----+--------------+
JobRequests:
+----+---------------+
| ID | CustomeUserID |
+----+---------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
| 7 | 4 |
| 8 | 4 |
| 9 | 4 |
| 10 | 5 |
| 11 | 5 |
| 12 | 5 |
| 13 | 6 |
| 14 | 6 |
| 15 | 7 |
+----+---------------+
CustomerUsers:
+----+------------+
| ID | CustomerID |
+----+------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 2 |
| 7 | 2 |
| 8 | 3 |
| 9 | 3 |
| 10 | 4 |
+----+------------+
Customers:
+----+------+--------------+
| ID | Name | CreationTime |
+----+------+--------------+
| 1 | a | 1415814194 |
| 2 | b | 1415814194 |
| 3 | c | 1415986994 |
| 4 | d | 1415986994 |
+----+------+--------------+
For the moment it returns 16 results (4X4), dividing each result from 1st sub-query to each result from the 2nd one (each of these sub-queries return 4 results). Can anyone please help me to get this to divide only 1 result from sub-query 1 to it's corespondent from sub-query 2?
Thank you in advance.
I suspect that you can do what you want this a query like this:
select c.name, count(*) / (datediff(curdate(), from_unixtime(c.CreationTime))
from customerusers cu join
jobrequests jr
on jr.customeruserid = cu.id join
customers c
on cu.customerid = c.id
group by c.name;
I don't see why you need two subqueries for this.
I'm guessing you need to join your results together -- as currently written, you're producing a cartesian product.
Try something like this adding c.id to each subquery (it's better to group by it presumably rather than the name):
select (a.TotalJobs / b.DaysActive)
from (
select c.id,
count(jr.id) as TotalJobs
from jobrequests jr
join customers c on jr.customeruserid=cu.id
join customerusers cu on cu.customerid=c.id
group by c.id) a join (
select c.id,
datediff(curdate(), from_unixtime(c.CreationTime)) as DaysActive
from customers c
group by c.id) b on a.id = b.id
Please note, I've updated your syntax to use the more standard join syntax.
I am having difficulty assembling the proper sql statements to list and sort data based upon my needs. Below is the structure of two tables I need to select data from.
For each user in the users table, I need to list id, name, and key[a] and key[b] from users_nfo table.
Table users:
+----+------+
| id | name |
+----+------+
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
+----+------+
Table users_nfo:
+----+-----+-----+-------+
| id | uid | key | value |
+----+-----+-----+-------+
| 1 | 1 | a | 22 |
| 2 | 1 | b | 47 |
| 3 | 2 | a | 38 |
| 4 | 2 | b | 16 |
| 5 | 3 | a | 27 |
| 6 | 3 | b | 67 |
| 7 | 4 | a | 75 |
| 8 | 4 | b | 67 |
| 9 | 5 | a | 63 |
| 10 | 5 | b | 67 |
+----+-----+-----+-------+
The result should be similar to this
Array result:
+----+------+---+---+
| id | name | a | b |
+----+------+---+---+
| 1 | aa |22 |47 |
| 2 | bb |38 |16 |
| 3 | cc |27 |67 |
| 4 | dd |75 |67 |
| 5 | ee |63 |67 |
+----+------+---+---+
Additionally, I need to be able to sort by any column key, such as sorted asc by b.
Help is appreciated. Thanks in advance!
The trick is to join the users_nfo (sic) table twice, and include the key column in the join condition. Like this:
SELECT u.ID, u.name, n1.value, n2.value from USERS u
JOIN users_nfo n1
ON u.id = n1.id AND n1.key = 'a'
JOIN users_nfo n2
ON u.id = n2.id AND n1.key = 'b'
ORDER BY n2.value ASC