Transpose Rows to Columns for the same ID - sql-server

I have table called equipment.
Unit_ID SensorSerial 1
11 1
22
1 33
2
44 2
55 2
66 3
77 3
88 3
99
I would like to make query to have following table:
Unit_ID Sensor1 Sensor2
Sensor3 1
11
22
33 2
44
55
66 3
77
88
99

You can try this
select Unit_ID,
max(case when seqnum = 1 then SensorSerial end) as Sensor1,
max(case when seqnum = 2 then SensorSerial end) as Sensor2,
max(case when seqnum = 3 then SensorSerial end) as Sensor3
from (select equipment.*,
row_number() over (partition by Unit_ID order by SensorSerial) as seqnum
from equipment
) equipment
group by Unit_ID;

Related

SQL - Select non repeating columns

I have a table like
id name R_id mgr_id
----------------------------
61 a 22 1
62 a 22 2
62 b 23 1
63 c 24 4
63 b 22 3
64 c 25 3
and I would like to get the following result set
R_id mgr_id
--------------
22 1
23 1
24 4
25 3
I would like select repeating R_ids only once
I tried using this query but with not much success, can anyone help me.
SELECT DISTINCT R_id, mgr_id from DT
Perhaps something like this... WITH TIES clause in concert with Row_NUmber()
Example
Select Top 1 with ties
R_ID
,mgr_id
From #YourTable
Order By Row_Number() over (Partition By R_ID order by Mgr_id)
Returns
R_ID mgr_id
22 1
23 1
24 4
25 3

How to Sort Amount Number Negative and Positive Number In SQL server

I have working SQL Server.I have More than 20000 Lines Using SQL server.I have column Filed Name Amount. in Amount Filed Inserted negative and Positive Number .Now I want Sort Amount Field Negative and Positive Number
Example Like :
Entity ExpenseTypeCode ExpenseType Amount
11 043 Hotel 5
12 044 travel 23
13 045 drink 55
14 046 Dinner 23
15 047 airline 556
16 048 Hotel -5
I how Like More than 30000 LINES .IN my table i have Expense type but negative and Positive value
I want Sort My table Like negative and Positive order same value
Entity ExpenseTypeCode ExpenseType Amount
11 043 Hotel 5
16 048 Hotel -5 --> Want sort like this
12 044 travel 23
13 045 drink 55
14 046 Dinner 23
15 047 airline 556
How can i sort my table liKE ?
Use ABS Function in sorting:
ABS() : It will convert your negative value to positive
SELECT
*
FROM TableName
Order BY ABS(Amount)
If you wants if negative and positive value same and order should consider positive first then:
SELECT
*
FROM TableName
Order BY ABS(Amount),Amount*-1
Example:
Initial
Output
select * from #t t
order by expensetype,
case when amount > 0 then 1 else 2 end
result
Entity ExpenseTypeCode ExpenseType Amount
----------- --------------- ----------- -----------
15 47 airline 556
14 46 Dinner 23
13 45 drink 55
11 43 Hotel 5
16 48 Hotel -5
12 44 travel 23
If you are looking for matching pairs then something like this might be what you want
declare #t table(Entity int, ExpenseTypeCode int, ExpenseType varchar(10), Amount int)
insert into #t values
( 11, 043, 'Hotel' , 6),
( 8, 043, 'Hotel' , 5),
( 9, 043, 'Hotel' , 5),
( 10, 043, 'Hotel' , 5),
( 12, 044, 'travel' , 23),
( 13, 045, 'drink' , 55),
( 14, 046, 'Dinner' , 23),
( 15, 047, 'airline' , 556),
( 16, 048, 'Hotel' , -5),
( 17, 048, 'Hotel' , -5),
( 18, 043, 'Hotel' , -6),
( 19, 043, 'Hotel' , -6)
select t.*,row_number() over(partition by t.ExpenseType, t.amount order by t.entity) rn,t.amount as absamount
from #t t
where t.amount > 0
union all
select t.*,row_number() over(partition by t.ExpenseTypeCode, t.amount order by t.entity) rn, abs(t.amount)
from #t t
where t.amount < 0
order by t.expensetype,absamount,rn,t.amount desc
result
Entity ExpenseTypeCode ExpenseType Amount rn absamount
----------- --------------- ----------- ----------- -------------------- -----------
15 47 airline 556 1 556
14 46 Dinner 23 1 23
13 45 drink 55 1 55
8 43 Hotel 5 1 5
16 48 Hotel -5 1 5
9 43 Hotel 5 2 5
17 48 Hotel -5 2 5
10 43 Hotel 5 3 5
11 43 Hotel 6 1 6
18 43 Hotel -6 1 6
19 43 Hotel -6 2 6
12 44 travel 23 1 23
or possibly a full join
select s.*,t.* from
(
select t.*,row_number() over(partition by t.ExpenseType, t.amount order by t.entity) rn
from #t t
where t.amount > 0
) s
full join
(
select t.*,row_number() over(partition by t.ExpenseTypeCode, t.amount order by t.entity) rn
from #t t
where t.amount < 0
) t on t.expensetype = s.expensetype and t.rn = s.rn and abs(t.amount) = s.amount
order by s.expensetype
Entity ExpenseTypeCode ExpenseType Amount rn Entity ExpenseTypeCode ExpenseType Amount rn
----------- --------------- ----------- ----------- -------------------- ----------- --------------- ----------- ----------- --------------------
NULL NULL NULL NULL NULL 19 43 Hotel -6 2
15 47 airline 556 1 NULL NULL NULL NULL NULL
14 46 Dinner 23 1 NULL NULL NULL NULL NULL
13 45 drink 55 1 NULL NULL NULL NULL NULL
11 43 Hotel 6 1 18 43 Hotel -6 1
10 43 Hotel 5 3 NULL NULL NULL NULL NULL
8 43 Hotel 5 1 16 48 Hotel -5 1
9 43 Hotel 5 2 17 48 Hotel -5 2
12 44 travel 23 1 NULL NULL NULL NULL NULL

