Insert data to tables [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to insert data and realize order operation from user input data.
I created this tables:
Order
|OrderID - PK|CustomerID - FK|OrderDate|
OrderDetails
|OrderID - PK,FK|ProductID - PK,FK|Quantity|
Customer
|CustomerID - PK|FirstName|LastName|Address|
Product
|ProductID - PK| |SuplierID - FK| |Quantity|
Supplier
|SupplierID - PK|Name|
I want to use stored procedure to insert data, but I've problem with construct t-sql.

if i understand the question correctly, you want to know which order to insert data into tables to ensure no violations, is that correct?
From your post it looks like you insert in this order:
Customer/Product/Supplier (these can all be inserted into in any order).
Order - This can only come after the row is in the customers table as it requires a valid customer
Order Details - this requires an entry in the order table and product table first.
Quantity column would be on the OrderDetail table, as you can have more than one of each line item.

Related

How to merge two or more rows where some columns have same values and some have different values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Sample Data output after joining 2 different tables on ProductNumber
ProductNumber
QuantityOnHand
VendorID
1
5
401
1
5
501
I want to write a query that would return this output
ProductNumber
QuantityOnHand
VendorID
1
10
401 & 501
I'm still pretty new to SQL. Stuck on a homework problem here. I don't really know which aggregated function to use to make it work. Sum() but I don't want to add the vendorIDs. Concat() but they're from the same column name.
If it is SQL Server, then you need STRING_AGG function for cancatenating the same column in different rows and SUM for totalling:
SELECT ProductNumber,
SUM(QuantityOnHand),
STRING_AGG(VendorID, ' & ')
FROM Product
GROUP BY ProductNumber
I assumed the table name is Product, as you did not provide one. You can update it to your actual table name.

Find distinct from multiple columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have 2 columns called price and PID in database called seatinginfo.
Both columns will have multiple of the same values like
pid 1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3
price 10,10,30,40,60,80,70,90,90,90,90 etc
I'm looking to make a query that will give me a list of all unique prices and pick just 1 or the max connected pid.
So example I only want one price,pid like 10,1 30,1 40,1 60,2 etc
Thanks
This is probably as simple as basic aggregation. Something like this should work based on the pretty vague details.
select price
, max(pid)
from YourTable
group by price

Sql instance number in new column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a table with userid in one column, purchase date in second column, and purchase item in the third. I ordered the table by purchase date and want to make a fourth column to record the count instance number of the user. See below for example.
enter image description here
Eventually I want to create a table that shows how much each user bought during the purchase. For example their first purchase they spent 10 second purchase 20 third purchase 30.
What you're looking for is the window function row_number.
select
*,
row_number() over(
partition by userid
order by purchasedate
) as instance
from that_table

POSTGRESQL: Insert Data from another table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
In above table customer ids are listed what I want to do is to get those ids from this table,
create another table named PROMOTION CLASSES where PREMIUM, PLUS AND NORMAL are in columns and put first 100 ids under PREMIUM COLUMN and soon.
Please help.
This creates you table with classification
CREATE TABLE promotion_classes as
SELECT customer_id,
case when customer_id < 100 then
'PREMIUM'
else
'NORMAL'
END as CLASSES
FROM CUSTOMER_TBL

Insert into a backup table based on date [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
My table in Oracle 11g has 6 fields. I also have a backup table also with no values. The original table has 2 date values. I need to insert into the backup table from the original table the orders having an order-date 5 months back as of today.
The table has a field names ORDER-DATE and has records entered inside.
insert into backup_table
select * from original_table
where order-data > add_months(sysdate,-5);