Summarizing a table in sql server by date - sql

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.

Related

How to order Order Date by Order Count SQL?

This might be a silly newbie question, but I am trying to figure out how to order something.
How would you construct an SQL statement so that in a table ORDERING with an ORDERDATE attribute I can see how many orders placed on each date?
I know that the following query makes it so it is organized by the date but I want to know how many duplicates for each date in order to find how many orders placed in a day.
SELECT *
FROM ORDERING
ORDER BY ORDERDATE DESC;
Count is an aggregate function, so you have to GROUP BY the attribute you want to aggregate.
SELECT ORDERDATE,COUNT(*)
FROM ORDERING
GROUP BY ORDERDATE
ORDER BY ORDERDATE DESC;
en making questiuons.
SELECT ORDERDATE,COUNT(*)
FROM ORDERING
GROUP BY ORDERDATE
HAVING COUNT(*)>1

Summary Query to Sum Operations not working

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

Combine two SQL queries, one involving GROUP BY?

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.

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.