oracle query - something similar to pivot, but - sql

I have a table like this:
ID_ORIG ID_DEST DISTANCE
------- ------- --------
1 101 10
1 102 15
1 103 20
2 101 25
2 102 30
2 103 35
3 101 40
3 102 45
3 103 50
And I want the following result:
ID_ORIG 101 102 103
------- --- --- ---
1 10 15 20
2 25 30 35
3 40 45 50
I tried to use 'pivot', but obiously didn't achive this result. Database is Oracle 11g.
Any help would be appreciated.

Please try:
select * From(
select * from YourTable
) PIVOT (sum(DISTANCE) for (ID_DEST) IN (101, 102, 103));
SQL Fiddle Demo

Related

I'm having this error when compiling my code: value with SQL

Team numbers are found by dividing the employee id by four. If 4 divides the employee id evenly, then that employee is part of team 1. For the other cases, a remainder of 1 places that employee on team 2, etc.
I got this result when I run the Code
SELECT EMPLOYEE_ID, MOD(EMPLOYEE_ID, 4) AS "TEAM"
100 0
101 1
102 2
103 3
104 0
105 1
106 2
107 3
but I need to get the following result:
100 1
101 2
102 3
103 4
104 1
105 2
106 3
107 4
Trivially, you may just add one to result of the MOD function:
SELECT
EMPLOYEE_ID,
MOD(EMPLOYEE_ID, 4) + 1 AS "TEAM"
FROM yourTable;

Show Budget column to Position Column in sql

My Table Structure
Designation_id Budget
102 6
105 5
106 2
Need Output.
Designation_ID Position
102 1
102 2
102 3
102 4
102 5
102 6
105 1
105 2
105 3
105 4
105 5
106 1
106 2
Is this is possible.
You need a table of numbers. This can be created on the fly. Here is typical syntax:
select s.designation_id, n.n as position
from (select 1 as n union all select 2 union all select 3 union all select 4 union all
select 5 union all select 6
) n join
structure s
on s.budget >= n.n;
The exact syntax for the subquery may vary, depending on the database.

SUM column values based on two rows in the same tables in SQL

I have one table like below in my SQL server.
Trans_id br_code bill_no amount
1 22 111 10
2 22 111 20
3 22 111 30
4 22 111 40
5 22 111 10
6 23 112 20
7 23 112 20
8 23 112 20
9 23 112 30
and I want desired output like below table
s.no br_code bill_no amount
1 22 111 110
2 23 112 90
try this:
select br_code, bill_no, sum(amount)
from TABLE
group by br_code, bill_no

SQL Syntax to group this data in SQL Server 2012

I have a table (called StayDate) which looks like this:
ResaID Date RoomCategory RateAmount
1 2014-09-01 A 125
1 2014-09-02 A 125
1 2014-09-03 B 140
2 2014-09-04 A 125
2 2014-09-05 A 125
2 2014-09-06 A 125
2 2014-09-07 C 160
2 2014-09-08 C 160
The output from the SQL syntax I'm after need to look like this:
ResaID Count RoomCategory RateAmount
1 2 A 125
1 1 B 140
2 3 A 125
2 2 C 160
Can anyone help with the SQL syntax needed to summarize the data as above?
A way to do this without a GROUP BY:
SELECT DISTINCT ResaID, COUNT(*) OVER (PARTITION BY ResaID, RoomCategory, RateAmount) Count, RoomCategory, RateAmount
FROM StayDate

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo