I have following table structure, with many tables like this:
data_1:
+-------+--------+-------+
| views | clicks | date |
+-------+--------+-------+
| 29 | 1 | ..... |
| 458 | 9 | ..... |
+-------+--------+-------+
....
data_43:
+-------+--------+-------+
| views | clicks | date |
+-------+--------+-------+
| 0 | 0 | ..... |
| 0 | 0 | ..... |
+-------+--------+-------+
...
My question here is, how can I get all the sum values in one query?
I tried it with a simple join:
mysql> SELECT SUM(t1.views) data_1_views,
SUM(t1.clicks) data_1_clicks,
SUM(t2.views) data_43_views,
SUM(t2.clicks) data_43_clicks
FROM data_1 t1, data_43 t2;
But my result isn't what I expected:
+--------------+---------------+---------------+----------------+
| data_1_views | data_1_clicks | data_43_views | data_43_clicks |
+--------------+---------------+---------------+----------------+
| NULL | NULL | NULL | NULL |
+--------------+---------------+---------------+----------------+
I expected it to be:
+--------------+---------------+---------------+----------------+
| data_1_views | data_1_clicks | data_43_views | data_43_clicks |
+--------------+---------------+---------------+----------------+
| 487 | 10 | 0 | 0 |
+--------------+---------------+---------------+----------------+
Anyone here who can help me with this query?
I am also interested if this is more performant than doing all this stuff in multiple queries
In this case multiple queries are fine especially if you are grouping many rows. There is virtually no benefit (and actually potential bugs) lumping these two tables in one query.
I would just make two queries
SELECT SUM(t1.views) data_1_views, SUM(t1.clicks) data_1_clicks
FROM data_1 t1
SELECT SUM(t2.views) data_43_views, SUM(t2.clicks) data_43_clicks
FROM data_43 t2;
Use Union Query.
Like below:
SELECT sum(t.data_1_views) as data_1_views,SUM(t.data_1_clicks) as data_1_clicks,
SUM(t.data_43_views) as data_43_views,SUM(t.data_43_views) as data_43_views
FROM (
SELECT SUM(t1.views) data_1_views,SUM(t1.clicks) data_1_clicks,
0 as data_43_views,0 as data_43_clicks
FROM FROM data_1 as t1
UNION
SELECT 0 as data_1_views,0 as data_1_clicks,SUM(t1.clicks) as data_43_views, SUM(t2.clicks)
FROM data_43_clicks as t2
) as t GROUP BY 1
Related
Hello i have two tables with same structure and now I want merge it.
Here is structure:
Terms:
steamid - that goes without saying
regcas - keep only a smaller value
VIP - sum
FunVIP - ignore when duplicate
Days - sum
KilledCT - sum
WinPP - sum
LastT - sum
cas - sum
lastnick - ignore when duplicate
lastlog - ignore when duplicate
ct_cas - sum
simon_cas - sum
Example when duplicate:
row from main table
steamid | regcas | VIP | FunVIP | Days | KilledCT | WinPP | LastT | lastnick | lastlog | ct_cas | simon_cas
------------------------------------------------------------------------------------------------------------------------------
76561198040874389 | 1546639030 | 1 | 0 | 125 | 1000 | 20 | 50 | Bomber | 1546639037 | 64 | 50
row from second table
steamid | regcas | VIP | FunVIP | Days | KilledCT | WinPP | LastT | lastnick | lastlog | ct_cas | simon_cas
------------------------------------------------------------------------------------------------------------------------------
76561198040874389 | 1553888234 | 1 | 5 | 100 | 1555 | 40 | 20 | Lucker | 1549387793 | 10 | 1
Result
steamid | regcas | VIP | FunVIP | Days | KilledCT | WinPP | LastT | lastnick | lastlog | ct_cas | simon_cas
------------------------------------------------------------------------------------------------------------------------------
76561198040874389 | 1546639030 | 2 | 0 | 225 | 2555 | 60 | 70 | Bomber | 1546639037 | 74 | 51
I absolutely don't know how to compose a complex SQL statement and I need help.
You seem to want union all and group by. I have no idea what "ignore with duplicate" is supposed to mean, but min() seems close enough. So:
select steamid, min(regcas) as regcas, sum(vip) as vip),
min(FunVIP) as FunVIP,
sum(Days) as days, sum(KilledCT) as KilledCT, sum(WinPP) as WinPP,
sum(LastT) as LastT, sum(cas) as cas,
min(lastnick) as lastnick,
min(lastlog) as lastlog,
sum(ct_cas) as ct_cas, sum(simon_cas) as simon_cas
from ((select t1.* from table1 t1) union all
(select t2.* from table2 t2)
) t2
group by steamid;
To merge two tables.Can use join tables command.
select*from natural join ;
OR [1]
select*from, where column.table1=column.table2;
I have a table that looks more or less like this:
user is_su last_login roles_for_groups
+------+---+------------+----------------------------+
| rob | 1 | 2018-02-09 | admin, read |
+------+---+------------+----------------------------+
| gian | 0 | 2018-06-21 | prod_full_access, readOnly |
+------+---+------------+----------------------------+
| gian | 0 | 2018-06-21 | prod_full_access, CCT |
+------+---+------------+----------------------------+
| rob | 1 | 2018-02-09 | admin, write |
+------+---+------------+----------------------------+
and I would like to merge into a single row all the rows with the same user, in a way such that the table will look like this:
+------+---+------------+---------------------------------+--+
| rob | 1 | 2018-02-09 | admin, read, write | |
+------+---+------------+---------------------------------+--+
| gian | 0 | 2018-06-21 | prod_full_access, readOnly, CCT | |
+------+---+------------+---------------------------------+--+
How can I achieve this?
Why not just query out the data the way you want, using GROUP_CONCAT with DISTINCT:
SELECT
user,
is_us,
last_login,
GROUP_CONCAT(DISTINCT roles_for_groups) roles_for_groups
FROM yourTable
GROUP BY
user,
is_us,
last_login;
It isn't clear whether the is_us and last_login columns are always functionally dependent on the user. If not, then you should include some sample data which reveals this behavior.
select a.user,a.is_su,a.last_login,group_concat(distinct a.roles_for_groups separator ',' )
from (select user,is_su,last_login,substring_index(roles_for_groups,',',1) as roles_for_groups from table union select user,is_su,last_login,substring_index(roles_for_groups,',',-1) as roles_for_groups from table) a group by a.user
this query is only for roles_for_groups containing two values
I have 2 tables, called tb_compulsory_savings and tb_time_deposits
for tb_compulsory_savings
form_no | badge_id | compulsory_savings | transaction_date
1 | 090802 | 50000 | 2016-05-20 13:10:11
2 | 090801 | 50000 | 2016-05-20 13:15:15
for tb_time_deposits
form_no | badge_id | time_deposits | transaction_date | period | status
1 | 090802 | 100000 | 2016-05-20 13:20:44 | May-2016 | closed
Now I want to query that 2 table to be like this:
badge_id | compulsory_savings | time_deposits
090802 | 50000 | 100000
090801 | 50000 | null
So, if badge_id: 090801 is not existed in tb_time_deposits, it will display null.
I have tried LEFT JOIN, RIGHT JOIN, UNION but still the display is not as I want.
Looks like you're looking for a left join:
SELECT cs.badge_id, cs.compulsary_savings, td.time_deposits
FROM tb_compulsory_savings cs
LEFT JOIN tb_time_deposits td ON cs.badge_id = td.badge_id
Try this query might work :
SELECT badge_id,compulsory_savings,time_deposit FROM
tb_compulsory_savings LEFT JOIN tb_time_deposits ON
tb_compulsory_savings.badge_id=tb_time_deposits.badge_id;
I'm having BookTable in database (with foregin hey LibID):
| BookID | BookName | BookPrice | LibID |
-------------------------------------------
| 1 | Book_1 | 200 | 1 |
| 2 | Book_2 | 100 | 1 |
| 3 | Book_3 | 300 | 2 |
| 4 | Book_4 | 150 | 4 |
and also LibraryTable:
| LibID | LibName | LibLocation |
-----------------------------------
| 1 | Lib_1 | Loc_1 |
| 2 | Lib_2 | Loc_2 |
| 3 | Lib_3 | Loc_3 |
| 4 | Lib_4 | Loc_4 |
I need to write SQL query that will return be the info about the library and number of books for that library:
| LibID | LibName | NumberOfBooks|
------------------------------------
| 1 | Lib_1 | 2 |
| 2 | Lib_2 | 1 |
| 3 | Lib_3 | 0 |
| 4 | Lib_4 | 1 |
It should be one SQL query, probably with nested queries or joins.. Not sure how the query should look like:
SELECT L.LibID AS LibID, L.LibName AS LibName, COUNT(B) AS NumberOfBooks
FROM LibraryTable L, BookTable B
WHERE L.LibID = B.LibID
Will that work?
No, this query will not work. COUNT aggregates data, so you must explicitely tell the DBMS for which group of data you want the count. In your case this is the library (you want one result record per library).
COUNT's parameter is a column, not a table, so change this to * (i.e. count records) or a certain column (e.g. LibID).
The join syntax you are using is valid, but deprecated. Use explicit joins instead. In your case an outer join would even show libraries that have no books at all, if such is possible.
select l.libid, l.libname, count(b.libid) as numberofbooks
from librarytable l
left outer join booktable b on b.libid = l.libid
group by l.libid;
You could also do all this without a join at all and get the book count in a subquery instead. Then you wouldn't have to aggregate. That's way simpler and more readable in my opinion.
select
l.libid,
l.libname,
(select count(*) booktable b where b.libid = l.libid) as numberofbooks
from librarytable l;
SELECT lt.LibID AS LibID, lt.LibName AS LibName, count(*) AS NumberOfBooks
FROM BookTable AS bt
LEFT JOIN LibraryTable AS lt ON bt.LibID = lt.LibID
GROUP BY bt.LibID
I'd like to query the table which stores the entries somehow mixed in rows and columns.
Here is the table:
| id | class | field | value |
|-----|-------|-------|-------|
| 1 | 1 | a | AA |
| 2 | 1 | b | BB |
| 3 | 1 | c | CC |
| 4 | 2 | a | DD |
| 5 | 2 | b | EE |
| 6 | 2 | c | FF |
What should be the query to get a result like:
a)
| class | new_a | new_c |
|-------|-------|-------|
| 1 | AA | CC |
| 2 | DD | FF |
My pseudo query I imagine it would be something like:
select class, value(where field=a) as new_a, value(where field=c) as new_c, from table;
b)
| class | new_a | new_c |
|-------|-------|-------|
| 2 | DD | FF |
For this one I guess it should be like:
select class, value(where field=a) as new_a, value(where field=c) as new_c, from table where class = '2';
Unfortunatelly I'm rarely using the mysql and I'm not sure how to build this query. All constructive suggestions are appreciated.
Try this query
For a) The query is
SELECT t1.class, t1.value as new_a, t2.value as new_b
FROM table t1
JOIN table t2 ON(t2.class=t1.class )
WHERE t1.field='a' AND t2.field='c'
For b) The query is
SELECT t1.class, t1.value as new_a, t2.value as new_b
FROM table t1
JOIN table t2 ON(t2.class=t1.class )
WHERE t1.field='a' AND t2.field='c' AND t1.class='2'
1) you are trying to convert the rows into columns so I joined the same table twice with condition as 2 tables should have same 'class' value
2) then added condition as what to fetch from table t1 and table t2 as t1.field='a' and t2.field='c'
3) In second query you need only the class value '2', so i added the condtion as t1.class=2