Group By .... Having SQL Query is not executing properly - sql

I want to see total product quantity sold within a date range i.e.
-------------------------
| PRODUCT | QUANTITY |
-------------------------
| A | 120 |
-------------------------
| B | 75 |
-------------------------
I have the following sql query in MS Access 2003:
SELECT Product, Sum(PurchaseQuantity) AS Quantity
FROM tblInvoice
GROUP BY Product
HAVING PurchaseDate BETWEEN #3/19/2013# and #3/22/2013#;
But this is not executing. MS Access giving me error like:
"You tried to execute a query that does not include the specified
expression 'PurchaseDate BETWEEN #3/19/2013# and #3/22/2013#' as part
of an aggregate function."
So far i understood that i need to add PurchaseDate in Group By clause. But is there any way to do it without putting PurchaseDate in Group By clause?
Thank you in advance.

You should use purchaseDate condition in "WHERE" clause:
SELECT Product, Sum(PurchaseQuantity) AS Quantity
FROM tblInvoice
WHERE PurchaseDate BETWEEN #3/19/2013# and #3/22/2013#
GROUP BY Product

Related

Filter between date in sum by group in sql from two different tables data

How are you, I have two tables one tables is PRODUCT and other tables is STOCK_sales, i use sum and group by,
But my problem is i can't not filter between dates.
in this link is my SQL database CODE and all structure how can i do this.
myquery
SELECT
Product.ProductID,buyprice,sellprice, SUM(sellqty) as Total
FROM product,Stock_sales
WHERE Product.ProductID=Stock_sales.ProductID
GROUP BY Product.ProductID,buyprice,sellprice
ORDER BY Product.ProductID
THANK YOU.
https://dbfiddle.uk/?rdbms=postgres_11&fiddle=f2b78aab10cf770bc918206da6aaeecd
You can use a where clause to filter the dates and other columns.
your FROM clause sould be wriiten by JOINS, your style is outdated for years
SELECT Product.ProductID,buyprice,sellprice,
SUM(sellqty) as Total
FROM product INNER JOIN Stock_sales ON Product.ProductID=Stock_sales.ProductID
WHERE data BETWEEN '2022-01-01' AND '2022-01-04'
GROUP BY Product.ProductID,buyprice,sellprice
ORDER BY Product.ProductID
productid | buyprice | sellprice | total
--------: | -------: | --------: | ----:
1 | 10 | 20 | 20
db<>fiddle here
I agree with the answer by nbk.
However, if you want to continue with the WHERE clause specifying the key IDs instead of using a JOIN then you just need to extend that using AND:
WHERE Product.ProductID=Stock_sales.ProductID
AND stock_sales.data = '2022-01-03'
or
WHERE Product.ProductID=Stock_sales.ProductID
AND stock_sales.data BETWEEN '2022-01-03' AND '2022-01-17'

Access SQL query update calculation for duplicates

I have a query that filters results for products which have had orders sent after an user-input date, and calculates what the quantity becomes if the order was sent after that date.
SELECT *, [OnHand]+[OrderJoin.Quantity] AS Qty After
FROM Query3
WHERE (((Query3.ShippedDate)>[Enter End Date] And (Query3.ShippedDate) Is Not Null));
However, I need a way for it to recognise duplicates and update it based on those.
e.g. I have this
ID | Product Name | Qty Before | Qty Shipped | Qty After
11 | Chocolate | 80 | 20 | 100
11 | Chocolate | 80 | 10 | 90
And I'd need a way for it to show Qty After as 110 (after the 10 and 20 shipped)
If I understand correctly, you want an aggregation query. This would be something like this:
SELECT id, ProductName,
OnHand]+ SUM([OrderJoin.Quantity]) AS Qty After
FROM Query3
WHERE Query3.ShippedDate > [Enter End Date] And
Query3.ShippedDate) Is Not Null
GROUP BY id, ProductName, OnHand;
I note that OrderJoin is not defined, but that is the structure of your original query.

SQL - find rows having n duplicate values

