TSQL Price Pivot Table - sql

Im after some assistance on how to pivot the following data. I have a pricing table like the following example.a
It contains products with varying pricing information on Qty Breaks. What I need to do is take this data and pivot into the table. The database is hosted in MSSQL-2012
example.a
ProductId Qty Price Reference
---------+---+-----+-------------
3 1 18.92
3 1 4.65
3 24 3.72
3 72 3.45
3 1 3.40 Quote:11223344
---------+---+-----+-------------
Additionally:
When pivoting the table, if there are products with the same Qty, pick the lowest price. As Shown above, ProductID 3 has 3x rows of pricing where they have the same Qty. So the pivot must only select and show the lowest rate
Required Pivot Result
ProductId Qty1 Price1 Reference1 Qty2 Price2 Reference2 Qty2 Price2 Reference2
---------+----+------+---------------+----+------+--------------+----+------+-------------
3 1 3.4 Quote:11223344 24 3.72 72 3.45
---------+----+------+---------------+----+------+--------------+----+------+-------------
Really would appreciate some ideas on who to tackle such a Query
thanks

Related

SQL Sum with conditions in two columns

I'm very new to SQL and VB.NET. I have an existing table called STOCK with the columns shown here, and I want to sum buy and sell to display current quantity.
Existing table:
ID
Date
BUY
SELL
Current quantity
1
01/01/22
88
0
2
03/01/22
22
0
94669
05/02/22
0
30
I want to display in Current quantity like this
(the current quantity amount in the row above + BUY - SELL)
I add result in Current quantity manually, but I want to do this in automatic way it is possible in SQL code
ID
Date
BUY
SELL
Current quantity
1
01/01/22
88
0
88
2
03/01/22
22
0
110
3
05/02/22
0
30
80
You can try this:
select a.*,
sum(net_sell) over (order by Curr_date ) as Current_quantity
from
(select s.*,
buy-sell as net_sell
from stock s) a ;
Dbfiddle link : https://dbfiddle.uk/?rdbms=postgres_11&fiddle=196a41a578d1e699ccaa3e878e261019

How to add a query to a table in SQL?

I have 3 tables.
For simplicity I changed them to these sample tables.
table1: CorporateActionSmmary
RATE Quantity ProductID
--------------------------
56 0 1487
30 0 1871
40 0 8750
table2# ProductMaster
RATEGROSS ISIN ProductID
--------------------------
60 JP0001 1487
33 JP0002 1871
45 JP0003 8750
table3# OpenPosition
Quantity ProductID
-------------------
5 1487
1 1487
5 1487
3 1871
2 1871
4 8750
2 8750
7 8750
3 8750
First I need to add ISIN from table2 to table1
table1: CorporateActionSmmary
RATE Quantity ProductID ISIN
-------------------------------------
56 0 1487 JP0001
30 0 1871 JP0002
40 0 8750 JP0003
So, I used this code
SELECT [dbo].[CorporateActionSummary].*, [dbo].[ProductMaster].[ISIN]
FROM [dbo].[CorporateActionSummary] JOIN [dbo].[ProductMaster] ON CorporateActionSummary.ProductID = ProductMaster.ProductID
Now as you can see the Quantity is missing in Table1 so I have to add-up all the quantities in Table3 for each product ID and add to Table1(as a new column or over-write the Quntity column)
I think I can get the sum of each ProductID's Quantity by the following code, But how can I add it to Table1 that already has ISIN column
SELECT SUM(Qantity),ProductID
FROM [dbo].[OpenPositions]
I am super new to SQL, please explain in detail if it is possible, thank you
I am using Microsoft SQL Server Management Studio
you can sum the quantities and then join with your query like so:
SELECT CA.*, PM.[ISIN],CA.Quantity
FROM [dbo].[CorporateActionSummary] CA
JOIN [dbo].[ProductMaster] PM
ON CA.ProductID = PM.ProductID
JOIN (
SELECT ProductID, SUM(Qantity) Quantity
FROM [dbo].[OpenPositions]
GROUP BY ProductID
) OO
on OO.ProductID = CA.ProductID
you are almost there.. you just need to use the same logic to join to the product master table. However, since you need the total of quantity, you need to group by the other columns you select (but not aggregate).
The query will be something like this :
SELECT
[dbo].[CorporateActionSummary].ProductID
, [dbo].[ProductMaster].[ISIN]
,sum([OpenPosition].Quantity) as quantity
FROM [dbo].[CorporateActionSummary]
JOIN [dbo].[ProductMaster]
ON CorporateActionSummary.ProductID = ProductMaster.ProductID
JOIN [dbo].[OpenPosition]
ON CorporateActionSummary.ProductID = OpenPosition.ProductID
group by
[dbo].[CorporateActionSummary].ProductID
, [dbo].[ProductMaster].[ISIN]
if you want to add more columns to your select, then you need to group by those colums as well

