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
Related
I want to use the following sql code to get Year information from MyDate and count unique CustomerId but get error.
SELECT YEAR(MyDate) AS MyYear, COUNT(DISTINCT CustomerId) AS MyCustomerCount
FROM MyTable
GROUP BY MyYear
However the following code works.
SELECT YEAR(MyDate), COUNT(DISTINCT CustomerId)
FROM MyTable
GROUP BY YEAR(MyDate)
Why can't alias be used in this scenario?
Simply because this is how it was defined in SQL standard.
Syntax rules
Group by clause is defined as:
However Order by clause is defined as bellow
So here you can see alias exists after order by clause then we can use it normally but not after group by clause.
DATABASE PICTURE
SQL Command:
SELECT CUST_ID,SUM(AMOUNT_SOLD),CUST_CREDIT_LIMIT FROM(
SELECT DISTINCT
CUST_ID,AMOUNT_SOLD,CUST_CREDIT_LIMIT,
TO_CHAR(TIME_ID,'YYYY')AS YEAR FROM SH.SALES
JOIN SH.CUSTOMERS USING (CUST_ID))
WHERE YEAR = '2001'
GROUP BY CUST_ID;
"not a GROUP BY expression"
Without CUST_CREDIT_LIMIT(after SUM(AMOUNT_SOLD) it works, but that column is mandatory for me and I need to keep it tracked.
The results should be for every Customer(CUST_ID) there should be all summerized sales(SUM(AMOUNT_SOLD) in year 2001 AND his credit limit(CUST_CREDIT_LIMIT)... It works without credit limit, but I need to work it with it.
Anyone can help ?
The error is exactly what it says.. You added CUST_CREDIT_LIMIT to your select list, but not your GROUP BY clause.
SELECT CUST_ID,SUM(AMOUNT_SOLD),CUST_CREDIT_LIMIT FROM(
SELECT DISTINCT
CUST_ID,AMOUNT_SOLD,CUST_CREDIT_LIMIT,
TO_CHAR(TIME_ID,'YYYY')AS YEAR FROM SH.SALES
JOIN SH.CUSTOMERS USING (CUST_ID))
WHERE YEAR = '2001'
GROUP BY CUST_ID,CUST_CREDIT_LIMIT;
I would say there are other issues with this too (why the DISTINCT?, etc) but that should get you by the GROUP BY error.
you must have ,CUST_CREDIT in your group by,
you can re select all with
select cust_id, sumid from (select SUM(AMOUNT_SOLD) sumid, your code
group by "all fields execpt calculate fields (grouped) sum, max etc )
SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
group by [date]
is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
You cannot select fields in the SELECT part which is not a part of an Aggregated function or specified in the the GROUP BY clause when using GROUP BY.
You can select "mount" because it's aggregated (SUM), but not price or total_price because they're not aggregated nor part of your group by date
You will need to either change your group by functionality, or the select part.
You have to group by the remaining tables that are not a function, therefore:
SELECT price,total_price,SUM(mount) As TotalAmount
FROM sales
Group by price,total_price
Order by date
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.
When I try to use the following SELECT statement:
SELECT [lots of columns]
FROM Client, Customer, Document, Group
WHERE [some conditions]
GROUP BY Group.id
SQL Server complains that the columns I selected are not part of the GROUP BY statement nor an aggregate function. Am I using GROUP BY wrong? What should I be using instead?
To return all single occurences of a group by field, together with associated field values, write a query like:
select group_field,
max(other_field1),
max(other_field2),
...
from mytable1
join mytable2 on ...
group by group_field
having count(*) = 1;
Yes, you are using GROUP BY incorrectly. The point of using GROUP BY is to use aggregate functions. If you have no aggregrate functions you probably want SELECT DISTINCT instead.
SELECT DISTINCT
col1,
col2,
-- etc
coln
FROM Client
JOIN Customer ON ...
JOIN Document ON ...
JOIN [Group] ON ...
WHERE ...
My first guess would be that the problem is that you have table called Group, which I believe is a reserved word in SQL. Try wrapping the Group name with ' '
You want to group by all columns you are selecting that is not in an aggregate funcion.
SELECT ProductName, ProductCategory, SUM(ProductAmount)
FROM Products
GROUP BY ProductName, ProductCategory
This will give you a disticnt result of Product names and categories with the sum total of product amount in all aggregate child records for that group.