I want to count the content of one column, which is no problem.
But also i want to count another column content but with a where condition.
I visualize it for better explaining:
My table
Index1 Index2
1 0
1 2
2 2
2 2
2 2
now i want to count the content of Index1 that i know how often the numbers 1 and 2 appears.
But further i want to count the content of Index2, but only the numbers which are higher than 1.
Result
Index1 amount
1 1
2 3
but when i use the where condition it is used for both columns.
I tried it with distinct but it doesn´t work.
Are there some comfortable solutions for this problem?
That's called conditional aggregation, and in MySQL it's pretty simple :
SELECT t.index1,
COUNT(*) as amount
SUM(t.index2>1) as amountWithWhere
FROM YourTable t
GROUP BY t.index1
Related
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.
i have table lets say - Students,
with 5 records and id(s) are 1 to 5, now i want to select the records - in a way that result should come like given sorting order of id column
id column should be resulted - 5,2,1,3,4
is there any other way to do this - then separate db calls for ids?
single db call ?
I guess if you really want a hard-coded order, you could do something like this:
order by case id
when 5 then 0
when 2 then 1
when 1 then 2
when 3 then 3
when 4 then 4
else 999
end
Or more simply (as #Strawberry points out in the comments):
order BY FIELD(id,4,3,1,2,5) desc
I'd like to retrieve some rows utilizing my index on Columns A and B. I was told the only way to ensure my index is being used to retrieve the rows is to use an ORDER by clause, for example:
A B offset
1 5 1
1 4 2
2 5 3
2 4 4
SELECT A,B FROM TableX
WHERE offset > 0 AND offset < 5
ORDER BY A,B ASC
but then I would like my results for just those rows returned to be ordered by column B and not A,B.
A B
1 4
2 4
2 5
1 5
How can I do this and still ensure my index is being used and not a full table scan? If I was to use ORDER BY B then doesn't this mean MySQL will scan by B and defeat the purpose of having the two column index?
Any index that includes A or B cloumns will have no effect on your query, regardless of your ORDER BY. You need an index on offset as that is the field that is being used in hte WHERE clause.
Sorry, but maybe I did not understand the question..
The above output query should result:
A B
1 4
1 5
2 4
2 5
For avoiding table scan, you should add an index for the offset and use it in your WHERE clause.
If possible to use unique then use it.
CREATE UNIQUE INDEX offsetidx ON TableX (offset);
or
CREATE INDEX offsetidx ON TableX (offset);
Considering your query, the best index is probably (offset,A,B). This will allow the optimizer to use the leftmost part of the index to honor the WHERE clause, and the rest of the index allowing to use merge sort.
ALTER TABLE TableX ADD INDEX (offset,A,B);
In order to take full advantage of that, the query has to be rewritten as:
SELECT A,B FROM TableX
WHERE offset BETWEEN 0 AND 5
ORDER BY A,B;
See http://sqlfiddle.com/#!2/c8e718/2
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)
I want to have a query that returns the best results from a table.
I am defining the best results to be the addition of two columns a + b (each column holds an int)
ie:
entry a b
1 4 5
2 3 2
3 20 30
Entry 3 would be returned because a + b is the highest in this case.
Is there a way to do this? One idea I had was to create another column in the table which holds the addition of a and b and then ORDER by DESC, but that seems a little bit messy.
Any ideas?
Thanks!
SELECT *
FROM mytable
ORDER BY
a + b DESC
LIMIT 1
Adding another column, however, would be a good option, since you could index this column which would improve the query.