Visual Foxpro Query for pending quantity - sql

I am having two tables AORDER for Purchase & BORDER for sale. I want to get pending quantity. Sale orders can have more than 1 records against one purchase order. I do not want to show those order having pending quantities 0. I tried this:
SELECT ;
aorder.orderid,;
aorder.orderdate,;
aorder.itemname,;
aorder.partyname,;
aorder.qty as Purchase,;
SUM(border.qty) AS Sale,;
SUM(aorder.qty-border.qty) as Pending;
FROM ;
aorder;
LEFT JOIN border ;
ON aorder.orderid = border.porderid;
GROUP BY ;
aorder.orderid,;
aorder.orderdate,;
aorder.itemname,;
aorder.partyname,;
aorder.qty
But I am failed to hide those records having purchase qty = sale qty.
Thnx in advance.

As Shahkalpesh mentioned, you do need to apply the having, but your SUM in incorrect.
It should be
aorder.qty - SUM(border.qty) > 0; && also for your field reference.
The reason, SUM is summing each part WITHIN the sum. You will have only one "Purchase" record, but many "Sale" records, as if inventory control First in / First Out (FIFO), Last In / First Out (LIFO), etc
So, say you have PURCHASE order #1 with a quantity of 10, and have sold separate times for quantities 2, 1, 1, 3, 2, 1... Total of 6 sale records. What you are doing is
sum( 10 - 2
+ 10 - 1
+ 10 - 1
+ 10 - 3
+ 10 - 2
+ 10 - 1 )
The revised way is...
10 - SUM( 2 + 1 + 1 + 3 + 2 + 1 )

Related

Finding the exact overlapping time

with tickets as (
select o.SSTID, o.open_Id, o.Createddatetime openTime, c.Createddatetime closeTime
from dbo.Close_ticket c
inner join dbo.Openticket o ON o.SSTID = c.SSTID and c.Open_ID=o.open_id
)
select t1.SSTID,
SUM(isnull(datediff(hour
, case when t1.openTime > t2.openTime then t1.openTime else t2.openTime end
, case when t1.closeTime > t2.closeTime then t2.closeTime else t1.closeTime end),0)) as [OverLappingtime]
from tickets t1
left join tickets t2 on t1.SSTID = t2.SSTID
and t1.openTime < t2.closeTime and t2.openTime < t1.closeTime
and t1.open_id < t2.open_id
group by t1.SSTID
This is my code where each ticket is compared to every other ticket to find the total overlapping time. But if I create more tickets the total time exceeds 24 hours when all the tickets where created on the same day. How can I find the exact overlapping time? If we see the first three tickets, the 2nd and the third ticket were opened and closed within the opening and closing time of the first ticket.
I need the exact overlapping time.
This is my Openticket table.
[Open_ID,SSTID,Createddatetime]
- 1,1,2020-04-27 06:40:32.337
- 2,1,2020-04-27 12:40:32.337
- 3,1,2020-04-27 14:40:32.337
- 4,1,2020-04-27 15:40:32.337
- 5,1,2020-04-27 18:40:32.337
This is my Close_ticket table.
[Close_id,open_id,SSTID,Createddatetime]
- 1,1,1,2020-04-27 20:40:32.337
- 2,2,1,2020-04-27 15:40:32.337
- 3,3,1,2020-04-27 16:40:32.337
- 4,4,1,2020-04-27 17:40:32.337
- 5,5,1,2020-04-27 21:40:32.337
You keep saying "the logic I've used so far is the one I mentioned" but at no point have you actually mentioned this logic in any useful form so that anyone can understand what it is you are doing: all you are doing is stating numbers with no indication on how you calculated these numbers.
Please provide a step by step guide to show how you calculated an overlap figure of 4 hours for the first 3 tickets.
For example, taking your data but moving the start/end times to the hour (rather than 40:32.337) for the sale of simplicity, we have this:
Possible overlap calculations:
2 overlaps 1 by 3 hours => overlap is 3
3 overlaps 1 by 3 hours => overlap is 3
You want to calculate overlap of both 2 & 3 compared to 1: 3 + 3 = 6
You only want the overlap when all 3 tickets overlap: 1
You don't want to double count any overlap: 2 overlaps 1 by 3 hours, 3 overlaps 1 by 3 hours, 2 & 3 overlap each other by 1 hour (double count) => 3 + 3 - 1 = 4
So which of these possible calculations are you using or are you using completely different logic and, if so, what it that logic?

SQL : how to distinguish between different rows with same value in some field and have a separate function applied to another field

I have a query output showing a list of orders. Some orders might occupy more then one record in the query output if those orders consist of sub-orders.Each sub-order occupies a separate line in the output. There is the OrderID column which has the same value for all sub-orders in the output:
OrderID Sub-Order Price
1 1 100
1 2 50
2 1 30
3 1 50
I need to add a column "Discount" to the output and fill it by following rules:
If certain order has one sub-order - the discount is 10% of the Price
If certain order has more than one sub-order, the discount is 20% on all sub-orders'
My query is a UNION of two SELECTs.
I use mssql with ms sql studio
Use CASE and COUNT window function
SELECT OrderID, Sub-Order, Price,
CASE WHEN (count(*) OVER (PARTITION BY OrderID)) > 1
THEN Price * 0.8
ELSE Price * 0.9
END
FROM ( table or <query> )

