Advanced SQL Update query - sql

I'm trying to create a sql update query for a stored procedure but I'm getting lost in trying to figure out how to to do it.
Here is what I'm trying do:
The table I'm wanting to update has 3 columns: product_item_id, rel_product_item_id, and sequence. This table is called "ProductRelationship".
I also have another table called ProductDetails. This table also contains a column called product_item_id as well as a column called "sku".
I want the user to be able input the sku number that updates the rel_product_item_id column with the corresponding product_item_id number from the ProductDetails table.
So on the front-end, the user is inputting a sku, but in the backend, the product_item_id number is getting updated in the database based on the sku the user entered.
Basically its cross-referencing the sku number on the details table, finding the appropiate product item id number and inserting that id number in the table instead of the user-inputted sku number.

update ProductRelationship
set rel_product_item_id = (select product_item_id
from ProductDetails
where sku = #UserInput)
where product_item_id = #ValueSetOnPage

Related

Database design for editable table

I have to design an application which will store products with default values, in a table called default_products .
This application will be used by customers (or users) who should be able to use the products from the default_products table, with their default value, but they should also be able to :
edit and customize the default values of such product records for
their own personal use - without affecting the product records of
the other customers.
add their own products, but these products should not be visible to other users.
One solution I could imagine would be:
one default_products table containing all the default products with their default values
one custom_products table with all the products crated by the users, and also a copy of the default product the user would have customized,
and a boolean field like is_update to indicate if it is an update
from a default_product record
A function which looks first for the updated product records from the custom_product table, then queries the default_products table and
returns records from the default_product table + only the updated and
custom products for that user from the custom_product table.
Example:
default_product
- id
- name
- price
custom_product
- default_product.ID
- custom_price
I want the query to return: default_product.ID, default_product.name, custom_product.price only if there is a record in custom_product corresponding to the product in default_prduct, otherwise return: default_product.ID, default_product.name, default_product.price.
Any idea if this is any good? Or does anybody have any easier way to do?
How you approach this depends on whether the user has a copy of the default, or an alias to the default. That is, once a user has selected a product, do they see updates to the name and price of the default?
If it's an alias, you could have a products table to store products, and a user_products table to store which products the user has selected and any customization.
products:
id
name not null
price not null
user_products:
id
user_id not null
product_id
name
price
user_products.name and user_products.price are allowed to be null. If null, they will use the value from products. You do this using coalesce which will pick the first non-null value.
If a user wants to add their own product, user_products.product_id is null. Do a left join to ensure a result.
select
coalesce(up.name, p.name) as name,
coalesce(up.price, p.price) as price
from user_products up
left join products p on p.id = up.product_id
where user_id = ?
The disadvantage is the two tables have to be maintained in sync, and queries have to be written to coalesce each field. This might be a use case for table inheritance, if your database supports it.
If it's a copy, have a single table with a user_id column which can be null.
products:
id
user_id
name not null
price not null
When products.user_id is null these are the defaults available to anyone.
When a user selects a product, its values are copied to a new row with that user_id. Once a product is selected by a user they do not see future changes to the default. This may or may not be desirable.
You can optionally include an original_product_id column to remember which default product the user copied from.
The advantage is only one table needs to be maintained and the queries are simple.

SQL Query to get distinct results from one table and match those results to the another column in the table

I have an SQL table with product names in one column and a product category in another column. So each product belongs to a specific category. I am trying to figure out an sql command that will return distinct values from the product name column but will also display the product category each product belongs to. Any ideas?
You can try like this,
SELECT Distinct([ProductName]),[ProductCategeory] FROM [DB].[dbo].[tblProduct]

SQL Trigger On Different Tables

I have a Database that looks like this.
Whenever a new user is created, the Id of the User(UserName) needs to be stored in the Budget table. After this, I want the attribute category in the Budget table to be filled with every category in the Category table.
My current Category table:
As you can see, I currently have 3 categories. Is it possible to create a Trigger which loops trough the categories, and inserts the Category.Id into Budget.Category.
The Budget table should look like this after a user has been created:
I don't know how to add each categoryId to the Budget table.
To insert the values into the budget table, the code would look something like this:
insert into budget(UserId, CategoryId, Date, Value)
select new.userid, c.categoryId, current date, 0
from categories c;
No foreach is required, but the syntax varies by database. In particular:
The syntax for the current date.
The syntax for accessing the values being inserted.

How to update multiple rows from values of first row in same table in Postgresql?

I have a table in the low code application Quickbase. Once I import data from the spreadsheet which populates the Customer Name, Invoice Num, Invoice Date and Invoice Amount columns. Then, I manually data enter the client ID & Customer ID in only the first row. I am planning to add a psql query in a separate table which interacts with Postgres database of the application. This trigger should update the other rows with the values of Client ID & Customer ID from first row. I am not sure how to move forward with this. If a Join statement is required?
Update table set clientid=(select top clientid from table where clientid is not null)
Update table set clientname=(select top clientname from table where clientname is not null)

Updating one SQL table based on data in another table

I am running Microsoft SQL Server 2008 R2, and pulling information from two tables to create one new table.
Table A has leads with a unique lead number and other information.
Table B has sales with a unique sales number, and the lead number associated with it.
Data from both tables are pulled into temp tables in SQL Server so I can change and update whatever I need, and the output of this will go into a new table.
One lead from Table A can have multiple sales associated with it in table B.
I want to update the Number of Sales column in Table A (Leads) based on how many times that lead number appears in Table B (sales). So if Table B (sales) has a lead number tied to seven (7) sales, the Number of Sales column in Table A (leads) will be updated to 7.
I have tried a few variations using the COUNT function but with no success. Any help would be appreciated.
This should work for you assuming the field name is leadNo:
update tablea
set sales = (select count(*)
from tableb
where tableb.leadNo = tablea.leadNo)
SQL Fiddle Demo