Having a table with columns 'price' and 'quantity'.
e.g:
rec price qty
1. 10,00 1
2. 7,50 5
3. 5,00 25
4. 3,00 100
I need to select the price for a quantity of 65. This is the price of record 3. Qty 65 is between qty 25 and 100. How to solve this in a sql query?
You can solve that with an inner SQL statement which tries to find the highest quantity lower than or equal to your requested quantity of 65:
select pce.price
from prices pce
join ( select max(qty) qty
from prices
where qty <= 65
) pce2
on pce.qty = pce2.qty
Here pce2 is the join to match the prices line. The pce table is joined to have access to all fields joined. This will only work correctly if there are no duplicates in prices for qty.
Related
I have a SQL query which will return product list and it's respective sales. Below is the query
SELECT *
FROM sales
where sales.name IN ('product_1', 'product_2')
AND sales.product_number IN (SELECT number FROM products) where sales.name = 'product_2'
There are some unnecessary rows in product_2 which i want to filter out with the help of prod_number.
For example the output of above query is
cust_id name product_number
1 product_1 11
2 product_2 22
3 product_2 23
4 product_2 34
Now i want to filter out the product_2 rows based on it's product_number. I want product_2 only with product_number 22 and 23. I tried the above query but it's returning an error.
Use an OR condition to deal with the two cases.
SELECT *
FROM sales
WHERE (name = 'product_1' AND product_number IN (SELECT number FROM products))
OR (name = 'product_2' AND product_number IN ('22', '23'))
Since MySQL often optimizes OR poorly, you may get better results if you split this into two queries that you combine with UNION.
SELECT s.*
FROM sales AS s
JOIN products AS p ON s.product_number = p.number
WHERE s.name = 'product_1'
UNION ALL
SELECT *
FROM sales
WHERE name = 'product_2' AND product_number IN ('22', '23')
EDIT ---
SQL FIDDLE sqlfiddle.com/#!18/f08bd/4
I couldn't load all the data into this as running out of limit on SQL FIDDLE but somehow the results are correct there with the little of sample data, but on my database it doubles somehow.
I am trying to display a total quantity of products, their weight and price.
The results come from 4 different tables.
When I do this without join then I get proper values but when I use Join then I get double values on all outputs.
Without join I can't achieve this as these results need to be all in one table.
I tried not using Join and just including those tables in 'FROM' but that thrown me errors about issues converting to numeric. I also tried Union but didn't work.
When using no join and not trying to display all the values, I get the desired output but with missing columns that I missed out on purpose to test this.
SELECT PriceListTest.Description, COUNT(ItemCode) AS Quantity,
SUM(Weight) AS 'Weight', Item.Pieces, PriceListTest.Price,
CAST(SUM( PriceListTest.Price * Weight) as
DECIMAL(10,2)) as 'Unit Price', CAST(SUM(PriceListTest.Price * Weight) as
DECIMAL(10,2))
AS 'Nett Amount'
FROM StockItems
INNER JOIN PriceListTest ON
StockItems.ItemCode = PriceListTest.Description
INNER JOIN Item ON StockItems.ItemCode = Item.ShortCode
WHERE Barcode IN (SELECT DISTINCT Barcode FROM StockOuttbl
WHERE ContainedID = 'isr5063' AND Status ='' GROUP BY Barcode) AND
PriceListTest.CustomerID = (SELECT DISTINCT CustomerID From Customerstbl
WHERE CustomerID ='1')
GROUP BY PriceListTest.Description, Item.Pieces, PriceListTest.Price;
Status in this case is empty, so that's not the issue
I getting these values:
Description Quantity Weight Pieces Price Unit Price Nett Amount
MAJ 52 20242 0 1.23 24897.66 24897.66
FLOCK 50 17206 0 1.23 21163.38 21163.38
This is the output I am looking:
Description Quantity Weight Pieces Price Unit Price Nett Amount
MAJ 26 10121 0 1.23 12448.83 12448.83
FLOCK 25 8603 0 1.23 10581.69 10581.69
When I don't use the PriceListTest in Join then I don't get the doubles, but then it's not exactly what I am looking for.
I get:
Description Quantity Weight Pieces
MAJ 26 10121 0
FLOCK 25 8603 0
EDIT-- Added the data for the tables
PriceListTest---
OID ShortCode Description CustomerID Price
7372 MAJ MAJ 1 1.23
7373 FLOCK FLOCK 1 1.23
StockItems---
TimeStamp DateStamp ItemCode Barcode ID Weight
104414357 20190701 MAJ 20190701104413935 7198 302
125350401 20190701 MAJ 20190701125349979 7220 360
125507063 20190703 MAJ 20190703125506641 7513 336
StockOutTbl---
ID AddedTimeStamp Quant Line UserID Weight Barcode Status Type StockoutTimeStamp StockoutUser TerminalStockOut TerminalAdded AddedDateStamp StockOutDateStamp ContainedID
41 115020205 NULL NULL NULL 336 20190703125506641 NULL 115020208 user 1 TC20 NULL 20190704 20190704 isr5063
Item Table ----
OID ShortCode ScreenCode Description AdminOid Kilos Pieces Inactive CategoryTitleStr BigBale
203 MAJ MAJ MAJ NULL 0 0 0 45 1
204 FLOCK FLOCK FLOCK NULL 0 0 0 45 1
Excuse me for the bad formatting.
I'd appreciate any help with this. Thanks in advance!
use subquery and distinct prcelist from 2nd table in subquery then use join
and there is no need GROUP BY Barcode insside your subquery cause you alredy used distinct
SELECT PriceListTest.Description, COUNT(ItemCode) AS Quantity,
SUM(Weight) AS 'Weight', Item.Pieces, PriceListTest.Price,
CAST(SUM( PriceListTest.Price * Weight) as
DECIMAL(10,2)) as 'Unit Price', CAST(SUM(PriceListTest.Price * Weight) as
DECIMAL(10,2))
AS 'Nett Amount'
FROM StockItems
INNER JOIN( select distinct * from PriceListTest) as PriceListTest ON
StockItems.ItemCode = PriceListTest.Description
INNER JOIN Item ON StockItems.ItemCode = Item.ShortCode
WHERE Barcode IN (SELECT DISTINCT Barcode FROM StockOuttbl
WHERE ContainedID = 'isr5063' AND Status ='' ) AND
PriceListTest.CustomerID = (SELECT DISTINCT CustomerID From Customerstbl
WHERE CustomerID ='1')
GROUP BY PriceListTest.Description, Item.Pieces, PriceListTest.Price;
I have three tables in my SQL server setup.
Two of the tables are given below:
Product Substitute
XXX ABC
YYY XYZ
Table two is similar to table one but lists alternates.
Product Alternate
XXX OPPP
YYY FTTT
And I have a master table with the quantities of all the products (replacements and alternates included):
Product Quantity
XXX 52
YYY 84
ABC 180
XYZ 220
OPPP 590
FTTT 760
Now I need an SQL query to list the total parts available for each main part number. For example, the output query should look like:
Product Quantity
XXX 822
YYY 1064
That is the total amount of XXX = total of XXX + total of ABC + total of OPPP.
You first get all the data you need (inner query with union all).
Then you group the whole set of data by Product.
Select Product, Sum(Quantity)
From (
Select Product, Quantity From master
Union All
Select a.Product, m.Quantity From master as m
Inner join alternates as a on A.alternate = m.product
Union All
Select s.Product, m.Quantity From master as m
Inner join Substitute as s on s.Substitute = m.product
) as p
Group By Product
I have this Query:
Select Denumire,Sum(Livrari.Cantitate)as Stoc,
isnull(Sum(Facturi_Emise.Cantitate),0) as 'Sold',
isnull(Sum(Livrari.Cantitate),0)-isnull(sum(Facturi_Emise.Cantitate),0) As 'Stoc After Sales',
Livrari.Id_Depozit
From Livrari
Left join Produse On Livrari.Id_Produs=Produse.Id_Produs
Left join Facturi_Emise On Produse.Id_Produs=Facturi_Emise.Id_Produs
group by Denumire, Livrari.Id_Depozit
order by Denumire
I need to get the Stock, the quantity of the items that are required to be sold, and what I got in the 2 deposits after the items are sold. But in the table "Livrari" I got the same item in both deposit, with the amount of 300 for deposit 1, and 300 for deposit 2 and when I make the sum and group by Id_Depozit I get that item with the amount of 600 in deposit 1 and 600 in deposit 2, same is for table Facutri_Emise as I should take only 250 items from deposit 1 and 300 from deposit 2, it takes 550(250+300) from both deposit and I got the Stoc after Sale 50 for each, instead of 50 for deposit 1 and 0 for deposit 2.
fiddle
My problem is that the result in row 8 and 9(that means Pahare) in column Stoc should be 300 for each. In next column Sold, row 8 should be 250 and for row 9 300. In the end, in column Stoc after Sale should be the difference, as row 8=50, row 9=0.
I mean each time a products repeats, my query is gonna make the sum and put the same value in deposit 1 and in deposit 2, as I give different values for each deposit it should show me the correspondent values. A supplier will bring a certain amount if items(let's say apples) into deposit 1 and another supplier will bring another amount of apples in deposit 2. When the bill is made("Facturi_Emise") for the client I should sent an amount of apples from deposit 1 and another from deposit 2 and I wanna see what amount remains in one deposit,and what remains in other one.
Here it is.
It is not ordered by deposit numbers,but it's okey.
SELECT Produse.Denumire,
Isnull(a.suma,0) AS Intrari,
Isnull(b.suma,0) AS Iesiri,
isnull(a.suma,0) - isnull(b.suma,0) AS Stoc
FROM Produse
LEFT JOIN
(SELECT ID_Produs, Sum(Cantitate) AS suma FROM Livrari GROUP BY ID_Produs)
AS a
ON Produse.ID_Produs = a.ID_Produs
LEFT JOIN
(SELECT ID_Produs, Sum(Cantitate) AS suma FROM Facturi_Emise GROUP BY ID_Produs)
AS b
ON Produse.ID_Produs = b.ID_Produs
SOLLUTION
Select Denumire,Sum(Livrari.Cantitate)as Stoc,
SUM(ISNULL(Facturi_Emise.Cantitate,0)) as 'Sold',
SUM(ISNULL(Livrari.Cantitate,0))-SUM(ISNULL(Facturi_Emise.Cantitate,0)) As 'Stoc After Sales',
Livrari.Id_Depozit
From Livrari
Left join Produse On Livrari.Id_Produs=Produse.Id_Produs
Left join Facturi_Emise On Produse.Id_Produs=Facturi_Emise.Id_Produs
and Livrari.Id_Depozit=Facturi_Emise.Id_Depozit
group by Denumire, Livrari.Id_Depozit
order by Denumire
You need ceck null before sum, soo first ISNULL and then SUM.
If you SUM and one is null then all SUM will be null.
I have 2 tables like this:
Stock Table
product_id bigint(20)
qty float
Sales Table
product_id bigint(20)
qty float
Sample Data
Stock Table
product_id---qty
1---10
2---11
3---20
4---50
1---10
3---10
Sales Table
product_id---qty
1---2
2---5
3---20
4---40
1---7
I want the following Output after running the Query
product_id---qty
1---11
2---6
3---10
4---10
Well, as spender ask I am trying to more clear the situation.
First of All, let's think that I store
10 quantity of product 1
11 quantity of product 2
20 quantity of product 3
50 quantity of product 4
10 quantity of product 1 (now I have total 20 of product 1)
10 quantity of product 3 (now I have total 30 of product 3)
Secondly, let's think that I sell
2 quantity of product 1
5 quantity of product 2
20 quantity of product 3
40 quantity of product 4
7 quantity of product 1 (now I have sold total 9 of product 1)
Thirdly, I want to know how much stock is now in my hand
11 quantity of product 1 (20-9 = 11)
6 quantity of product 2 (11-5 = 6)
10 quantity of product 3 (30-20 = 10)
10 quantity of product 4 (50-4 = 10)
My Question is: To find out this stock what is the Query?
Thanks in Advance for answering my question.
This answer works in Oracle - don't have MySql so can't test there
select product_id, sum(qty) from
(
select product_id, qty from stock
union all
select product_id, (-1 * qty) from sales
) as a
group by prod
You question is lacking detail and looks like it might even contain typos in the presented data. I'm going to make the assumption you are trying to calculate the diff between stock quantities and sales quantities, despite your data not actually supporting this (!!!). It looks like you require the following:
select
st.product_id,
sto.qty-st.qty
from
salesTable as st
join stockTable as sto on sto.product_id=st.product_id
Chris's answer is absolutely correct. But for the information I want to add this one which I found on NET.
SELECT tunion.product_id, (
(IFNULL((SELECT SUM(s.qty) FROM stock s WHERE s.product_id=tunion.product_id),0))-
(IFNULL((SELECT SUM(p.qty) FROM sales p WHERE p.product_id=tunion.product_id),0)))
AS quantity
FROM (SELECT DISTINCT s.product_id FROM stock s
UNION ALL SELECT DISTINCT p.product_id FROM sales p)
AS tunion GROUP BY tunion.product_id