I would like to update row using next couple of rows.
I have database like
id Product_number qty parent_id type
1 AAAA null PRODUCT
2 bbb 5 1 ITEM
3 ccc 8 1 ITEM
I have this type of data into table, so I want to update qty column of that row where type is PRODUCT using min value of those rows where parent_id is id of row which has column type value PRODUCT etc
so result should be
id Product_number qty parent_id type
1 AAAA 5 null PRODUCT
2 bbb 5 1 ITEM
3 ccc 8 1 ITEM
Is this what you want?
update t join
(select parent_id, min(qty) as min_qty
from t
group by parent_id
) tt
on t.id = tt.parent_id
set t.qty = tt.min_qty
where t.qty is null and t.type = 'ITEM';
Related
i have 3 tables to count the item price, and i need to know total of spent too,
table data
id
name
1
data
2
data2
detail
id
data id
item
price
1
1
bag
2000
2
1
chair
2000
table spent
id
detail id
spent
1
1
200
2
1
200
expected result
-> data | 4000 | 400
SELECT data.name, d.price, s.spent
FROM data
JOIN ( SELECT data_id id, SUM(price) price
FROM detail
GROUP BY id ) d USING (id)
JOIN ( SELECT detali_id id, SUM(spent) spent
FROM spent
GROUP BY id ) s USING (id)
I have a table that stores values for attributes associated to products.
row_id product_id attribute_id value
1 1 1 a
2 1 2 b
3 2 1 d
4 2 2 e
How do i write a select to get the value for attribute_id=2 only for products that have value="a" for attribute_id=1?
Thanks,
Have a nice day
Maybe this is what you want?
select *
from your_table
where attribute_id = 2
and product_id in (
select product_id
from your_table
where attribute_id = 1 and value = 'a'
)
With your sample data the row with row_id = 2 would be returned.
i have data below for example
id product_id date
------ ---------- ----
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
result data query that i want "the last record of last date on each product_id"
to get it that result i use the query like below
SELECT a.* FROM test AS a
JOIN (SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE FROM test GROUP BY product_id) AS b
ON a.id = b.id AND a.product_id = b.product_id AND a.date = b.date
this time i got what i want as the result
id product_id date
------ ---------- --------
3 1 3
6 2 2
7 3 1
my problem when i add another data like below
id product_id date
------ ---------- --------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 2
7 3 1
8 1 3
9 1 2
and use the same query the result become like this
id product_id date
------ ---------- --------
6 2 2
7 3 1
where the the value '1' for product_id?
Try this
SELECT id, product_id, DATE FROM test sitem WHERE product_id IN (1,2,3) AND DATE = (SELECT DATE FROM test WHERE product_id =
sitem.product_id ORDER BY DATE DESC LIMIT 1) AND id =
(SELECT id FROM test WHERE product_id = sitem.product_id ORDER BY DATE DESC,
id DESC LIMIT 1) GROUP BY product_id
This is your subquery:
SELECT MAX(id) AS id, product_id, MAX(DATE) AS DATE
FROM test
GROUP BY product_id
It is independently calculating the maximum of id and date. But, there is no guarantee that these two values are in the same record. There are ways to fix the subquery, but they are rather complicated.
Instead, I would suggest using an alternative method to get the last record:
SELECT t.*
FROM test t
WHERE NOT EXISTS (SELECT 1
FROM test t2
WHERE t2.product_id = t.product_id AND
(t2.date > t.date OR
t2.date = t.date AND t2.id > t.id
);
This identifies the last record for each product as the one where no other record has a larger date. And, if two records have the same date, no other record has a larger id.
I'm using the SQL Server 2008.
Now I have the scenario as description below:
1 table with 3 columns: ID, Name, Order.
They has 8 records.
5 records have the same data: ID='1', Name='AAA'
3 records have the same data: ID='2', Name='BBB'
Now I want to update Order column with number increasing (start from 1) for each ID,Name:
No Name Order
1 AAA 1
1 AAA 2
1 AAA 3
1 AAA 4
1 AAA 5
2 BBB 1
2 BBB 2
2 BBB 3
How can I get this result without using Cursors?
I'm very appriciated for your help.
Thanks.
You can try the following. I use CTE to drive the update statement
WITH data AS
(
SELECT Order
, ROW_NUMBER() OVER (PARTITION BY ID, NAME
ORDER BY ID, NAME) AS Seq
FROM TableA
)
UPDATE data
SET Order = Seq
Substitute TableA with your table name
I am writing a query to grab the items that a specific user_id was the first to use. Here is some sample data -
item_id used_user_id date_used
1 1 2012-08-25
1 2 2012-08-26
1 3 2012-08-27
2 2 2012-08-27
3 1 2012-08-27
4 1 2012-08-21
4 3 2012-08-24
5 3 2012-08-23
query
select item_id as inner_item_id, ( select used_user_id
from test
where test.item_id = inner_item_id
order by date_used asc
limit 1 ) as first_to_use_it
from test
where used_user_id = 1
group by item_id
It returns the correct values
inner_item_id first_to_use_it
1 1
3 1
4 1
but the query is VERY slow on a giant table. Is there a certain index that I can use or a better query that I can write?
i can't get exactly what you mean because in your inner query you have sorted it by their used_user_id and and on your outer query you have filtered it also by their userid. Why not do this directly?
SELECT DISTINCT item_id AS inner_item_id,
used_user_id AS first_to_use_it
FROM test
WHERE used_user_id = 1
UPDATE 1
SELECT b.item_id,
b.used_user_id AS first_to_use_it
FROM
(
SELECT item_ID, MIN(date_used) minDate
FROM tableName
GROUP BY item_ID
) a
INNER JOIN tableName b
ON a.item_ID = b.item_ID AND
a.minDate = b.date_used
WHERE b.used_user_id = 1