Count all of a column where value is 2 and sum this value with price

I'm doing with Northwind database where I use the Products table. I need to count all of the rows where Category_Id is 2 and sum the amount with the prices.
Here's the example of a table shortly:
Category_ID | Unit Price
1 | 2,90
2 | 3,70
3 | 4,90
2 | 1,90
5 | 0,90
2 | 2,90
There are 3 rows where category_Id is 2. How to sum this 3 with that rows Unit price?
3,70 + 1,90 + 2,90 = 8,50
So the answer I need is 8,50 but I have no idea how to get that amount with a SQL query.
Does someone know?
you can get the aggregated values for all Ids using
Select Categeory_Id, sum([Unit Price]) Total, count(*) Qty
from Products
group by Category_Id
or just a specific total such as
select sum([Unit Price]) total
from products
where category_Id=2

How to calculate product prices cheaper than current row

Need help with an SQL query.
I have a products table... Fields are...
product_id
product_name
product_price
I want to show all products and have an additional column that shows how many products are cheaper than the current product. So, for example....
product_id product_name product_price Products_cheaper
1 product 1 1.50 0
2 product 2 6.50 2
3 product 3 2.50 1
4 product 4 10.50 3
Any suggestions on this SQL query?
Using SQL Server. Also, if products are equal then it's 0.
SELECT product_id,
product_name,
product_price,
RANK() OVER (ORDER BY product_price) - 1 AS product_cheaper
FROM products
ORDER BY product_id
Result (I added the sample I used on my comment)
product_id product_name product_price product_cheaper
1 product 1 1.50 0
2 product 2 6.50 2
3 product 3 2.50 1
4 product 4 10.50 4
5 product 5 6.50 2

Add a New Column in Table For Order by

In my project, I have 12 tables. Let take 2 of them as sample:
SaleOrder PurchaseOrder
Date SaleOrdId Qty UnitPrice Amount || Date PurchOrdId Qty UnitPrice Amount
4/2/12 S_1 2 600 1200 6/2/12 P_1 50 500 25000
7/2/12 S_2 5 600 3000
SaleordId and Purchordid are the primary keys.
From these, I am Generating a Product Inquiry Report like:
Product Inquiry Report
Date TranId QtyPurchased QtySold Balance
4/2/12 S_1 2 20 (previous)
6/2/12 P_1 50 70
7/2/12 S_2 5 65
For Now,When i Select data from both the tables by union, I use Order by 'Date'. But In practice I think it may cause problems if there is some problem with system Date and time. So what i have decided is to add an extra Column 'OrdCol' to both the tables and maintain a separate table for that and keep it primary key there like:
SaleOrder PurchaseORder OrderTbl
Date OrdCol ----- Date OrdCol ----- OrdCol
1 2 1
3 2
3
Is my idea correct in terms of DB?
Is Not, then any other idea?
I cant apply order by 'Primary keys' of the tables(SO and PO) as they are in nvarchar and there numeric
part can be same for both.