Combine two SQL queries, one involving GROUP BY? - sql

I would like to combine these two SQL queries into one.
SELECT COUNT() as total_grants, SUM("CURRENT_AWARD") as total_spent FROM t;
SELECT YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY YEAR AS by_year;
The first query shows the total number of grants, and the total spent. The second is the same, but by year.
Is this possible? I've already combined two queries into one in the first query, but I can't figure out how to use an AS clause properly in the second query.
Thanks for any help.

How about using CROSS JOIN
SELECT YEAR,
COUNT(*),
SUM('CURRENT_AWARD') ,
t2.total_grants,
t2.total_spent
FROM t
CROSS JOIN
(
SELECT COUNT(*) as total_grants,
SUM("CURRENT_AWARD") as total_spent
FROM t
) t2
GROUP BY YEAR;

Maybe something like this?
SELECT BY_YEAR, COUNT(), SUM('CURRENT_AWARD') FROM t GROUP BY rollup(by_year);
(I think rollup can be rdbms/version dependent...)

Try this.
SELECT YEAR, COUNT(*) as total_grants, SUM(CURRENT_AWARD) as total_spent
FROM t
GROUP BY YEAR;
COUNT(*) will count all rows in table t, including ones with NULL. If you want to ignore rows with NULL, count a specific column. For example, COUNT(CURRENT_AWARD).
You don't need the quotes around CURRENT_AWARD since this identifier contains only letters and underscores.

Related

SQL: Finding the Minimum aggregate using only count

I'm using DB2 for a project and looking to find which Group has the fewest members without using the min feature. My idea is to find all the groups and then subtract out any group which has more members from some other group thus leaving me with the group with that has no more members than any other group, i.e. the min.
So far I have
SELECT DISTINCT P.group as Group, count(P.id) as Count
FROM People P
EXCEPT
SELECT P.group, count(P.id)
FROM People P, People O
WHERE count(P.cid) > count(O.cid);
With a schema for People like
create table People (
group varchar(25) not null,
id smallint not null,
);
I am getting the following error:
SQL0119N An expression starting with "CLUB" specified in a SELECT clause,
HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or
it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column
function and no GROUP BY clause is specified. SQLSTATE=42803
If you could help point out what I am doing wrong or the correct format for such a query it would be greatly appreciated!
find which Group has the fewest members
You can aggregate by group, order by member count, and fetch the top row only:
select p.group as grp, count(*) as cnt
from people p
group by p.group
order by count(*)
fetch first 1 rows only
You should try to use min(). Very straight forward. From your Error message, it seems like your HAVING clause is wrong so it would look into that.

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

Summarizing a table in sql server by date

I have a table with 20 columns in which one column is a transaction date and one is a sales amount column. I want to pull all the columns and group it by transaction date. But I get a error if I don't mention all the columns in group by. Any suggestions.
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. You need to provide some aggregate function to use it with group by. E.g:
SELECT transaction_date, COUNT(sales_amount) as AmountCount
FROM table_name
GROUP BY transaction_date
you can try this:
SELECT *
FROM table_name
order by transaction_date ASC
note:
This will selects your 20 column from table ordered by Transaction date and change ASC or DESC as per your need.
When using Aggregate functions in SQL you need to use the group by statement by non-aggregate functions.
Example:
Select a.transactiondate,
a.vehicles,
a.market,
a.city,
sum(a.quantity) as Quantity
from table_name a
group by a.transactiondate,
a.vehicles,
a.market,
a.city
Sum being your aggregate function so you will not include in your group by. Even if you want to just group by date, you will need any other attributes that need to belong with it.
Its going to group by at the most granular data point. Since you are only summing or counting one column you will need all other attributes with it in the group by statement.

SQL Query Group by Trouble

I am trying to display the number of quotes made during a certain period, sum the forecast for each and group them by the person who created the quote. Below is my query...but I think I am doing something wrong with the group by, but I don't know what. I am using standard SQL 2008. Any help would be greatly appreciated.
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM
Quotes
WHERE
quotes.created>=? AND quotes.created<=?
Group by 1
Your error is due to the fact that you are grouping by an aggregate. You can group by column numbers, but your first column includes count().
You can either change the 1 to a 3, or use the column name.
You need to use createdby in your group by clause
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby FROM Quotes
WHERE
quotes.created>=? AND quotes.created<=?
Group by createdBy
In the GROUP BY clause you must include the columns that you have in SELECT clause.
If you want to aggregate the forecast by who has created it, you should do like #Sachin and #Dan Bracuk recommend:
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM quotes
WHERE
quotes.created>=? AND quotes.created<=?
GROUP BY quotes.createdby
or
SELECT
CAST(COUNT (DISTINCT QUOTES.Quote) AS SQL_CHAR) AS Number_of_Quotes,
SUM(Forecast) AS Amount_Forecast,
quotes.createdby
FROM quotes
WHERE
quotes.created>=? AND quotes.created<=?
GROUP BY 3 --equivalent because createdBy is the 3ยบ column on the select clause

SQL count distinct

How can I do a count distinct selection? Basically I have all these charges and I want to count only the number of distinct employees involved with that charge. (I already have other fields for the group by)
SELECT COUNT(DISTINCT employeeid) FROM ....
Simple as: select count(distinct(field)) from table
Live sample: http://sqlfiddle.com/#!3/81379/2/3
You can put a DISTINCT in a COUNT (in TSQL):
SELECT COUNT(DISTINCT(employee)) ...