Sqlserver predict top5 records but various group - mathematical logic

I have table like below,
user_id Task_name Score
__________________________________
1 101 10
1 102 9
1 103 8
2 104 10
2 105 9
3 106 9
4 107 9
4 108 8
5 109 8
5 110 7
5 111 6
6 112 10
6 113 9
6 114 8
7 115 9
7 116 8
I want to take top 5 score or max 5 Tasks.
Condition:
1) At least try to get one Tasks from one user.
Output:
User_Id Task_Name Score
____________________________________
1 101 10
2 104 10
6 112 10
3 106 9
4 107 9
I think this is what you need:
SELECT TOP 5 User_id, Task_name, Score
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY Score DESC) R
FROM Src
) T
ORDER BY R, Score DESC, user_id
It yields:
User_Id Task_Name Score
____________________________________
1 101 10
2 104 10
6 112 10
3 106 9
4 107 9
If you need top 5 ordered, wrap results with another SELECT..ORDER clauses:
SELECT * FROM
(
SELECT TOP 5 user_id, Task_name, Score
FROM
(
SELECT *, RANK() OVER (PARTITION BY user_id ORDER BY Score DESC) R
FROM Src
) T
ORDER BY R, Score DESC, user_id
) E
ORDER BY Score DESC, user_id
I think this is what you want:
select top 5 t.*
from (select t.*,
row_number() over (partition by user_id order by score desc) as seqnum
from t
) t
where seqnum = 1
order by score desc;
The row_number() is used to select the best row for each user. The outer query then chooses the top 5 users with the tasks.

SQL Server : transpose multiple columns [duplicate]

