I am wondering how to create a SQL Query to calculate if the row is unique based on certain columns(in this case InventoryID, CheckoutID, postal code, and UserID) to determine if its the first one that is unique then give it a value 1 if it has occurred again give it a 0.
Primary Key
StoreID
InventoryID
CheckoutID
Postal Code
UserID
1
101
201
301
111
A1
2
101
201
301
111
A1
3
102
202
302
112
A2
4
103
203
303
113
A3
5
104
204
304
114
A4
6
104
205
305
114
A4
7
104
205
305
114
A4
I am looking to add a column or out how to get it to look like this:
Primary Key
StoreID
InventoryID
CheckoutID
Postal Code
UserID
unique
1
101
201
301
111
A1
1
2
101
201
301
111
A1
0
3
102
202
302
112
A2
1
4
103
203
303
113
A3
1
5
104
204
304
114
A4
1
6
104
205
305
114
A4
1
7
104
205
305
114
A4
0
Assuming you are ordering by primary key, you can try this:
SELECT
"Primary Key", StoreID, InventoryID, CheckoutID, "Postal Code", UserID,
case WHEN row_num = 1 THEN 1 ELSE 0 END AS "unique"
FROM (
SELECT
*, ROW_NUMBER() OVER (PARTITION BY InventoryID, CheckoutID, "Postal Code", UserID ORDER BY "Primary Key") AS row_num
FROM my_table
) foo;
Related
I have the following table -
ID A1 A2 A3 A4 A5 A6
1 324 243 3432 23423 342 342
2 342 242 4345 23423 324 342
I can unnest this table to give me counts of all numbers like so -
324 2
243 1
3432 1
23423 1
342 3
242 1
4345 1
23423 1
But how do I get it to count numbers in the same row only 1 time. For example, this is the output I am expecting -
324 2
243 1
3432 1
23423 1
342 2
242 1
4345 1
23423 1
342 is 2 because -
1) It is in the first row.
2) It appears 2 times in the second row, but I only want to count it once.
Simply use count(distinct):
select v.a, count(distinct t.id)
from t cross join lateral
(values (a1), (a2), (a3), (a4), (a5), (a6)
) v(a)
group by v.a;
Here is a db<>fiddle.
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
I have the following relation:
employeevalue(id, name, value, code)
id name value code
101 bobby 150 100
101 bobby 12 150
101 bobby 14.6 200
102 mary 189 100
102 mary 128 150
102 mary 112 200
103 john 112 100
103 john 13 150
103 john 76 200
Where code 100 is value1, 150 is value2 and 200 is value3. How could I write an SQL statement to retrieve the following from this table?
id name value1 value2 value3
101 bobby 150 12 14.6
102 mary 189 128 112
103 john 112 13 76
You can do this with conditional aggregation:
select id,
max(case when code = 100 then value end) as value1,
max(case when code = 150 then value end) as value2,
max(case when code = 200 then value end) as value3
from table t
group by id;
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.
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