pivot table in TSQL - sql-server

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

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

How to add values in sql

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

Pivot the data in SQL Server

I have the following table data
ID Night_111 NightOT_112 NightLeave_113
----------------------------------------------------
1 8.00 0.00 0.00
2 16.00 4.00 0.00
3 8.00 0.00 2.00
I want that table to convert it into this structure:
ID wageType hours
---------------------------
1 111 8
2 111 16
2 112 4
3 111 8
3 113 2
Please help
Thanks
There are several ways, but I advocate using apply:
select v.*
from t cross apply
(values (id, 111, Night_111), (id, 112, NightOT_112), (id, 113, NightOT_112)
) v(ID, wageType, hours)
where v.hours > 0;
You can do something similar using union all or unpivot. However, apply implements something called a lateral join, which is a very powerful operator and useful many other situations.

MSSQL - split records per week_start and week_end

I have a table similar to the one represented below.
myID | some data | start_date | end_date
1 Tom 2016-01-01 2016-05-09
2 Mike 2015-03-01 2017-03-09
...
I have a function that when provided with start_date, end_date, interval (for example weeks)
returns me data as below. (splits the start and end dates to week intervals)
select * from my_function('2016-01-01','2016-01-12', 'ww')
2015-12-28 00:00:00.000 | 2016-01-03 00:00:00.000 15W53
2016-01-04 00:00:00.000 | 2016-01-10 00:00:00.000 16W1
2016-01-11 00:00:00.000 | 2016-01-17 00:00:00.000 16W2
I would like to be able to write a query that returns all of the values from the 1 table, but splits Start date and end date in to multiple rows using the function.
myID | some data | Week_start_date | Week_end_date | (optional)week_num
1 Tom 2015-12-28 2016-01-03 15W53
1 Tom 2016-01-04 2016-01-10 16W1
1 Tom 2016-01-11 2016-01-17 16W2
...
2 Mike etc....
Could someone please help me with creating such a query ?
select myID,some_data,b.Week_start_date,b.Week_end_date,b.(optional)week_num from #a cross apply
(select * from my_function('2016-01-01','2016-01-12', 'ww'))b
like sample data i tried
create table #a
(
myID int, some_data varchar(50) , start_date date, end_date date)
insert into #a values
(1,'Tom','2016-01-01','2016-05-09'),
(2,'Mike','2015-03-01','2017-03-09')
here iam keeping function result into one temp table
create table #b
(
a datetime,b datetime, c varchar(50)
)
insert into #b values
('2015-12-28 00:00:00.000','2016-01-03 00:00:00.000','15W53'),
('2016-01-04 00:00:00.000','2016-01-10 00:00:00.000','16W1 '),
('2016-01-11 00:00:00.000','2016-01-17 00:00:00.000','16W2 ')
select myID,some_data,b.a,b.b,b.c from #a cross apply
(select * from #b)b
output like this
myID some_data a b c
1 Tom 2015-12-28 00:00:00.000 2016-01-03 00:00:00.000 15W53
1 Tom 2016-01-04 00:00:00.000 2016-01-10 00:00:00.000 16W1
1 Tom 2016-01-11 00:00:00.000 2016-01-17 00:00:00.000 16W2
2 Mike 2015-12-28 00:00:00.000 2016-01-03 00:00:00.000 15W53
2 Mike 2016-01-04 00:00:00.000 2016-01-10 00:00:00.000 16W1
2 Mike 2016-01-11 00:00:00.000 2016-01-17 00:00:00.000 16W2
Based on your current result and expected result,the only difference ,i see is myID
so you will need to frame your query like this..
;with cte
as
(
select * from my_function('2016-01-01','2016-01-12', 'ww')
)
select dense_rank() over (order by somedata) as col,
* from cte
Dense Rank assigns same values for the same partition and assigs the sequential value to next partition ,unlike Rank
Look here for more info:
https://stackoverflow.com/a/7747342/2975396

Finding common value in two columns

I have a problem with a query where i need to get all the common values in column #2 that are for every element of column #1.
For example:
Column #1 Column #2
-------------------
21 2.00
21 5.00
21 6.00
21 8.00
21 9.00
41 2.00
41 3.00
41 4.00
41 5.00
41 6.00
41 9.00
52 2.00
52 5.00
52 9.00
52 10.00
52 20.00
Result
-------------------
2.00
5.00
Any help will be greatly appreciated.
Juan Alvarez
SELECT column2
FROM YourTable
GROUP BY column2
HAVING COUNT(*) = (SELECT COUNT(DISTINCT column1) FROM YourTable)

Resources