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.
Related
My database is Db2 for IBM i.
I have read-only access, so my query must use only basic SQL select commands.
==============================================================
Goal:
I want to select every record in the table until the sum of the amount column exceeds the predetermined limit.
Example:
I want to match every item down the table until the sum of matched values in the "price" column >= $9.00.
The desired result:
Is this possible?
You may use sum analytic function to calculate running total of price and then filter by its value:
with a as (
select
t.*,
sum(price) over(order by salesid asc) as price_rsum
from t
)
select *
from a
where price_rsum <= 9
SALESID | PRICE | PRICE_RSUM
------: | ----: | ---------:
1001 | 5 | 5
1002 | 3 | 8
1003 | 1 | 9
db<>fiddle here
I have a table of transactions. One column is for the vendor ID, and one column is for the amount due. [There are other columns, but they aren't relevant]
id | amount | custid
23 | -31.32 | 904424
24 | -19.94 | 646744
25 | -4.77 | 904424
26 | -29.40 | 972979
I want to run a query that delivers the total for each distinct customer ID.
The goal is to determine how much each customer is owed.
That's basic aggregation:
select custid, sum(amount) total_amount
from mytable
group by custid
I am trying to query a sql database table for the minimum price for products. I also want to grab an additional column with the value of the row with the minimum price. My data looks something like this.
ProductId | Price | Location
1 | 50 | florida
1 | 55 | texas
1 | 53 | california
2 | 65 | florida
2 | 64 | texas
2 | 60 | new york
I can query the minimum price for a product with this query
select ProductId, Min(Price)
from Table
group by ProductId
What I want to do is also include the Location where the Min price is being queried from in the above query. Is there a standard way to achieve this?
One method uses a correlated subquery:
select t.*
from t
where t.price = (select min(t2.price) from t t2 where t2.productid = t.productid);
In most databases, this has very good performance with an index on (productid, price).
Lets says I have the following database table (date truncated for example only, two 'id_' preix columns join with other tables)...
+-----------+---------+------+--------------------+-------+
| id_table1 | id_tab2 | date | description | price |
+-----------+---------+------+--------------------+-------+
| 1 | 11 | 2014 | man-eating-waffles | 1.46 |
+-----------+---------+------+--------------------+-------+
| 2 | 22 | 2014 | Flying Shoes | 8.99 |
+-----------+---------+------+--------------------+-------+
| 3 | 44 | 2015 | Flying Shoes | 12.99 |
+-----------+---------+------+--------------------+-------+
...and I have a query like the following...
SELECT id, date, description FROM inventory ORDER BY date ASC;
How do I SELECT all the descriptions, but only once each while simultaneously only the latest year for that description? So I need the database query to return the first and last row from the sample data above; the second it not returned because the last row has a later date.
Postgres has something called distinct on. This is usually more efficient than using window functions. So, an alternative method would be:
SELECT distinct on (description) id, date, description
FROM inventory
ORDER BY description, date desc;
The row_number window function should do the trick:
SELECT id, date, description
FROM (SELECT id, date, description,
ROW_NUMBER() OVER (PARTITION BY description
ORDER BY date DESC) AS rn
FROM inventory) t
WHERE rn = 1
ORDER BY date ASC;
I have a table that lists items in my inventory, the total quantity on hand, order qty and ship date.
+-------+-----------+------------+----------+
| Items | QtyOnHand | QtyOrdered | ShipDate |
+-------+-----------+------------+----------+
| Itema | 100 | 80 | 3/4/14 |
| Itemb | 80 | 220 | 3/8/14 |
| Itema | 100 | 80 | 3/10/14 |
| Itemb | 80 | 100 | 3/12/14 |
+-------+-----------+------------+----------+
I would like a return like this that includes the item, date we are out of inventory and the amount over the inventory we are on that date.
Note: the same item is generally on the list multiple times and this is a representation of actual sales orders. The qty on hand number is the total on hand for that item TODAY and will be the same every time the item is listed.
My issue is that if item a ships 80 cases on 3/4/14 and has 100 on hand then with 20 left over the shipment of 80 cases on 3/10/14 will be 60 cases short so the query will return item a 3/10/14 -60. To indicate that based the current on hand values, on 3/10/14 item a will not have adequate inventory to cover the order.
Itemb 3/8/14 -140
Itema 3/10/14 -60
Any help would be greatly appreciated.
Ken
I think you may want something like this:
SELECT Items,ShipDate,(QtyOnHand-QtyOrdered)
from TableName
where (QtyOnHand-QtyOrdered) < 0
You haven't specified which DBMS you're using. MSSQL 2012 makes this fairly simple:
WITH cteInventory (Items, ShipDate, Stock) As
(
SELECT
Items,
ShipDate,
QtyOnHand - Sum(QtyOrdered) OVER (PARTITION BY Items ORDER BY ShipDate)
FROM
Inventory
)
SELECT
Items,
Min(ShipDate) As Date,
Max(Stock) As Stock
FROM
cteInventory
WHERE
Stock < 0
GROUP BY
Items
ORDER BY
Date
;
SQL Fiddle