CockroachDB INNER JOIN UPDATE not working - sql

I am trying to implement CockroachDB INNER JOIN update, however it says following
HINT: You have attempted to use a feature that is not yet implemented.
Any thoughts?
The query is below:
UPDATE products
SET products.prodname = 'Hello'
FROM products
INNER JOIN prodcategory
ON products.categoryid = prodcategory.prid
WHERE products.productid = '9680ead9-57f6-441d-af5f-a384a66d3300';

Try:
UPDATE products
SET products.prodname = 'Hello'
WHERE products.categoryid IN (
SELECT prodcategory.prid
FROM prodcategory
)
AND products.productid = '9680ead9-57f6-441d-af5f-a384a66d3300';

Related

Oracle (Netsuite) SQL one join limit results

I have an oracle SQL query and a slight problem. I need to check if an item has a PO# that it has at least 1 line item. The query below works however it returns a result for each line of transaction_lines and I need only une result. PS I tried DISTINCT but get an ODBC error.
SELECT ITEMS.NAME, INVENTORY_NUMBER.INVENTORY_NUMBER, INVENTORY_NUMBER.ON_HAND_COUNT, ITEMS.SALESDESCRIPTION, CONDITION.LIST_ITEM_NAME,
BRAND_PARTNER.LIST_ITEM_NAME, PPROGRAM.LIST_ITEM_NAME, ENTITY.NAME, PO.TRANSACTION_NUMBER, INVENTORY_NUMBER.RECEIVED_COST, ITEMS.SALESPRICE, IR.TRANSACTION_NUMBER,
INVENTORY_SOURCE.LIST_ITEM_NAME, LOCATIONS.NAME, INVENTORY_NUMBER.RECEIPT_DATE, PO.INTERNAL_MEMO, INVENTORY_NUMBER.REFERENCE_, TEST_RESULTS.LIST_ITEM_NAME,
INVENTORY_NUMBER.TEST_FILE_LINK, INVENTORY_NUMBER.CONNECT_TRADE_ID, INVENTORY_NUMBER.SOLD_DATE, INVENTORY_NUMBER.SOLD_PRICE, INVENTORY_NUMBER.MEMO, ITEMS.UPC_CODE, ITEMS.MPN,
ITEMS.ITEM_ID, INVENTORY_NUMBER.CLEI, INVENTORY_NUMBER.CERTIFICATION_REF_ID
FROM INVENTORY_NUMBER
INNER JOIN ITEMS ON INVENTORY_NUMBER.ITEM_ID = ITEMS.ITEM_ID
INNER JOIN TRANSACTIONS AS PO ON INVENTORY_NUMBER.PURCHASE_ORDER_ID = PO.TRANSACTION_ID
INNER JOIN TRANSACTIONS AS IR ON INVENTORY_NUMBER.ITEM_RECEIPT_ID = IR.TRANSACTION_ID
INNER JOIN TRANSACTION_LINES ON PO.TRANSACTION_ID = TRANSACTION_LINES.TRANSACTION_ID
INNER JOIN ENTITY ON TRANSACTIONS.ENTITY_ID = ENTITY.ENTITY_ID
INNER JOIN CONDITION ON INVENTORY_NUMBER.CONDITION_ID = CONDITION.LIST_ID
INNER JOIN BRAND_PARTNER ON INVENTORY_NUMBER.BRAND_PARTNER_ID = BRAND_PARTNER.LIST_ID
INNER JOIN PPROGRAM ON INVENTORY_NUMBER.PROGRAM_ID = PPROGRAM.LIST_ID
INNER JOIN INVENTORY_SOURCE ON INVENTORY_NUMBER.INVENTORY_SOURCE_ID = INVENTORY_SOURCE.LIST_ID
INNER JOIN LOCATIONS ON INVENTORY_NUMBER.LOCATION_ID = LOCATIONS.LOCATION_ID
INNER JOIN TEST_RESULTS ON INVENTORY_NUMBER.TEST_RESULTS_ID = TEST_RESULTS.LIST_ID
WHERE INVENTORY_NUMBER.ON_HAND_COUNT IS NOT NULL AND ((INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NULL) OR (INVENTORY_NUMBER.PURCHASE_ORDER_ID IS NOT NULL AND TRANSACTION_LINES.TRANSACTION_LINE_ID IS NOT NULL))
you could also remove the join to the transaction_lines and instead of tl.TRANSACTION_LINE_ID IS NOT NULL use an exists clause
and exists (select 1 from transaction lines tl
where tl.transaction_id = po.transaction_id)
I would suggest using a GROUP BY to help limit your results. You could also if you are interacting with transactions in your query you must always remember that without limiting results based on the "main line" you will receive the header record and then a record for each individual line item.
If you were doing this with a saved search you could put the criteria as "main line = true". Since I don't understand your query entirely I can't advise where to put this limitation in.

