mysql sum question - mysql

Is it possible to get the sum of a column and all the column itself using one query?
I mean, I need SUM(Result.num) and the individual Result.num as well, but I dont want to use two separate queries for the purpose? the idea result might be the SUM as another column in the result.
Result might look like:
col1
Result.num1, SUM
Result.num2, SUM
Result.num3, SUM
....
SELECT SUM(Result.num)
FROM (SELECT COUNT(colA) AS num
FROM Table1
GROUP BY colB) AS Result;
SELECT Result.num
FROM (SELECT COUNT(colA) AS num
FROM Table1
GROUP BY colB) AS Result;

The answer is that you will do two statements but you can return the results in one result set. You can do that with a UNION ALL statement. Just take your two queries and put a UNION ALL statement between them. It will look like this:
SELECT Result.num
FROM (SELECT COUNT(colA) AS num
FROM Table1
GROUP BY colB) AS Result;
UNION ALL
SELECT SUM(Result.num)
FROM (SELECT COUNT(colA) AS num
FROM Table1
GROUP BY colB) AS Result;
I switched the order around so that your SUM value would be at the end but you could put it at the beginning if you would like.

Pretty sure you just need to union your queries
SELECT SUM(Result.num) FROM (SELECT COUNT(colA) AS num FROM Table1 group by colB) AS Result
union all
SELECT Result.num FROM (SELECT COUNT(colA) AS num FROM Table1 group by colB) AS Result;

The WITH ROLLUP clause might be useful to you here.
http://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

Related

How to count the total number of results before a hyphen?

I am looking to count the number of results from a SQL count query, the results currently have like 50 results, but in reality there are only 5 results... the results appear in a format such as:
test1-helpme1
test1-helpme3
test1-helpme4
test2-helpme1
test2-helpme2
test3-helpme4
Is there a way I can count just the "testx-" part of the results?
There can be hundreds of results so the number part of "test" can't be hardcoded
SELECT COUNT(*) as CountbyID, OriginalId FROM Table1 GROUP BY OriginalId;
If you want the number of the distinct occurrences of the pattern, then:
SELECT
COUNT(DISTINCT LEFT(OriginalId, INSTR(OriginalId, '-') - 1)) as counter
FROM Table1
or a counter for each one:
SELECT
LEFT(OriginalId, INSTR(OriginalId, '-') - 1) pattern,
COUNT(*) as counter
FROM Table1
GROUP BY LEFT(OriginalId, INSTR(OriginalId, '-') - 1)
Yup you can use LEFT to group by a subset of the string:
declare #test as table (test varchar(100))
insert into #test
Select 'test1-helpme1' UNION ALL
Select 'test1-helpme3' UNION ALL
Select 'test1-helpme4' UNION ALL
Select 'test2-helpme1' UNION ALL
Select 'test2-helpme4'
Select count(*) as countbyID, left(test,5) as originalid from #test group by left(test,5)

Return Year-Value that occurs more than once in two columns

I would like to select all number of years that occur more than once in between two columns.
Here is what I have so far:
SELECT YEAR(`Date1`), COUNT(*) as Counter
from (SELECT YEAR(`Date1`)
from table1 UNION
SELECT YEAR(`date2`)
from table1
) as year
GROUP by YEAR(`date1`)
WHERE Counter > 2;
I appreciate any advice!
Thanks.
When you are using GROUP BY , you need to use HAVING not WHERE like following.
SELECT Y ,
COUNT(*) AS Counter
FROM (
SELECT DISTINCT YEAR(`Date1`) Y
FROM table1
UNION ALL
SELECT DISTINCT YEAR(`date2`) Y
FROM table1) AS YEAR
GROUP BY Y
HAVING COUNT(*) > 2;
Note: You don't need to put YEAR again in your outer query, also you can put DISTINCT, for each column so that you don't get duplicates for the column.
I think your problem is the UNION. It needs to be UNION ALL, or you will never find duplicates.
Then, you can simply do:
SELECT yyyy, COUNT(*) as Counter
FROM (SELECT YEAR(`Date1`) as yyyy
FROM table1
UNION ALL
SELECT YEAR(`date2`) as yyyy
FROM table1
) y
GROUP BY yyyy
HAVING Counter >= 2;
Note the changes to the query:
UNION ALL instead of UNION so the subquery does not eliminate duplicates.
Giving a column alias to the year, in this case, yyyy.
Using the column alias in the outer query.
Using HAVING instead of WHERE.

Sum two fields from different tables when the results are a sum and multiplication in the same query

