How this query can be answered ? Select SUM(1) FROM table - mysql

select * from "Test"."EMP"
id
1
2
3
4
5
Select SUM(1) FROM "Test"."EMP";
Select SUM(2) FROM "Test"."EMP";
Select SUM(3) FROM "Test"."EMP";
why the output of these queries is?
5
10
15
And
I don't understand why they write table name like this "Test"."EMP"

your table has 5 records. the statement select 1 from test.emp returns 5 records with values as 1 for all 5 records.
id
1
1
1
1
1
This is because db engine simply returns 1 for each existing record without reading the contents of the cell. and same happens for select <any static value> from test.emp
same happens for 2 and 3
id
2
2
2
2
2
hence there are 5 records returned with the static values and sum of those values will be the product of static number passed in the select statement and total records in the table
additional fact: It is always recommended to perform count(1) than count(*) as it consumes less resource and hence less load on the server

I don't think it's "Test"."EMP" with double quotes.. it's probably `Test`.`EMP` with backticks instead. The definition means its database_name.table_name. This is the recommended format to get the correct table_name from database_name; in this case, you're specifically making the syntax to query from `Test`.`EMP`. Read more about identifier qualifiers.
As for SUM(x), the x get's repeated according to the rows present in the table. So SUM(1) on 5 rows is 1+1+1+1+1, SUM(2) on 5 rows is 2+2+2+2+2, and so on.

Related

MySQL limit clause unusal issue

SELECT id,name,info FROM table LIMIT 5
the result Set should be contains 5 rows wich is The first 5 rows of the table,but is any exception about this usage? .the table like this :
SELECT * FROM table limit 10;
1. company_id company_name tel
1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
7 一呼医知己健康咨询(北京)有限公司 010-62957992
8 一呼(北京)电子商务有限公司 62957992
9 一汽轿车股份有限公司 0431-85782608
10 一通万通商务服务(北京)有限公司 010-68061805
I use the first sql the result is normal:like this
SELECT company_id,company_name,tel FROM table LIMIT 5;
1. 1 TCL集团股份有限公司 0752-2288333
2 UNITEDSTACK(北京)科技有限公司 15727325616
3 《市政技术》杂志社有限公司 13401070358
4 《网络安全技术与应用》杂志社有限公司 010-62765013
5 《艺术市场》杂志社股份有限公司 64271947
However I use the second sql like this :
SELECT comapny_id,company_name FROM table LIMIT 5;
1275992
1758051
2990914
5241776
5344925
We are seeing the result is not the 5 rows of the table obviously,the difference of these fileds is that company_id is a primary key,company_name is a type of MUL.can you help me?thank you very much!
the result Set should be contains 5 rows wich is The first 5 rows of
the table,but is any exception about this usage?
Only the first part of this statement is correct. Your query returns 5 rows from the query. However, those are 5 indeterminate rows.
SQL tables represent unordered sets. Hence, there is no first five rows in a table. If you want your result set ordered, then you need to include an order by clause. Often, an auto-incremented id is used for this purpose, because such an id captures the order that rows are inserted into the table.

SQL count not return all rows, phpmyadmin add LIMIT but not show it in the query

I have a query:
select lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id,
that returns 88 rows. Then I tried the following:
select count(lr2.event_id) as cc, lr2.event_id as ce
from data_requests as lr2
group by lr2.event_id
but it only returned 25 rows, so I am really puzzled, where did other 63 rows go.
I tried it in sqlfiddle, it seems to work correctly, but on my server it just doesn't, so it must be a setting or something... Feels like the server calculates the count after it select a subset of all group results. weird.
if you want to count the number of rows for each lr2.event_idyou must use count(*) , not count(lr2.event_id) . Remember, you are counting rows.
Function of GROUP BY
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical/similar/equal data into groups.
Demonstration
If I have table like below, then 1st query will give same output as table definition:
ce
--
1
2
3
4
5
And 2nd query will give output as,
cc |ce
--- ---
1 1
1 2
1 3
1 4
1 5
Since, all are distinct in Table, I got 5 rows! but If some ce values are repetitive as,
ce
--
1
2
1
2
2
then, 2nd query will give output as:
cc |ce
--- ---
2 1
3 2
And here If I get shocked where did other 3 rows go? Then I need to study!
Of course, it's a spoon feeding! OP needs to study about GROUP BY in SQL.
My bad, it seems to be a phpmyadmin problem, I run the query in the phpmyadmin, and it auto added a limit in the end of every query

Limit On Accumulated Column in MySQL

I'm trying to find an elegant way to write a query that only returns enough rows for a certain column to add up to at least n.
For example, let's say n is 50, and the table rows look like this:
id count
1 12
2 13
3 5
4 18
5 14
6 21
7 13
Then the query should return:
id count
1 12
2 13
3 5
4 18
5 14
Because the counts column adds up to n > 50. (62, to be exact)
It must return the results consecutively starting with the smallest id.
I've looked a bit into accumulators, like in this one: MySQL select "accumulated" column
But AFAIK, there is no way to have the LIMIT clause in an SQL query limit on an SUM instead of a row count.
I wish I could say something like this, but alas, this is not valid SQL:
SELECT *
FROM elements
LIMIT sum(count) > 50
Also, please keep in my the goal here is to insert the result of this query into another table atomically in an automated, performance efficient fashion, so please no suggestions to use a spreadsheet or anything that's not SQL compatible.
Thanks
There are many ways to do this. One is by using Correlated Subquery
SELECT id,
count
FROM (SELECT *,
(SELECT Isnull(Sum(count), 0)
FROM yourtable b
WHERE b.id < a.id) AS Run_tot
FROM yourtable a) ou
WHERE Run_tot < 50

