How to calculate rows in sql - mysql

I have a table:
+---------+-----------+--------------+
| acc_id | dns_id | mail_name |
+---------+-----------+--------------+
| 1| 5 | myac1 |
| 2| 5 | myac2 |
+---------+-----------+--------------+-
I showed to you two records. I have a lot of acc_id, but I have the same dns_id for a lot of this acc_id. How can I calculate how many acc_id in the same dns_id. I have not only dns_id = 5, I have other values. How can I calculate for each dns_id number of `acc_id'?
According to my table, I have two acc_id in one dns_id.

You can use count(acc_id) in the select statement and a group by with the column dns_id. It would look something like this::
SELECT dns_id, count(acc_id) FROM [TABLE_NAME]
GROUP BY dns_id

Try GROUP BY and count():
SELECT dns_id, COUNT(*) FROM table GROUP BY dns_id;
It groups the results by different dns_id and counts the number of rows that were grouped.

Related

How to compute occurence probability of rows inside a SQL (MySQL) table using a single query?

I have a table containing some similar rows representing objects for a game. I use this table as a way to select objects randomly. Of course, I ignore the size of the table. My problem is that I would like to have a single query that returns the probability to select every object and I don't know how to proceed.
I can get the total number of objects I have in my table:
select count(id) from objects_desert_tb;
Which returns
+-----------+
| count(id) |
+-----------+
| 81 |
+-----------+
1 row in set (0.00 sec)
and I have a query that return the number of occurence of every object in the table:
select name, (count(name)) from objects_desert_tb group by name;
which gives:
+-------------------+---------------+
| name | (count(name)) |
+-------------------+---------------+
| carrots | 5 |
| metal_scraps | 14 |
| plastic_tarpaulin | 8 |
| rocks_and_stones | 30 |
| wood_scraps | 24 |
+-------------------+---------------+
5 rows in set (0.00 sec)
Computing the probability for every object just consist in doing (count(name)) divided by the total number of rows in the table. For example with the row carrots, just compute 5/81, from the two queries given above. I would like a single query that would return:
+-------------------+---------------+
| carrots | 5/81 = 0.06172839
| metal_scraps | 0.1728...
| plastic_tarpaulin | 0.09876...
| rocks_and_stones | 0.37...
| wood_scraps | 0.29...
+-------------------+---------------+
Is there a way to use the size of the table as a variable inside a SQL query? Maybe by nesting several queries?
Cross join your queries:
select c.name, c.counter / t.total probability
from (
select name, count(name) counter
from objects_desert_tb
group by name
) c cross join (
select count(id) total
from objects_desert_tb
) t
In MySQL 8+, you would just use window functions:
select name, count(*) as cnt,
count(*) / sum(count(*)) over () as ratio
from objects_desert_tb
group by name;

Find multiple totals by adding values from mysql table

I need to create a number adding all the values i can find in the db related to a specific customer.
Ex.
| Cust. | Value |
| 1 | 3 |
| 2 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 5 |
The result i want is : Customer #1 = 4, Customer #2 = 2; Customer #3 = 5.
There is a way to do that right into the mysql query?
Try Below query.
Select CONCAT('Customer #' , cust) as customer , sum(Value)
FROM customer_table
Group By cust
You want to SUM the values with a specific GROUP BY clause. Think of the GROUP BY as dividing rows into buckets and the SUM as aggregating the contents of those buckets into something useful.
Something like:
SELECT SUM(Value) FROM table GROUP BY Cust

Query to Find Duplicate entries

I am looking for an SQL query to give me a list of duplicate entries in a table. However, there are 3 different columns to take into account. First is an ID, Second is a Name, and third is a Date. The situation is that there are multiple Names that are assigned with the same ID, and there are multiple records of those in a day, which makes THOUSANDS of different records per day.
I already filtered it so that only results for the past 7 days will show, but the amount of records is still too much for me to extract. I just want to decrease the number of rows in the output order to properly extract the results.
Sample
|--id-|--name--|-------date------|
| 1 | a |5-9-2015, 10:00am|
| 1 | a |5-8-2015, 10:02am|
| 1 | a |5-8-2015, 11:00am|
| 1 | b |5-8-2015, 10:00am|
| 1 | b |5-8-2015, 10:02am|
| 1 | c |5-8-2015, 10:00am|
| 2 | d |5-8-2015, 10:00am|
expected output
|--id-|--name--|
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | d |
Inclusion of entries without any duplicates are fine. The important thing is to only return a single record of a unique id-name combination for a day.
Thanks in advance for any help that you can give.
You can get the combinations as:
select distinct id, name
from sample;
If you want duplicates, using group by and having:
select id, name
from sample
group by id, name
having count(*) > 1;
EDIT:
If you want this by date, then add date(date) to the group by and perhaps select clauses.
To return single id-name data per day you can use this:
select id, name
from tab
group by id, name, date(date)
The DATE() function extracts the date part of a date or date/time expression.
select id,name
from sample
group by id,name,DATE(date)
having count(*)>1;

Get 5 most frequent values/occuring ids?

I'm trying to get the 5 most occurring IDs in my table, my table looks like this:
+-----------+---------------------+---------+---------+
| mashup_id | mashup_time | user_id | deal_id |
+-----------+---------------------+---------+---------+
| 1 | 2011-08-24 21:58:22 | 1 | 23870 |
+-----------+---------------------+---------+---------+
I was thinking of doing a query with a sub-query, something that orders by the count of deal_id? Not exactly sure how to go about it though, if anyone can help, thanks!
In (sort of) generic SQL:
SELECT deal_id, COUNT(*)
FROM your_table
GROUP BY deal_id
ORDER BY COUNT(*) DESC
LIMIT 5
If you meant a different ID field, just substitute it for deal_id.

Distinct Help

Alright so I have a table, in this table are two columns with ID's. I want to make one of the columns distinct, and once it is distinct to select all of those from the second column of a certain ID.
Originally I tried:
select distinct inp_kll_id from kb3_inv_plt where inp_plt_id = 581;
However this does the where clause first, and then returns distinct values.
Alternatively:
select * from (select distinct(inp_kll_id) from kb3_inv_plt) as inp_kll_id where inp_plt_id = 581;
However this cannot find the column inp_plt_id because distinct only returns the column, not the whole table.
Any suggestions?
Edit:
Each kll_id may have one or more plt_id. I would like unique kll_id's for a certain kb3_inv_plt id.
| inp_kll_id | inp_plt_id |
| 1941 | 41383 |
| 1942 | 41276 |
| 1942 | 38005 |
| 1942 | 39052 |
| 1942 | 40611 |
| 1943 | 5868 |
| 1943 | 4914 |
| 1943 | 39511 |
| 1944 | 39511 |
| 1944 | 41276 |
| 1944 | 40593 |
| 1944 | 26555 |
If you do mean, by "make distinct", "pick only inp_kll_ids that happen just once" (not the SQL semantics for Distinct), this should work:
select inp_kll_id
from kb3_inv_plt
group by inp_kll_id
having count(*)=1 and inp_plt_id = 581;
Get all the distinct first (alias 'a' in my following example) and then join it back to the table with the specified criteria (alias 'b' in my following example).
SELECT *
FROM (
SELECT
DISTINCT inp_kll_id
FROM kb3_inv_plt
) a
LEFT JOIN kb3_inv_plt b
ON a.inp_kll_id = b.inp_kll_id
WHERE b.inp_plt_id = 581
in this table are two columns with
ID's. I want to make one of the
columns distinct, and once it is
distinct to select all of those from
the second column of a certain ID.
SELECT distinct tableX.ID2
FROM tableX
WHERE tableX.ID1 = 581
I think your understanding of distinct may be different from how it works. This will indeed apply the where clause first, and then get a distinct list of unique entries of tableX.ID2, which is exactly what you ask for in the first part of your question.
By making a row distinct, you're ensuring no other rows are exactly the same. You aren't making a column distinct. Let's say your table has this data:
ID1 ID2
10 4
10 3
10 7
4 6
When you select distinct ID1,ID2 - you get the same as select * because the rows are already distinct.
Can you add information to clear up what you are trying to do?