Mysql get the amount of products - mysql

I have the following two tables
# vendor table
vendor_id host
1 192.168.0.2
1 192.168.0.4
1 192.168.0.6
2 192.168.1.21
2 192.168.1.23
2 192.168.1.25
2 192.168.1.27
# information table
host name
192.168.0.2 bar
192.168.0.4 bar1
What I need at the end is the following result set
vendor_id amount_live amount_total
1 2 3
2 0 4
The column amount_live is the amount of entries per vendor in the information table and the column amount_total is the amount of hosts in the vendor table per vendor.
Can some of the experts please tell me the mysql select statement to get the desired result set. Thanks.

You can do this with count and an outer join:
select v.vendor_id,
count(i.host) amount_live,
count(*) amount_total
from vendor v
left join information i on v.host = i.host
group by v.vendor_id
SQL Fiddle Demo

Related

how to get multiple 'group by' outputs as columns in one query in MySQL

I have a table with 3 columns
table data and structure:
id|username|work_type|points
----------
1|user 1 |walking |1500
2|user 1 |running |1500
3|user 2 |walking |2500
4|user 3 |running |1500
5|user 4 |walking |1500
6|user 4 |walking |1200
I need this output:
username|walking|running|points
user 1 | 1| 1|3000
user 2 | 1| 0|2500
user 3 | 0| 1|1500
user 4 | 2| 0|2700
what I have done:
Select * FROM (
Select Count(id),username as "username" from table where work_type='walking' group by username)
USERNAME LEFT JOIN
Select Count(id),username as "walking" from table where work_type='walking' group by username)
WALKING ON USERNAME.username=WALKING.username LEFT JOIN
Select Count(id),username as "running" from table where work_type='running' group by username)
RUNNING ON USERNAME.username=RUNNING.username LEFT JOIN
Select SUM(points),username as "point" from table group by username)
POINTS ON USERNAME.username=POINTS.username)
the above query gives the correct output, but since it takes 4 rounds in the same table it takes more time. this is only a logical structure of my actual DB table but it's similar. where it takes 1.4-2.1 Seconds since there are over 50k rows in the lowest one. I need to run the same query for 17 tables like this in one go. my time limit is about 20 seconds with about 2M+ rows. currently, it takes over 1 minute.
I don't think my SQL query is efficient. is there some way I can speed this up?. this is a DB that has been running for 3 years+ for commercial uses, so I cannot change the structures.
these tables also need a left join from another table as well, but that I can build a different system for that if needed.
This kind of question is asked rather often. Generally, I think it's best resolved in application code, but anyway...
SELECT username
, SUM(work_type = 'running') running
, SUM(work_type = 'walking') walking
, SUM(points) total
FROM my_table GROUP BY username;
The 'application code' version might look something like this:
SELECT username
, work_type
, COUNT(*) cnt
, SUM(points) total
FROM my_table
GROUP
BY username
, work_type;

find the row which has the highest sequence number in a table for a specific ID that is in another Table