Let's say I have a table like this:
OrderId | CustomerId | ProductName
======================================
73 | 301 | Sponge
74 | 508 | Garbage Bag
75 | 301 | Spoon
76 | 301 | Bacon
77 | 508 | Dog treats
78 | 301 | Paper
79 | 905 | Text book
and I want to find a customer who has made two orders in the past. How would I set up the query?
For the table above, the query would return the two rows for customer 508.
How would I modify it to return customers who have one previous order, so that it would return the row for customer 905?
select customerId, count(*)
from mytable
group by customerId
having count(*) >= 2
If you need only CustomerId of those who have exactly one order in table (they exist once) then the following query groups customers and counts how many times they appear in a table (here showing only those who appear once, modify as you wish).
SELECT
CustomerId
FROM
table
GROUP BY 1
HAVING COUNT(*) = 1
Let's say you want to list every customer and number of orders they've placed but no less than 2 then modify above query to add COUNT(*) in column list to be selected and the HAVING condition like that:
SELECT
CustomerId
COUNT(*) AS no_of_orders
FROM
table
GROUP BY 1
HAVING COUNT(*) > 1
I used a SQL query of something like that just yesterday.
I adapted it to your table, but I'm not 100% sure it will work.
CREATE TEMPORARY TABLE COMMANDS
SELECT OrderId, ProductName, COUNT(CustomerId) AS NbCmds
FROM your_table
GROUP BY OrderId;
SELECT CustomerId
FROM COMMANDS
WHERE NbCmds > 1;
Edit : You can also do it with a HAVING clause. Take a look here

SQL sum field when column values match

The title was hard to word but the question is pretty simple. I searched all over here and could not find something for my specific issue so here it is. I'm usuing Microsoft SQL Server Management Studio 2010.
Table Currently looks like this
| Value | Product Name|
| 300 | Bike |
| 400 | Bike |
| 300 | Car |
| 300 | Car |
I need the table to show me the sum of Values where Product Name matches - like this
| TOTAL | ProductName |
| 700 | Bike |
| 600 | Car |
I've tried a simple
SELECT
SUM(Value) AS 'Total'
,ProductName
FROM TableX
But the above doesn't work. I end up getting the sum of all values in the column. How can I sum based on the product name matching?
Thanks!
SELECT SUM(Value) AS 'Total', [Product Name]
FROM TableX
GROUP BY [Product Name]
SQL Fiddle Example
Anytime you use an aggregate function, (SUM, MIN, MAX ... ) with a column in the SELECT statement, you must use GROUP BY. This is a group function that indicates which column to group the aggregate by. Further, any columns that are not in the aggregate cannot be in your SELECT statement.
For example, the following syntax is invalid because you are specifying columns (col2) which are not in your GROUP BY (even though MySQL allows for this):
SELECT col1, col2, SUM(col3)
FROM table
GROUP BY col1
The solution to your question would be:
SELECT ProductName, SUM(Value) AS 'Total'
FROM TableX
GROUP BY ProductName

Comparing in SQL and SUM

I really couldn't figure out a good title for this question, but I have a problem that I'm sure you can help me with!
I have a query which outputs something like this:
Month | Year | Subcategory | PrivateLabel | Price
-------------------------------------------------
1 | 2010 | 666 | No | -520
1 | 2010 | 666 | No | -499,75
1 | 2010 | 666 | No | -59,95
1 | 2010 | 666 | No | -49,73
1 | 2010 | 666 | No | -32,95
I want to SUM on the price because all the other data is the same. I thought I could do this with SUM and GROUP BY, but I can't figure out how to do it or at least it doesn't output the right result.
The query is an inner join between two tables, if that helps.
select
month
,year
,subcategory
,privatelabel
,sum(price) as [total sales]
from
a inner join b ...
where
any where clauses
group by
month
,year
,subcategory
,privatelabel
should work if i am understanding you correctly.. every colum in the select either needs to be part of the group by or an aggregate function on all rows in the group
added a fiddle.. mainly as i didn't know about he text to DDL functionality and wanted to test it ;-) (thanks Michael Buen)
http://sqlfiddle.com/#!3/35c1c/1
note the where clause is a place holder..
select month, year, subcategory, privatelabel, sum(price)
from (put your query in here) dummyName
group by month, year, subcategory, privatelabel
Basic idea is it will run your current query to get above output then do the sum and group by on the result.
You query has to be in parentheses and you have to give it some name e.g. dummyName. As long as it's unique in the sql and preferably not a key word, doesn't matter what it is.
There might be a way of doing all this in one go, but without the sql for your query we can't help.