Calculating Stock movement History

I am currently trying to output Product code and Movement to show a complete history of how many of a product we have sold.
I have two tables PUB.movement and PUB.product
I've made it this far
SELECT a."prod-code" AS productcode, count(*) AS Movement
FROM (SELECT mov."tran-date", pro."prod-code"
FROM PUB."movement" mov,
PUB."product" pro
WHERE mov.SKU=pro.SKU
AND mov."move-type" = 'i'
AND pro."prod-group" like 'SLA%') a
GROUP BY a."prod-code"
The output generated is:
PRODUCTCODE | MOVEMENT
0490786 1
0500012 1
0566003 1
0566004 1
0650594 1
0920127 1
0920154 1
1000557M1 1
1000578M1m 19
The only issue I have is if more than one is invoiced at that time it only counts that invoice as 1, not the quantity invoiced. There is a qty column in PUB.movement. I just can't incorporate it with the current query to output the correct stock movement.
Sounds like you want a SUM() of the qty field rather than a count of movements? Try this:
Select pro."prod-code" As productcode, Sum(mov.qty) As Movement
From PUB.movement As mov
Inner Join PUB.product As pro On mov.SKU = pro.SKU
Where mov."move-type" = 'i'
And pro."prod-group" like 'SLA%'
Group By pro."prod-code";

Finding the sum of sales when a series of conditions are met

I have a database table that looks like below. It contains a key (id) that identified each transaction. Within each transaction, there may be multiple items that were purchased, thus someome with transact 103 has three different id values because they purchased three different items.
Here is what I am trying to do. For a given set of conditions, I want to total number of items that were purchased (item qty). Let's say that my conditions are that for stores 20 and 35, AND items 7, 12, aned 21, I want to find the total number of purchased items (item qty). When condition x is met, which is the reason for the subquery, sun up the item quantity to get total sales.
Can anyone help?
transac id item_qty store item
101 1 2 20 13
102 2 1 35 21
103 3 3 35 16
103 4 1 35 12
103 5 1 35 7
104 6 1 15 21
104 7 2 20 7
I have the following query which is related to my example but when I utilize such queries on my data it returns a null value each time.
SELECT SUM(Cnt) AS "Sales Count"
FROM (SELECT ti.id, SUM(ti.item_qty) AS Cnt
FROM dbo.vTransactions1 ti
WHERE ti.store IN (20, 35)
AND ti.item IN (7, 12, 21)
GROUP BY ti.id) inner_query1;
One way of doing this would be to group by store and item and then calculating the sum. This way you would be able to add more conditions if required based on valid combinations of (Store,Item). You have grouped by id which is not worth as each row will have unique id so no group will be formed.
For given condition you can write as;
;with CTE as
(
select sum(item_qty) as Cnt,store,item
from test
group by store,item
)
select sum (Cnt) as [Sales Count]
from CTE
where store in (20,35)
and item in (7,12,21))
SQL Fiddle Demo here.
I have no idea why there is a subquery here. Unless I'm missing something, this should work:
select sum (item_qty)
FROM dbo.vTransactions1 ti
WHERE ti.store IN (20, 35)
AND ti.item IN (7, 12, 21)
If you need to add extra conditions in the outer query - you either need to
remove the "Sum" from Sum(Cnt) as the Sum is already done in the sub query (and probably return the ID as well); OR
You need to add GROUP BY condition to the outer query as well.

Omit item from Sum SQL

I'm very new to programming and SQL, I can't figure this one out, perhaps I haven't learned the concept yet, but I'm hoping you can help me. Sorry if it's too easy and boring.
/*2.37 Write an SQL statement to display the WarehouseID and the sum of
QuantityOnHand,grouped by WarehouseID. Omit all SKU items that have 3 or more items
on hand from the sum, and name the sum TotalItemsOnHandLT3 and display the results
in descending order of TotalItemsOnHandLT3.*/
SELECT WarehouseID, SUM(QuantityOnHand) AS TotalItemsOnHandLT3
FROM INVENTORY
GROUP BY WarehouseID
HAVING COUNT(WarehouseID) >= 3
ORDER BY TotalItemsOnHandLT3 DESC
"Omit all SKU items that have 3 or more items on hand from the sum", sounds more like :
FROM INVENTORY WHERE QuantitiyOnHand < 3
rather than :
HAVING COUNT(WarehouseID) >= 3
INVENTORY is the list of products (SKU = Stock Keeping Unit = Individual Product Stored in the warehouse) where every product has a WarehouseID. This warehouseID presumably determines where the product is stored.
By Omit all SKU items, it asks you to only display those products that are stored in minimum 3 places in the warehouse. This can be done with the having clause,
HAVING COUNT(WarehouseID) >= 3
I do not know the structure and data of your INVENTORY table, but simply put, Consider your data is like this:
SKUID WareHouseID QuantityOnHand
1 1 10
1 2 10
2 1 10
1 3 5
2 2 20
In the above case, Product = 1 (SKUID), is stored in 3 different warehouses whereas product 2 is stored in 2 warehouses. Hence,
SKUID COUNT(WareHouseID) SUM(QuantityOnHand)
1 3 25
2 2 30
In this case, your query will only "Omit" product 1, and not the product 2.
Its says Omit, they why
HAVING COUNT(WarehouseID) >= 3
and not
HAVING COUNT(WarehouseID) < 3