SELECT from SELECT in SQL

I'm working in small food depot where we preparing deliveries for small shops. We using WMS to get our work done. For some reasons i need to get some data from database using SQL builder in system. So the case is.
To get information to which store particular product has been despatched I'm using this query:
select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
inner join item_master im on im.sku_id=cd.sku_id
INNER JOIN pkt_hdr pk ON pk.pkt_ctrl_nbr=cd.pkt_ctrl_nbr
where im.barcode = '105683004'
and cd.open_cage_batch = 31102014
To see list of stores and quantities of product allocated by our customer i'm using this query:
select sd.to_store, sd.alloc_qty, sd.alloc_batch, im.size_desc
from store_distro sd
inner join item_master im on im.sku_id=sd.sku_id
where im.barcode = '105683004'
and sd.alloc_batch = 31102014
But i need a SQL to show me any stores to which product has not been despatched.
I will appreaciate any help
Thanks
You can probably try doing a LEFT JOIN for your first query and check for NULL. Something like
select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
left join item_master im
on im.sku_id = cd.sku_id and im.barcode = '105683004'
LEFT JOIN pkt_hdr pk
ON pk.pkt_ctrl_nbr = cd.pkt_ctrl_nbr
where cd.open_cage_batch = 31102014
and pk.to_store IS NULL
I learned how to SELECT FROM (SELECT's) here:
SQL SELECT Union SELECT FROM (Select...)
In this case, it would be something like...
SELECT (columns)
FROM
(select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
inner join item_master im on im.sku_id=cd.sku_id
INNER JOIN pkt_hdr pk ON pk.pkt_ctrl_nbr=cd.pkt_ctrl_nbr
where im.barcode = '105683004'
and cd.open_cage_batch = 31102014
) AS dispatched
JOIN
(select sd.to_store, sd.alloc_qty, sd.alloc_batch, im.size_desc
from store_distro sd
inner join item_master im on im.sku_id=sd.sku_id
where im.barcode = '105683004'
and sd.alloc_batch = 31102014
) AS allocated
ON (stuff)

How to turn this big query into a stored procedure

How can I turn this big query into a stored procedure and should I? What would the benefit be?
SELECT *
FROM user_items
LEFT JOIN items ON (items.item_id = user_items.item_id)
INNER JOIN item_categories ON (item_categories.item_id = items.item_id)
INNER JOIN item_subcategories ON (item_subcategories.item_id = items.item_id)
INNER JOIN brands ON (brands.brand_id = items.item_brand)
INNER JOIN item_photos ON (item_photos.item_id = items.item_id)
INNER JOIN place_items ON (place_items.item_id = items.item_id)
INNER JOIN places ON (places.place_id = place_items.place_id)
WHERE user_items.user_id = :user_id
from the brands table I only need the brand_name
from the places table I only need the place_name
The way I'm doing it right now, I'm getting all columns from brands and places, so a friend of mine told me I should probably consider using stored procedures
If you want columns from brands and items tables only, you can do like below
"SELECT brands.brand_name,places.place_name,
user_items.*,items.*,item_categories .*,
item_subcategories.*,item_photos.*,place_items.*
FROM user_items
LEFT JOIN items ON (items.item_id = user_items.item_id)
INNER JOIN item_categories ON (item_categories.item_id = items.item_id)
INNER JOIN item_subcategories ON (item_subcategories.item_id = items.item_id)
INNER JOIN brands ON (brands.brand_id = items.item_brand)
INNER JOIN item_photos ON (item_photos.item_id = items.item_id)
INNER JOIN place_items ON (place_items.item_id = items.item_id)
INNER JOIN places ON (places.place_id = place_items.place_id)
WHERE user_items.user_id = :user_id"
The use of stored procedure is to reuse a set of SQL statements . The performance of stored procedure would be as good as the SQL statements it contains.
A better approach for better readability of your code is to use ALIASES for table names.
When to use SQL Table Alias
In my experience, stored procedures have been more trouble than they're worth, being inflexible and therefore more difficult to maintain than inline SQL, residing outside version control, and failing to provide much if any performance benefit. And in this case a stored routine doesn't seem necessary or beneficial, because your query doesn't demand an advanced feature, such as a cursor. For another discussion of advantages and disadvantages, see this post.
DELIMITER $$
DROP PROCEDURE IF EXISTS `ABC` $$
CREATE PROCEDURE `ABC`(IN UID LONG)
READS SQL DATA
BEGIN
SELECT *
FROM user_items
LEFT JOIN items ON (items.item_id = user_items.item_id)
INNER JOIN item_categories ON (item_categories.item_id = items.item_id)
INNER JOIN item_subcategories ON (item_subcategories.item_id = items.item_id)
INNER JOIN brands ON (brands.brand_id = items.item_brand)
INNER JOIN item_photos ON (item_photos.item_id = items.item_id)
INNER JOIN place_items ON (place_items.item_id = items.item_id)
INNER JOIN places ON (places.place_id = place_items.place_id)
WHERE user_items.user_id = UID
END $$
DELIMITER ;
you can do like this and execute this and can call then :-
CALL ABC(1234);
where 1234 is user_id of user. Thank you

SQL Update with Subquery as Data source doesn´t work (Postgresql)

i have the following SQL DML Update command, but the syntax isn´t correct and the command doesn´t work:
UPDATE hmsg_vehicle_category
SET hmsg_vehicle_category.hmsg_id, hmsg_vehicle_category.vehiclecategories_inputname
SELECT l_p.hmsg_id, tmp_p_vc.inputname
FROM hmsg_him_product AS l_p INNER JOIN ( SELECT p.id, vc.inputname
FROM him_product p INNER JOIN vehicle_category vc
ON p.id = vc.product
ORDER BY p.id, vc.inputname DESC ) AS tmp_p_vc
ON l_p.products_id = tmp_p_vc.id
WHERE l_p.hmsg_id = 171;
How can i execute this SQL command? Where is the mistake in the snytax?
Thanks for help !
Greetz
Marwief
Something like:
update hmsg_vehicle_category set
hmsg_id = l_p.hmsg_id,
vehiclecategories_inputname = tmp_p_vc.inputname
from hmsg_him_product as l_p
inner join him_product as p on p.id = l_p.products_id
inner join vehicle_category as vc on vc.product = p.id
where l_p.hmsg_id = 171
Be warned, this one will update all records in hmsg_vehicle_category table. May be you want to add this into where clause:
update hmsg_vehicle_category as hvc set
vehiclecategories_inputname = tmp_p_vc.inputname
from hmsg_him_product as l_p
inner join him_product as p on p.id = l_p.products_id
inner join vehicle_category as vc on vc.product = p.id
where
l_p.hmsg_id = 171 and hvc.hmsg_id = 171
But I cannot advice something more specific at the moment, because it's unclear from your question.

Ambiguous outer join in MS Access

Trying to create an outer join on two other joined tables when recieving this error - I just dont see how to create two separate queries to make it work. Subqueries don't seem to work either, any help appreciated. I get errors for the below query, thanks.
SELECT
CardHeader.CardID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin, CardDetail.ItemID, Items.ItemDescription,
Items.VCatalogID, CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost, CardColors.ColorID
FROM
((Items
INNER JOIN
(CardHeader INNER JOIN CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID)
LEFT JOIN
CardColors ON CardDetail.ItemID = CardColors.ItemID)
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID
ORDER BY
CardHeader.CardID;
I tried the following which runs but asks for the following parameters (which it shouldnt)
CardHeader.ID, MainQry.CardID
SELECT
MainQry.ID, MainQry.CardDescription, MainQry.GloveSize,
MainQry.GloveDescription, MainQry.Bin, MainQry.ItemID,
MainQry.ItemDescription, MainQry.VCatalogID, MainQry.ChargeCode,
MainQry.Quantity, MainQry.Cost, SubQry.ColorID
FROM
(SELECT
CardHeader.ID, CardHeader.CardDescription, CardHeader.GloveSize,
CardHeader.GloveDescription, CardDetail.Bin,
CardDetail.ItemID, Items.ItemDescription, Items.VCatalogID,
CardDetail.ChargeCode, CardDetail.Quantity, Items.Cost
FROM
Items
INNER JOIN
(CardHeader
INNER JOIN
CardDetail ON CardHeader.CardID = CardDetail.CardID) ON Items.ItemID = CardDetail.ItemID
) AS MainQry
LEFT JOIN
(SELECT
CardColors.ItemID, CardColors.ColorID
FROM
CardColors
INNER JOIN
Colors ON CardColors.ColorID = Colors.ID) AS SubQry ON MainQry.ItemID = SubQry.ItemID
ORDER BY
MainQry.CardID;
The second SQL statement can be corrected by reference to the first statement and the error. The error is that both CardHeader.ID and MainQry.CardID are prompting for a parameter, which indicates that the inner statement should include CardHeader.CardID, rather than CardHeader.ID