How to add values in sql - sql-server

Hey i have a table like this
Product_name Rate Cost GST_percentage Recipt_no Amount Final_Amount ID Description GST_price Quantity OrderID Discount Net_Unit_Price Stock_Pending Payment_Pending
SINGTEL DATA + EZ $10 1.5 GB 7 DAYS 10 120.00 5 1 120.00 126 1 A 6.00 12 ODR1 0.00 10.00 Received Paid
SINGTEL DATA + EZ $10 1.5 GB 7 DAYS 12 180.00 0 2 180.00 180.00 2 A 0.00 15 ODR2 0.00 12.00 NULL NULL
SINGTEL DATA + EZ $8 CHINA 888 10 120.00 0 2 120.00 120.00 3 B 0.00 12 ODR2 0.00 10.00 NULL NULL
and i want to show the final_Amount column value groupped by order Id.then i want to show the final_amount for those which is Payment_Pending status is not null but i can't get the correct result.
Note:
i got a result as
query:
SELECT [OrderID],
SUM(convert(float,[Final_Amount])) as Final_Amount,
(select sum(convert(float,Final_Amount)) as Final_Amount
from Purchase_Order
where Payment_Pending is not null) as paid
FROM [Purchase_Order]
group by [OrderID]
order by OrderID desc
OrderID Final_Amount paid
ODR2 300 126
ODR1 126 126
but i want like this
OrderID Final_Amount paid
ODR2 300 0
ODR1 126 126
(Because ODR2 Payment_Pending Column filled with null)

Probably your sub-query is wrong. It need to include a reference to OrderId of main query
SELECT [OrderID],
SUM(convert(float,[Final_Amount])) as Final_Amount,
(select sum(convert(float,Final_Amount)) as Final_Amount
from Purchase_Order x
where x.Payment_Pending is not null
and x.OrderId = p.OrderId) as paid
FROM [Purchase_Order] p
group by [OrderID]
order by OrderID desc

Related

Non Aggregate fields in MS SQL

I have MS SQL server table which contains the data as follows
id val1 val2
1 100.00 50.00
2 25.00 30.00
3 30.00 25.00
4 100.00 50.00
5 40.00 80.00
6 25.00 30.00
7 80.00 21.00
8 25.00 30.00
In the above table, few val1 val2 values combination occurs more than ones i.e. 100.00 50.00 is occurred twice, 25.00 30.00 occurred thrice. Likewise, if any combinations occurred more than ones, I would need to get those Ids.
So my result would be id - 1,2,4,6,8.
please help how to query this in MS SQL. Thanks
The CTE obtains the val1, val2 pairs that occur more than once. The we join with the table to get the id values:
;with cte as (
select val1, val2
from t
group by val1, val2
having count(*) > 1
)
select id
from t
join cte on t.val1 = cte.val1 and t.val2 = cte.val2

SQL Server Table Column from data

I'm trying to achieve sort of solution to the below table, as I need to make one of the rows in column as column
sku title name product_qty ID warehouse_id
-----------------------------------------------------
S001 Title 1 warehouse1 5 23 1
S001 Title 1 warehouse2 95 23 2
S001 Title 1 warehouse3 3 23 3
S002 Title 2 warehouse1 1 24 1
S002 Title 2 warehouse2 91 24 2
S002 Title 2 warehouse3 0 25 3
and what I would like to achieve
sku title name
warehouse 1 warehouse 2 warehouse 3
S001 Title 1 5 95 3
S002 Title 2 1 91 0
Please try this ... solution...You need to pivot the data.
CREATE TABLE Pivots
(
sku VARCHAR(10)
,title VARCHAR(10)
,name VARCHAR(10)
,product_qty INT
,ID INT
,warehouse_id INT
)
GO
INSERT INTO Pivots VALUES
('S001','Title1','warehouse1',5 , 23 , 1),
('S001','Title1','warehouse2',95 , 23 , 2),
('S001','Title1','warehouse3',3 , 23 , 3),
('S002','Title2','warehouse1',1 , 24 , 1),
('S002','Title2','warehouse2',91 , 24 , 2),
('S002','Title2','warehouse3',0 , 25 , 3)
GO
SOLUTION
SELECT sku,title titlename,ISNULL(MAX(x.warehouse1),0) warehouse1
,ISNULL(MAX(x.warehouse2),0) warehouse2 ,ISNULL(MAX(x.warehouse3),0) warehouse3 FROM Pivots p
PIVOT
(
MAX(product_qty) FOR name IN ([warehouse1],[warehouse2],[warehouse3])
)x
GROUP BY sku,title
OUTPUT
sku titlename warehouse1 warehouse2 warehouse3
---------- ---------- ----------- ----------- -----------
S001 Title1 5 95 3
S002 Title2 1 91 0
(2 rows affected)
SOLUTION 2 - USING conditional aggregation
SELECT sku,title titlename
,ISNULL(MAX(CASE WHEN name = 'warehouse1' THEN product_qty END),0) warehouse1
,ISNULL(MAX(CASE WHEN name = 'warehouse2' THEN product_qty END),0) warehouse2
,ISNULL(MAX(CASE WHEN name = 'warehouse3' THEN product_qty END),0) warehouse3
FROM Pivots p
GROUP BY sku,title
OUTPUT
sku titlename warehouse1 warehouse2 warehouse3
---------- ---------- ----------- ----------- -----------
S001 Title1 5 95 3
S002 Title2 1 91 0
(2 rows affected)

