Summary Query to Sum Operations not working - sql

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

Related

SQL group by within a group

How can we use group by within separate categories? That is treat each category as it's own group and then group by something else within the category.
For instance in this Image, I would like to group by quarter only within a particular ISSN.
Expected result would be
It is possible to group by more than one column:
select issn, quarter, count(*)
from t
group by issn, quarter
Simply add columns -as prioritized- to group by, example:
SELECT * FROM MyTable GROUP BY ISSN, Quarter

How to aggregate in SQL by having clause

The below is the query I'm using, I would like to add two more columns in the select statement which I have and noted them in the group by clause but the total results are different. The below script gives me the correct total count but I want to see the totals also for a column called transaction, and a column called employee. The total count should still be the same.
SELECT SUB.YEAR, SUM(SUB.TOTAL_COUNT), SUM(SUB.TOTAL_SPENT)
FROM (
SELECT YEAR, COUNT(*) AS TOTAL_COUNT, SUM($SPENTX) AS TOTAL_SPENT, CUSTOMERID
FROM TABLE A
WHERE YEAR = 2017
GROUP BY YEAR, CUSTOMERID
HAVING SUM($SPENTX)<=1000
) SUB
GROUP BY SUB.YEAR

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.

Group by one column and select more than one column Sql query

I need to group by more than one columns but in special case:
I have a table of (Payment_Type,Year,TotalMoney)
I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:
select Payment_Type,Year,SUM(TotalMoney)
from table
where 1=1
group by Payment_Type,Year
I get an error message as:
Year is not contained in either an aggregate function or the GROUP BY clause
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney)
from table
group by Payment_Type,Year(YourDateColumn)
if your column is named year then
select Payment_Type,[Year],SUM(TotalMoney)
from table
group by Payment_Type,[Year]

How can I use the GROUP BY SQL clause with no aggregate function?

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.