DolphinDB - Fill missing values of a sequence - sql

I have a table t
t=table(100 101 103 106 as price,take(10,4) as n)
price n
100 10
101 10
103 10
106 10
For the missing values of the column price, like 102, 104, and 105, I want to fill it with n=0.
price n
100 10
101 10
102 0
103 10
104 0
105 0
106 10

Not sure how will you generate the price seq, but you can do something like:
t=table(100 101 103 106 as price,take(10,4) as n)
t2=table(100..106 as price)
select price, n.nullFill(0) from t2 left join t on t2.price = t.price

Related

find records with same key but different second column value

My Input data is :
Id salary
101 1000
101 1000
102 2500
102 3000
105 5000
105 5000
105 5000
106 12
106 142
106 12
Output :
102 2500
102 3000
106 12
106 142
106 12
I mean based on the id , I want to find out which Id has different salary. If the salary is the same in all the records , I want to discard those records. Kindly help.
select *
from t
where id in(
select Id
from t
group by Id
having max(salary) <> min(salary)
)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
Fiddle
SELECT `Id`, `salary` FROM salary
WHERE Id IN( SELECT Id FROm salary GROUP By Id HAVING COUNT(DISTINCT SALARY) > 1)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
fiddle

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.

oracle query - something similar to pivot, but

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

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