I have this two queries and I want to sum their results in one query, but union all is not working.
I use this query:
select sum(a.value1) from myTable a
and I get: 10, then I use this query
select b.value2*b.value3 from myTable b
and I get:10,I want to sum them and get 20, so I use union:
select sum(a.value1) from myTable a
union all
select b.value2*b.value3 from myTable b
But the query returns
10
10
How I can sum them to get 20?
You could do this:
select ((select sum(a.value1) from myTable a) +
(select b.value2*b.value3 from myTable b)
) as sum
Hope this helps!!
select (sum(a.value1)+(select sum(b.value2*b.value3) from myTable b )) as test from myTable a
I finnaly could using this query:
select a.value4 + b.value5
from (select sum(a.value1) value4 from myTable) a,
(select b.value2 * b.value3 value5 from myTable) b

SQL - ORDER BY ASC LIMIT 1 excludes multiple same results - best method?

I'm still learning SQL so I was wondering if there is a better way of doing the following.
I need to get row data for the lowest and highest values in a column (lets call it columnA).
I would use:
SELECT *
FROM table
ORDER BY columnA
DESC LIMIT 1
Problem is I get only one result due to the LIMIT 1 but there may be identical lowest / highest values in ColumnA that have different values in the other columns. I need those other rows too.
There is SELECT(MAX) but I believe that will also only produce one row of data.
The ways I can think do this are by putting the highest / lowest columnA values into a variable and then back into a second query OR use a LEFT JOIN on alias tables to do this in single query but is there any more direct method?
The simplest way is to perform a sub-query:
SELECT * FROM MyTable WHERE columnA = (SELECT MAX(columnA) FROM MyTable);
You can even query both extremes at once:
SELECT * FROM MyTable
WHERE columnA = (SELECT MAX(columnA) FROM MyTable);
OR columnA = (SELECT MIN(columnA) FROM MyTable);
I haven't tested the next one (don't know if MySQL supports UNION in
sub-queries), but it should work as well, might be a bit more
efficient (depending on your data size).
SELECT * FROM MyTable
WHERE columnA IN (
SELECT MAX(columnA) FROM MyTable
UNION
SELECT MIN(columnA) FROM MyTable
);
Another option is :
SELECT *
FROM MyTable m1
WHERE not exists (select 1 from MyTable where columnA > m1.columnA)

Get result row count for individual SELECT statements with UNION ALL in MySQL when using LIMIT

I need to get the row count for individual SELECT statements in an UNION ALL SELECT query that uses LIMIT. The MySQL docs are pretty clear about getting the global row count for the query (place individual SELECTs in parenthesis and place an SQL_CALC_FOUND_ROWS only in the first statement, then get FOUND_ROWS() the usual way). However, I also need the result row count for the individual SELECT statements. The query, simplified:
(SELECT SQL_CALC_FOUND_ROWS `field1`,`field2` FROM `db`.`table1` WHERE `id`>1000)
UNION ALL
(SELECT `field1`,`field2` FROM `db`.`table2` WHERE `id`>1000)
UNION ALL
...
(SELECT `field1`,`field2` FROM `db`.`tableN` WHERE `id`>1000)
LIMIT 0,10
If SQL_CALC_FOUND_ROWS is placed in every SELECT statement an "Incorrect usage/placement of 'SQL_CALC_FOUND_ROWS'" error is issued.
Google this a lot, read related messages here, to no avail. Might be something really simple, I just can't get my mind around it.
You're getting that error because SQL_CALC_FOUND_ROWS can be used on a query to return a single number into FOUND_ROWS().
You can get back counts doing this:
SELECT 'table1' AS tablename, COUNT(1) AS rowcount FROM table1 WHERE id > 1
UNION ALL
SELECT 'table2' AS tablename, COUNT(1) AS rowcount FROM table2 WHERE id > 1
...
SELECT 'tableN' AS tablename, COUNT(1) AS rowcount FROM tableN WHERE id > 1
if that helps.
If you want to return the rows from the tables with a count then modify it:
SELECT field1, field2, (SELECT COUNT(1) FROM table1 WHERE id > 1000) FROM table1 WHERE id > 1000
UNION ALL
SELECT field1, field2, (SELECT COUNT(1) FROM table2 WHERE id > 1000) FROM table2 WHERE id > 1000
...
SELECT field1, field2, (SELECT COUNT(1) FROM tableN WHERE id > 1000) FROM tableN WHERE id > 1000
From your question, it's not precisely clearly what you're trying to achieve.