i have some problem about sql , i want to select the bigger warehouse_product_id and with same product_id row,i have try to use
please help
Table:
http://postimg.org/image/mkavxnmg9/
i want to:
http://postimg.org/image/8msomu47l/
SELECT warehouse_product_id FROM [your table] where product_id="value"
unless I missed what you're getting at, which is entirely possible
You seem to be looking for a greatest-n-per-group solution. For a given product_id and warehouse_id pair you need to get the max warehouse_product_id:
SELECT t1.* FROM aTable t1
JOIN (
SELECT product_id, warehouse_id, MAX(warehouse_product_id) maxVal
FROM aTable
GROUP BY product_id, warehouse_id
) t2
ON t1.product_id = t2.product_id AND t1.warehouse_id = t2.warehouse_id AND t1.warehouse_product_id = t2.maxVal
Alternatively:
SELECT t1.* FROM aTable t1
LEFT JOIN aTable t2
ON t1.product_id = t2.product_id AND t1.warehouse_id = t2.warehouse_id AND t1.warehouse_product_id < t2.warehouse_product_id
WHERE t2.warehouse_product_id IS NULL
These solutions should work on most DBMS.
I have assumed qty and enable have the same values for the same warehouse/product. If not, you'll need to join back to the table to get those.
select warehouse_id, max(warehouse_product_id) as item_id, product_id, max(qty), max(enable)
from warehouse_product
group by warehouse_id, product_id
Related
I have a table like this
I would like to select the Itemid that occurs more than once with a different Rate with group by Masterid
The output should be something like:
You might try the following:
SELECT masterid, detailid, itemid, rate FROM mytable
WHERE (masterid, detailid, rate) IN
(
SELECT masterid, detailid, rate FROM mytable t
JOIN mytable o ON o.masterid = t.masterid
AND o.detailid = t.detailid AND o.rate <> t.rate
GROUP BY t.masterid, t.detailid, t.rate
HAVING COUNT(*) >= 2
)
The inner join within the sub-query assures only rows appearing that have an unequal counter part. Alternatively you might add another sub-query condition to the outer query:
AND EXISTS
(
SELECT * FROM mytable o
WHERE o.masterid = t.masterid AND o.detailid = t.detailid AND o.rate <> t.rate
)
I believe you are looking for a query like below
select t1.* from t t1
join
(
select masterid,itemid
from t
group by masterid,itemid
having count(distinct rate )>1
)t2
on t1.masterid=t2.masterid and t1.itemid=t2.itemid
order by masterid,detailid
and here's a working db fiddle
Try following code:
Select masterid, detailid, rate, count(*) as count from Mytable
group by masterid, detailid, rate
having count(*) > 1
So this one should be pretty simple for most of you:
My table has an ID, an order_id and a status.
The same order_id may have several IDs.
What I need to do is get the last ID from each order_id, which is pretty simple:
SELECT order_id, max(ID) AS last_id
FROM mytable
GROUP BY order_id
Now, I also need to get the status that is linked to last ID, so what I was trying to do was:
SELECT order_id, max(ID) AS last_id, x.status
FROM mytable t
LEFT JOIN (SELECT ID, status
FROM mytable) x ON last_id = x.ID
I know I'm not allowed to use the last_id alias to join the subquery, as it says it does not exist. So how do I go about this?
You can't use the alias in the FROM or in the WHERE parts of the query, you should use max(t.ID):
SELECT order_id, max(t.ID) AS last_id, x.status
FROM mytable t
LEFT JOIN (SELECT ID, status
FROM mytable) x ON MAX(t.ID) = x.ID
You can also wrap the query as a subquery and then do the join using the alias:
SELECT t.order_id, t.last_id, x.status
FROM (
SELECT order_id, max(ID) AS last_id
FROM mytable
) t
LEFT JOIN mytable x
ON t.last_id = x.ID
An alternative is to DISTINCT ON the column order_id and then apply max() on id.
SELECT DISTINCT ON (order_id)
order_id, max(id) AS last_id,
status
FROM mytable
GROUP BY order_id,status;
Demo: db<>fiddle
So my current problem is that I have two tables that look like this:
table1(name, num_patient, quant, inst)
table2(inst_name, num_region)
Where I want to find the patient with max quantity per region.
I first had the idea of doing something like this:
SELECT num_region, num_patient, MAX(quant)
FROM
(SELECT num_patient, quant, num_region
FROM table1
INNER JOIN table2
ON table1.inst = table2.inst_name) AS joined_tables
GROUP BY num_region;
But this doesn't work since either num_patient has to be on the GROUP BY (and this way it doesn't return the max value by region anymore) or I have to remove it from the SELECT (also doesn't work because I need the name of each patient). I have tried to fix my issue with a WHERE quant = MAX() statement but couldn't get it to work. Is there any workaround to this?
Use DISTINCT ON:
SELECT DISTINCT ON (num_region), num_patient, quant, num_region
FROM table1 t1 JOIN
table2 t2
ON t1.inst = t2.inst_name
ORDER BY num_region, quant DESC;
DISTINCT ON is a convenient Postgres extension. It returns one row per keys specified in the SELECT, based on the ordering in the ORDER BY.
Being an extension, not all databases support this functionality -- even databases derived from Postgres. The traditional method would use ROW_NUMBER():
SELECT t.*
FROM (SELECT num_patient, quant, num_region,
ROW_NUMBER() OVER (PARTITION BY num_region ORDER BY quant DESC) as seqnum
FROM table1 t1 JOIN
table2 t2
ON t1.inst = t2.inst_name
) t
WHERE seqnum = 1;
This is a duplicate of the DISTINCT ON question I linked.
SELECT distinct on (num_region) num_patient, quant, num_region
FROM table1
INNER JOIN table2
ON table1.inst = table2.inst_name
ORDER BY num_region, quant desc
I am trying to build a SQL query to recover only the most young record of a table (it has a Timestamp column already) where the item by which I want to filter appears several times, as shown in my table example:
.
Basically, I have a table1 with Id, Millis, fkName and Price, and a table2 with Id and Name.
In table1, items can appear several times with the same fkName.
What I need to achieve is building up a single query where I can list the last record for every fkName, so that I can get the most actual price for every item.
What I have tried so far is a query with
SELECT DISTINCT [table1].[Millis], [table2].[Name], [table1].[Price]
FROM [table1]
JOIN [table2] ON [table2].[Id] = [table1].[fkName]
ORDER BY [table2].[Name]
But I don't get the correct listing.
Any advice on this? Thanks in advance,
A simple and portable approach to this greatest-n-per-group problem is to filter with a subquery:
select t1.millis, t2.name, t1.price
from table1 t1
inner join table2 t2 on t2.id = t1.fkName
where t1.millis = (select max(t11.millis) from table1 t11 where t11.fkName = t1.fkName)
order by t1.millis desc
using Common Table Expression:
;with [LastPrice] as (
select [Millis], [Price], ROW_NUMBER() over (Partition by [fkName] order by [Millis] desc) rn
from [table1]
)
SELECT DISTINCT [LastPrice].[Millis],[table2].[Name],[LastPrice].[Price]
FROM [LastPrice]
JOIN [table2] ON [table2].[Id] = [LastPrice].[fkName]
WHERE [LastPrice].rn = 1
ORDER BY [table2].[Name]
I have a table with Item, Price and Price Date columns. What I need to do is filter out only the old prices, showing only the most recent price for each item. I am using MS Access 2007 and need to reference the most updated price in some VBA I am writing, and in my research I cannot find a way to programmaticly access pivot table data for customer pricing elsewhere in the DB.
Example:
Table
Item | Price | Price Date
You can use a correlated subquery:
select t.*
from t
where t.pricedate = (select max(t2.pricedate)
from t as t2
where t2.item = t.item
);
One option uses a subquery to find the most recent price for each item:
SELECT t1.Item, t1.Price, t1.PriceDate
FROM yourTable t1
WHERE t1.PriceDate = (SELECT MAX(t2.PriceDate) FROM yourTable t2 WHERE t2.Item = t1.Item);
This is a bit more efficient than using MAX()-function:
SELECT Item, Price, PriceDate
FROM customer_pricing t1
WHERE NOT EXITS(
SELECT *
FROM customer_pricing t2
WHERE t1.item = t2.item
AND t1.PriceDate < t2.PriceDate
)
The solution was by #dewey and was
SELECT t1.item, t1.unit_price1, t1.effect_date, t1.site_ref
FROM dbo_itemprice_mst_all AS t1
WHERE (((t1.site_ref)="MED" Or (t1.site_ref)="CRD") AND ((Exists (SELECT *
FROM dbo_itemprice_mst_all t2
WHERE t1.item = t2.item
AND t1.effect_date < t2.effect_date
))=False))
ORDER BY t1.item;