Retrieve unique data from MYSQL database

I have a table in my database which contains 5 rows. I am trying to write an sql statement that will retrieve all rows which only have 1 agency assigned to them.
case_id agency_ID
1 4
2 4
3 3
4 2
4 4
To clarify I would like to select the required rows (and any further rows) but only if the case_id is unique. Any rows with duplicates would be ommited.
I have tried to use DISTINCT(case_id), COUNT(*) to count all rows but it doesn't work and it's slowly sapping away my soul. It is probably an easy fix, but for the life of me I just can't see it.
Hope this is enough information to go on. Any help would be greatly appreciated.
SELECT * FROM your_table GROUP BY case_id HAVING COUNT(agency_ID) = 1
You can try
SELECT case_id,agency_ID,COUNT(case_id) as c
FROM yourTable
GROUP BY case_id
HAVING (c=1)

Obtain running frequency distribution from previous N rows of MySQL database

I have a MySQL database where one column contains status codes. The column is of type int and the values will only ever be 100,200,300,400. It looks like below; other columns removed for clarity.
id | status
----------------
1 300
2 100
3 100
4 200
5 300
6 300
7 100
8 400
9 200
10 300
11 100
12 400
13 400
14 400
15 300
16 300
The id field is auto-generated and will always be sequential. I want to have a third column displaying a comma-separated string of the frequency distribution of the status codes of the previous 10 rows. It should look like this.
id | status | freq
-----------------------------------
1 300
2 100
3 100
4 200
5 200
6 300
7 100
8 400
9 300
10 300
11 100 300,100,200,400 -- from rows 1-10
12 400 100,300,200,400 -- from rows 2-11
13 400 100,300,200,400 -- from rows 3-12
14 400 300,400,100,200 -- from rows 4-13
15 300 400,300,100,200 -- from rows 5-14
16 300 300,400,100 -- from rows 6-15
I want the most frequent code listed first. And where two status codes have the same frequency it doesn't matter to me which is listed first but I did list the smaller code before the larger in the example. Lastly, where a code doesn't appear at all in the previous ten rows, it shouldn't be listed in the freq column either.
And to be very clear the row number that the frequency string appears on does NOT take into account the status code of that row; it's only the previous rows.
So what have I done? I'm pretty green with SQL. I'm a programmer and I find this SQL language a tad odd to get used to. I managed the following self-join select statement.
select *, avg(b.status) freq
from sample a
join sample b
on (b.id < a.id) and (b.id > a.id - 11)
where a.id > 10
group by a.id;
Using the aggregate function avg, I can at least demonstrate the concept. The derived table b provides the correct rows to the avg function but I just can't figure out the multi-step process of counting and grouping rows from b to get a frequency distribution and then collapse the frequency rows into a single string value.
Also I've tried using standard stored functions and procedures in place of the built-in aggregate functions, but it seems the b derived table is out of scope or something. I can't seem to access it. And from what I understand writing a custom aggregate function is not possible for me as it seems to require developing in C, something I'm not trained for.
Here's sql to load up the sample.
create table sample (
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
status int
);
insert into sample(status) values(300),(100),(100),(200),(200),(300)
,(100),(400),(300),(300),(100),(400),(400),(400),(300),(300),(300)
,(100),(400),(100),(100),(200),(500),(300),(100),(400),(200),(100)
,(500),(300);
The sample has 30 rows of data to work with. I know it's a long question, but I just wanted to be as detailed as I could be. I've worked on this for a few days now and would really like to get it done.
Thanks for your help.
The only way I know of to do what you're asking is to use a BEFORE INSERT trigger. It has to be BEFORE INSERT because you want to update a value in the row being inserted, which can only be done in a BEFORE trigger. Unfortunately, that also means it won't have been assigned an ID yet, so hopefully it's safe to assume that at the time a new record is inserted, the last 10 records in the table are the ones you're interested in. Your trigger will need to get the values of the last 10 ID's and use the GROUP_CONCAT function to join them into a single string, ordered by the COUNT. I've been using SQL Server mostly and I don't have access to a MySQL server at the moment to test this, but hopefully my syntax will be close enough to at least get you moving in the right direction:
create trigger sample_trigger BEFORE INSERT ON sample
FOR EACH ROW
BEGIN
DECLARE _freq varchar(50);
SELECT GROUP_CONCAT(tbl.status ORDER BY tbl.Occurrences) INTO _freq
FROM (SELECT status, COUNT(*) AS Occurrences, 1 AS grp FROM sample ORDER BY id DESC LIMIT 10) AS tbl
GROUP BY tbl.grp
SET new.freq = _freq;
END
SELECT id, GROUP_CONCAT(status ORDER BY freq desc) FROM
(SELECT a.id as id, b.status, COUNT(*) as freq
FROM
sample a
JOIN
sample b ON (b.id < a.id) AND (b.id > a.id - 11)
WHERE
a.id > 10
GROUP BY a.id, b.status) AS sub
GROUP BY id;
SQL Fiddle