update corresponding value from other table - sql

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

Related

How to select multiple counts from the same column in hive

I have the following table in hive :
userid productid action
1 101 Browse
2 102 Clicked
3 103 AddToCart
4 104 Purchase
5 105 LogOut
6 106 Browse
7 107 Browse
8 108 Browse
9 109 Clicked
10 110 Clicked
11 111 Clicked
12 112 Clicked
13 101 Browse
14 101 Browse
15 101 Browse
16 101 Browse
17 102 Clicked
18 103 AddToCart
19 102 Clicked
20 103 AddToCart
Now in my output i want productid and count of those actions which are Browse or Clicked.
**
Output :
**
productid browseCount clickCount
101 5 1
102 null 4
106 1 null
107 1 null
108 1 null
109 null 1
110 null 1
111 null 1
112 null 1
You can do it with conditional sum, e.g.,
select
productid,
sum(if(action = 'Browse', 1, 0)) as BrowseCount,
sum(if(action = 'Clicked', 1, 0)) as ClickedCount
from table
group by productid

Oracle select query based on multiple conditions

MESSAGE_ID GROUP_ID REV_NO
100 200 1
101 201 1
102 202 1
103 203 1
104 204 1
105 200 2
106 201 2
107 202 2
108 203 2
109 204 2
110 205 2
First I want to select all group ID's and their correpsponding lowest revision number.
Then I want select first X message ID's (Controllable X input) with condition that it should contain all the revisions of of any selected group. For e.g if I select first 5 messages by rownum then all revisions of group_id 200 is not selected.
Hope I made it clear.

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

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