Sql join and pivot to get category totals

I have these tables.
Main table
[key] [CategoryID]
AAAA 100
BBBB 100
CCCC 101
DDDD 102
EEEE 201
FFFF 202
GGGG 202
etc.
Category lookup
[CategoryID] [Category] [Subcategory]
100 Category1 Subcategory1
101 Category1 Subcategory2
102 Category1 Subcategory3
103 Category1 Subcategory4
200 Category2 SubcategoryA
201 Category2 SubcategoryB
202 Category2 SubcategoryC
etc.
Status lookup
[StatusID] [Description]
0 New
500 Accepted
501 Rejected
Status history
[key] [StatusID] [date]
AAAA 0 2017-01-01
BBBB 0 2017-01-01
CCCC 0 2017-01-01
DDDD 0 2017-01-01
EEEE 0 2017-01-01
FFFF 0 2017-01-01
GGGG 0 2017-01-01
AAAA 500 2017-01-02
BBBB 501 2017-01-02
EEEE 501 2017-01-02
FFFF 500 2017-01-02
BBBB 500 2017-01-03
EEEE 500 2017-01-03
etc.
I'd like to get a monthly summary of the results (which is emailed out). Right now I'm building the summary table in the code. I'd like to learn how to do it in SQL, but I don't really know where to start.
Final table where the totals are based on the last [Status History] value and ordered by [Total] DESC.
[Category] [Subcategory] [New] [Accepted] [Rejected] [Total]
Category1 Subcategory1 13 8 2 23
Category2 Subcategory3 10 4 6 20
Category1 Subcategory2 5 8 4 17
I've tried to look at PIVOT to do this, but I don't understand how to do it with the joins and with getting only the last status history value.
I think this is what you're after. I recreated your data and tested it out, but I definitely had to make one or two assumptions about the relationships involved. Try it and let me know:
select Category,
Subcategory,
sum(case when description = 'New' then 1 else 0 end) as New,
sum(case when description = 'Accepted' then 1 else 0 end) as Accepted,
sum(case when description = 'Rejected' then 1 else 0 end) as Rejected,
count(*) as Total
from
(select tB.Category, tB.Subcategory, status_lookup.Description
from status_history inner join (select my_key, max(status_date) as lastdate
from status_history
group by my_key) tA on status_history.my_key = tA.my_key
and status_history.status_date = tA.lastdate
inner join status_lookup on status_history.StatusID = status_lookup.StatusID
inner join (select main.my_key, category_lookup.Category, category_lookup.Subcategory
from main inner join category_lookup on main.CategoryID = category_lookup.CategoryID) tB on status_history.my_key = tB.my_key) tC
group by Category, Subcategory
I renamed your [key] column my_key, and called the tables main, category_lookup, status_lookup, and status_history respectively.

How to remove duplicates in SQL Server

I have data looks like this:
Order No. Name Date Unit Price Freight
001 ABC 1-16 232 25
001 ABC 1-16 55 25
001 ABC 1-16 156 25
002 DEF 2-5 478 16
002 DEF 2-5 356 16
I am trying to let freight cost only show once in my table, the result would look like:
Order No. Name Date Unit Price Freight
001 ABC 1-16 232 25
001 ABC 1-16 55 0
001 ABC 1-16 156 0
002 DEF 2-5 478 16
002 DEF 2-5 356 0
Please help me with this
Here is a query to get what you want:
SELECT
order_no, name, theDate, unit_price,
case
when row_number() OVER (PARTITION by order_no ORDER BY order_no) = 1 then freight
else 0
end as freight
from yourTable
This looks at all rows for each order number and provides the row number. If it's row 1 of that order it uses the values of the freight column, otherwise it uses 0.
Note that I'm assuming that the freight value is the same across all rows for the same order number.

pivot table in TSQL

Here is the dataset from the table
TABLE XYZ
ID Date col1 col2 col3
1 2012-09-21 100.00 0.00 0.00
1 2012-09-21 0.00 10.00 20.00
2 2012-09-21 0.00 20.00 0.00
2 2012-09-21 100.00 0.00 20.00
3 2012-09-21 10.00 25.00 5.00
I want Result like below:
ID Date COL
1 2012-09-21 130.00
2 2012-09-21 140.00
3 2012-09-21 40.00
I tried below Query, but not getting a exact value from table.
Select ID,
date,
sum(Col1+col2+col3) as COL
From table xyz
Group by ID,DATE
I got below result set
ID Date COL
1 2012-09-21 130.00
2 2012-09-21 270.00
3 2012-09-21 310.00
IS there any better way to get expected result set or to do a pivot table?
Thanks
You can use CTE and then use group by on the result.
WITH S AS ( SELECT ID,DATE,COL1+COL2+COL3 AS. COLS FROM TABLE1);
SELECT ID,DATE,SUM(COLS)
FROM S
GROUP BY ID,DATE

Resources