Sql instance number in new column [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 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

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

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

How to add the result of a query to another field by some critiria access [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
The description of the tables are as follow:
Moves: IDPro, IDMove, Quantity, Date
Stock: IDPRo, Quantity, IDstock
Products: IDPro, Quantity, Name, Price
Its a basic inventory access database where every product has a number of movement either positive or negative so the sum of that should be the stock quantity of the specific product. I Want to sum all the moves of each individual item and then update each individual stock quantity field
I dont think you want make an update. Because every time a new move appear you will need another update. That sound more like a trigger, and I belive access doesn't allow that.
You are better creating a VIEW and access a view_Stock instead of the real table
Something Like this:
CREATE VIEW view_Stock (IDPRo, IDstock, Quantity) AS
SELECT S.IDPro, S.IDstock, SUM (M.Quantity)
FROM Stock S
INNER JOIN Moves M
ON S.IDPRo = M.IDPRo
GROUP BY S.IDPro, S.IDstock

Insert data to tables [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
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.