I'm have 2 tables:
Table 1:
Job #
XLG24
ABC123
XYZ123
Table 2:
Sequence # Job # Display Order
1 XLG24 1
2 XLG24 2
2 ABC123 1
3 ABC123 2
4 ABC123 3
4 XYZ123 1
I am trying to pull the Job # and Sequence # that are found in Table 1 from Table 2. however, when i try it only pulls the job #s found in Table 1 that have the highest sequence # (in this case only Sequence # 4).
This is my code:
select * from TABLE2 where JOB# IN
(SELECT JOB# FROM table1)
and SEQUENCE# = (select max (SEQUENCE#) from Table2)
order by Display_order desc
The output of my code is like this:
Sequence # Job #
4 ABC123
4 XYZ123
I want it to look like:
Sequence # Job #
2 XLG24
4 ABC123
4 XYZ123
So in summary, I am trying to only find the row which has the highest sequence number in Table 2 for a specific ID that is in Table 1
Can someone please give any advice?
Assuming table1 has unique entries (job number only shows up once), and table2 has multiple entries (I'm sure these are both just example names for this post), then I think you want to join table1 to table2 and then find the max.. like so:
SELECT t1.job# jobNum,max(t2.Sequence#) maxSeq
FROM table1 t1 LEFT JOIN table2 t2 ON t1.job#=t2.job#
This will include all entries in table1 - even if they're not found in table2 - if not found in table2 then maxSeq will contain NULL. If all entries are in both tables, or you only want all entries found in both, you'll want an INNER JOIN instead.

Fetching crossed records from database

I have the following table structure (simplified):
id | structure_id | structure_hash_id
1 1 1
2 1 2
3 1 3
4 2 4
5 2 1
6 3 2
As you can see, all structures contain many structure hashes. What I want to fetch is information for each structure id, how many structure hashes it contains exist in other structures. So for this example it'd be:
structure_id #1: 2
structure_id #2: 1
structure_id #3: 1
The query I wrote for this is:
SELECT contains.structure_id, COUNT(contains.structure_hash_id)
FROM (
SELECT *
FROM structureTable st
WHERE structure_id = 1
) AS contains
INNER JOIN (
SELECT *
FROM structureTable st
WHERE structure_id != 1
) AS notcontains
ON contains.structure_hash_id = notcontains.structure_hash_id
GROUP BY contains.structure_id;
It works, I wrote it from memory, I don't remember how I wrote it earlier as I deleted it, but you got the idea.
But the problem is that in real table I've got ~500mln records and some other columns, so for each structure_id the query execution time is huge (> 15min).
Also, I have type structure_id manually, while I'd like to have them all as a result like I gave an example at the top of this post.
How can I solve this problem?
You can achieve this with self join and group by.
Here is the way to do that:
select
t1.structure_id ,
count(t1.structure_id ) as count
from structure t1
inner join structure t2 on t1.structure_id !=t2.structure_id
and t1.structure_hash_id=t2.structure_hash_id
group by t1.structure_id
SQL Fiddle Example: http://sqlfiddle.com/#!9/678bf7/1/0

SQL Query to Find IDs which are allocated to all available Room_Nos

I have a very simple problem to find IDs in SQL which are allocated to all available Room_No. For Example: In following table there are total three unique rooms i.e. 100,400 and 600 and there is only one ID i.e. 1 which is allocated to all available rooms. And I am not able to find a correct query to do this. Please Help
Table Data
Room_No Id
400 1
100 2
600 3
100 1
400 1
600 1
YOu can do this with aggregation and a having clause:
select id
from data
group by id
having count(distinct room_no) = (select count(distinct room_no) from data);

Join from two tables mysql

I have a doubt where trying to join two tables by a previous search. I've looked several solutions and read some chapters in a mysql book but I think I'm pretty close to the right answer but still not get it
I have this table "userprocess":
idProcess username state
------------------------------------------
1 blisssing 3
2 enriquecalvera 1
2 africabaluja 2
1 enriquecalvera 3
2 blisssing 1
The primery key for this table is the union of idProceso+username.
I have this other table "user":
index username pass active tipeUser .... so on
----------------------------------------------------------------- ----
1 blisssing 6OiZVVUPi3LDE 1 user
2 carmen 6OOtfrXB2Nu5. 1 user
3 consuelo 6OgdhVSkr1VDs 1 user
4 africabaluja 6OoPtGjWMQARE 1 user
5 enriquecalvera 6O6tvHg.122uQ 1 user
The thing is I want to show the join of the two tables but with a search within the first table. If I run this query
SELECT username FROM userprocess where idProcess='1' ORDER BY state
I get this:
username
---------
blisssing
enriquecalvera
which is what I am looking for, but I want to show all the fields in the "user" table for those usernames ordered by idProceso. So I run this other query:
SELECT *
FROM
user u,
userprocess p
WHERE
u.username=p.username
AND u.username IN (
SELECT username
FROM userprocess
where idProcess='1'
ORDER BY username
) ORDER BY p.state
I got this:
username pass active tipeUser idProcess state
----------------------------------------------------------------------
blisssing 6Od3nSkfOiwlg 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 2 1
enriquecalvera 6Oc9usiDEk51U 1 user 1 3
blisssing 6Od3nSkfOiwlg 1 user 2 3
But this is not what I want I just want the same two results as in the previous query but with all the columns of the result of joining the two tables..
I know there is a lot of questions like this, but I have tried a lot of things and still not having the desire result..
What am I missing?
thank you, if you have any qestion or doubt just ask :)
The reason you're seeing multiple results is because you're joining on just the username, but of course the userprocess table has 2 rows where username = enriquecalvera. Your subquery is correctly only returning the 1 row you're interested in (where idprocess = 1) but as your join is seperate to this, and therefore doesn't include the idprocess = 1 condition, you're getting both rows back.
You should just do this in one step with a join like this:
SELECT *
FROM
user u
INNER JOIN userprocess p on u.username=p.username and p.idProcess='1'
ORDER BY p.state