This question already has answers here:
SQL Server : Transpose rows to columns
(5 answers)
Closed 7 years ago.
What's the best way to get from this input to output?
Input:
date id name info price qty
-----------------------------------------------
20140523 10036 ABC B 12 100
20140523 10036 ABC S 13 75
20140523 10034 XYZ B 22 56
20140523 10034 XYZ S 24 41
20151023 10037 PQR B 30 45
20151023 10037 PQR S 5 20
Output:
date id name b_price b_qty s_price s_qty
---------------------------------------------------------
20140523 10036 ABC 12 100 13 75
20140523 10034 XYZ 22 56 24 41
20140523 10037 PQR 30 45 5 20
With conditional aggregation:
select date,
id,
name,
sum(case info = 'b' then price end) as bprice,
sum(case info = 'b' then qty end) as bqty,
sum(case info = 's' then price end) as sprice,
sum(case info = 's' then qty end) as sqty
from tablename
group by date, id, name

SQL Server : query for DENSE_RANK() grouped by 2 column?

Need help in construct SQL, over an orders table, that holds the Date, SalesID, ItemID and other misc. columns.
Table looks like:
Date SalesID ItemID
13-9-15 6:15:00 56 6
13-9-15 6:00:00 56 6
13-9-15 6:26:00 56 4
13-9-15 6:38:00 34 4
13-9-15 7:05:00 34 2
13-9-15 6:42:00 12 2
13-9-15 7:20:00 12 5
13-9-15 7:34:00 78 5
13-9-15 7:41:00 78 6
What I'd like to have as an additional column is, one counter which is increments each time when new SalesID begins order by date.And the counter column will count until max no is 3.I'm using DENSE_RANK()for the increment column.
Finally what I need:
Date SalesID ItemID Counter
13-9-15 6:00:00 56 6 1
13-9-15 6:15:00 56 6 1
13-9-15 6:26:00 56 4 1
13-9-15 6:38:00 34 4 2
13-9-15 6:42:00 34 2 2
13-9-15 7:05:00 12 2 3
13-9-15 7:20:00 12 5 3
13-9-15 7:34:00 78 5 1
13-9-15 7:41:00 78 6 1
This solution works for sqlserver 2012+. I had to correct invalid data in your example in order to get the correct output
DECLARE #t table(Date datetime, SalesID int, ItemID int)
INSERT #t values
('2015-09-13 6:15:00',56,6),
('2015-09-13 6:00:00',56,6),
('2015-09-13 6:26:00',56,4),
('2015-09-13 6:38:00',34,4),
('2015-09-13 6:42:00',34,2),
('2015-09-13 7:05:00',12,2),
('2015-09-13 7:10:00',12,5),
('2015-09-13 7:34:00',78,5),
('2015-09-13 7:41:00',78,6)
;WITH CTE as
(
SELECT
[Date], [SalesID], [ItemID],
CASE WHEN lag(SalesID) over (order by Date) = SalesID
THEN 0 ELSE 1 END x
FROM #t
)
SELECT
[Date], [SalesID], [ItemID],
(sum(x) over (ORDER BY Date) - 1) % 3 + 1 [Counter]
FROM CTE
Result:
Date SalesID ItemID Counter
2015-09-13 06:00 56 6 1
2015-09-13 06:15 56 6 1
2015-09-13 06:26 56 4 1
2015-09-13 06:38 34 4 2
2015-09-13 06:42 34 2 2
2015-09-13 07:05 12 2 3
2015-09-13 07:10 12 5 3
2015-09-13 07:34 78 5 1
2015-09-13 07:41 78 6 1
This will work for sqlserver 2008:
;WITH CTE as
(
SELECT
[Date],
SalesID,
ItemID,
row_number() over (order by Date)-
row_number() over (partition by SalesID order by Date) x
FROM #t
)
SELECT
[Date],
SalesID,
ItemID,
(dense_rank() over (order by x) - 1) % 3 + 1 [Counter]
FROM CTE

Resources