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
Related
I'm trying to update the values of a select number of rows from one column to each be incremented by a certain value taken from another column from another table but I keep hitting this wall : 'single-row subquery returns more than one row'.
Essentially what this is all about is deleting a hypothetical shopping cart (each row in the shopping cart corresponds to a certain item and a desired quantity of that item) but before doing that I must first make sure that all the item quantities from the cart are returned to the product relation which keeps track of all available items and the quantity of those items currently on supply.
UPDATE Product SET supply = (...) - subquery returning a single column list of all quantites to be added back to the respective product supplies, ordered by the product_id - even though it's not included in the select statement
WHERE product_id IN (...) - same query as the last one but returns the product ids of all the corresponding shopping cart items rather than their quantities, again ordered by product_id
All looks fine but ORACLE's not having it. Perhaps this can be looped somehow? If not I'll have to resort to doing it in the app layer (PHP)
Product relation:
product_id
Supply
...
1
10
...
2
20
...
3
10
...
Basket relation:
product_id
quantity
...
1
3
...
2
4
...
Upon the deletion of the cart I would like the two quantities from the cart table to be added back to their corresponding rows in the supply column in the product relation
This:
product_id
Supply
...
1
10
...
2
20
...
3
10
...
Turns into this:
product_id
Supply
...
1
13
...
2
24
...
3
10
...
You can use corelated subquery as follows:
Update product p
Set p.supply = p.supply + coalesce(select quantity from basket b
Where b.product_id = p.product_id), 0)
It is considered good to use exists to reduce the redo logs as follows:
Update product p
Set p.supply = p.supply + (select quantity from basket b
Where b.product_id = p.product_id)
WHERE EXISTS
(Select 1 from basket b
Where b.product_id = p.product_id)
Please note that we have removed the coalesce in this query as it is not required if we use exists.
I have two tables Medication and Inventory. I'm trying to SELECT all the below details from both tables but there are multiple listings of medication ids with different BRANCH_NO also in the INVENTORY table (the primary key in INVENTORY is actually BRANCH_NO, MEDICATION_ID composite key)
I need to total up the various medication_IDs and also join the tables in one SELECT command and display all the infomation for each med (there are 5) with a total sum of each med at the end of each row. But im getting all muddled trying Group by and Sum and at one point partition. Help please I'm new to this.
Below is the latest non working version - but it doesn't display
Medication Name
Medication Desc
Manufacturer
Pack Size
like i chanced it might.
SELECT I.MEDICATION_ID,
SUM(I.STOCK_LEVEL)
FROM INVENTORY I
INNER JOIN (SELECT MEDICATION_NAME, SUBSTR(MEDICATION_DESC,1,20) "Medication Description",
MANUFACTURER, PACK_SIZE FROM MEDICATION) M ON MEDICATION_ID=I.MEDICATION_ID
GROUP BY I.MEDICATION_ID;
For the data imagine I want this sort of output:
MEDICATION_ID MEDICATION_NAME STOCK_LEVEL OtherColumns.....
1 Alpha 10
2 Bravo 20
3 Charlie 20
1 Alpha 30
4 Delta 10
5 Echo 20
5 Echo 40
2 Bravo 10
grouping and totalling into this:
MEDICATION_ID MEDICATION_NAME STOCK_LEVEL OtherColumns.....
1 Alpha 40
2 Bravo 30
3 Charlie 20
4 Delta 10
5 Echo 60
I can get this when its just one table but when Im trying to join tables and also SELECT things its just not working.
Thanks in advance guys. I appreciate it may be a simple solution, but it will be a big help.
You need to write explicitly all non-aggregated columns into both SELECT and GROUP BY lists ( Btw, no need to use a nested query, and if it's the case MEDICATION_ID column is missing in it ) :
SELECT I.MEDICATION_ID, M.MEDICATION_NAME, SUM(I.STOCK_LEVEL) AS STOCK_LEVEL,
SUBSTR(M.MEDICATION_DESC,1,20) "Medication Description", M.MANUFACTURER, M.PACK_SIZE
FROM INVENTORY I
JOIN MEDICATION M ON M.MEDICATION_ID = I.MEDICATION_ID
GROUP BY I.MEDICATION_ID, M.MEDICATION_NAME, SUBSTR(M.MEDICATION_DESC,1,20),
M.MANUFACTURER, M.PACK_SIZE;
This way, you'll be able to return all the listed columns.
my apologies if this is a duplicate but I could not find an answer to my particular question. I have a table that lists products on a sales order, and their various quantities. Some products are components for other products and are denoted so with a flag. I would like to know if there is a way to have a running total for the parent/normal items that would reset on each parent/normal item.
Here is an example of the table data and my desired output:
OrderNo Item Qty Regular Line
349443 AFU20451-KIT1 1 Y 1
349443 AFU20451 0 N 2
349443 HAWKE-14252 1 N 3
349443 RGPM-25H4 1 N 4
349443 AV-003-265 1 Y 5
349443 AV-A00090-KIT 1 Y 6
349443 AV-A00091 1 N 7
349443 AV-A00090 1 N 8
349443 AV-00043 1 N 9
349443 AV457/310GR/FP 2 Y 10
desired output:
OrderNo Item Qty
349433 AFU20451-KIT1 3
349433 AV-003-265 1
349433 AV-A00090-KIT 4
349433 AV457/310GR/FP 2
As you can see, I would like to reset the sum every time it says Y, only include the parent item (I could get around this as I can keep the order of the items the same, could maybe use row number). I have been trying to use Over and Partition by in order to do this, but to no avail. Let me know if this is even possible or if you need any further information.
with cte as
(
select OrderNo,
-- only return the main item
case when Regular = 'Y' then Item end AS Item,
Qty,
-- assign a unique number to each `YNNN..` component group
-- needed for GROUP BY in next step
sum(case when Regular = 'Y' then 1 else 0 end)
over (partition by OrderNo
order by Line
rows unbounded preceding) as grp
from myTable
)
select OrderNo,
-- find the matching value for the main component
max(Item),
sum(Qty)
from cte
group by OrderNo, grp
Current representation is against 1st Codd's rule.
Rule 1: The information rule: All information in a relational data
base is represented explicitly at the logical level and in exactly one
way – by values in tables.
But I believe you can still create FUNCTION/PROCEDURE and iterate row one by one with IF statement for Y/N. E.g. you create new table, IF Y - add new row to table, IF N - add +1 to QTY to latest row.
I would create two separate tables: manufacturer & part, to get the values so you don't have to hand-jam each inventory, or care about where they fall in the invoice list.
[1
[]2
Then, all you would need to do is compare the values to the part table to get this data. It's more work upfront, but will pay off to have this all saved and stored. A future sample query would look something like:
SELECT OrderNo.OrderTable, Item.OrderTable, Sum(Qty.OrderTable) AS Quantity
FROM OrderTable INNER JOIN Part ON OrderTable.Item = Table.PartName
GROUP BY OrderNo.OrderTable, Item.OrderTable, Regular.OrderTable, Part.ParentID;
try this:
select orderno, item, sum(qty) over(partition by regular order by regular)
from your_table
group by orderno, item, regular
I have a child table with a number of charact/value pairs for a given 'material' (MaterialID). Any material can have a number of charact values and may have several of the same name (see id's 2,3).
The table has a large number of records (8+ million). What I'm trying to do is find the materials that are the most similar to a supplied material. That is, when I supply a MaterialID, I would like an ordered list of the most similar other materials (those with the most matching charact/value pairs).
I've done some research but, I may be missing some key terms or just not conceptualizing the problem correctly.
Any hints as to how to go about this would be very much appreciated.
ID MaterialID Charact Value
1 1 ROT_DIR CCW
2 1 SPECIAL_FEATURE CATALOG_CP
3 1 SPECIAL_FEATURE CHROME
4 1 SCHEDULE 80
5 2 BEARING_TYPE SB
6 2 SCHEDULE 80
7 3 ROT_DIR CCW
8 3 SPECIAL_FEATURE CATALOG_HSB
9 3 BEARING_TYPE SP
10 4 NDE_STYLE W_FAN
11 4 BEARING_TYPE SB
12 4 ROT_DIR CW*
You can do this with a self join:
select t.materialid, count(*) as nummatches
from t join
t tmat
on t.Charact = tmat.Charact and t.value = tmat.value
where tmat.materialid = #MaterialId
group by t.materialid
order by nummatches desc;
Notes:
You might want to remove the specified material, by adding where t.MaterialId <> tmat.MaterialId to the where clause.
If you want all materials, then make the join a left join and move the where condition to the on clause.
If you want only one material with the most matches, use select top 1.
If you want all materials with the most matches when there are ties, use `select top (1) with ties.
My database contains a list of products and finishes. I have been able to successfully average the price of each finish option, but I only want to display the lowest priced option.
Here is a sample of my data.
Table 1 - product_t
ProductID ProductLineID ProductDescription ProductFinish ProductStandardPrice ProductOnHand
1 1 "Cherry End Table" Cherry 175.00 0
2 1 "Birch Coffee Tables" Birch 200.00 0
3 1 "Oak Computer Desk" Oak 750.00 0
4 1 "Entertainment Center" Cherry 1650.00 0
This query results in a list of each available product finish and the average price of an item that has the finish.
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
However, I only want to display the lowest price that results from this query. I attempted this query, but it will not execute.
SELECT ProductFinish, MIN (AVG(ProductStandardPrice)) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
Thanks in advance for any help. It is much appreciated.
A typical way to get the lowest value is to use order by and some sort of limit on the number of rows. In ANSI standard SQL, this would be:
SELECT ProductFinish, AVG(ProductStandardPrice) as AveragePrice
FROM product_t
WHERE ProductFinish IS NOT NULL
GROUP BY ProductFinish;
ORDER BY AveragePrice ASC
FETCH FIRST 1 ROW ONLY;
Note that some databases using LIMIT 1 or TOP 1 instead of FETCH FIRST 1 ROW ONLY. Also, this version only fetches one record with the lowest price. If there are duplicate minimums, an arbitrary value is chosen.