@@ -108,15 +108,56 @@ DO UPDATE SET activity_date_int = activity_date_int_table.activity_date_int | EX
108
108
-- Way to check whether that day from current day active or not, while checking the bit is active or not (right bit wise)
109
109
select userid, (activity_date_int >> 1 ) & 1 from activity_date_int_table
110
110
111
- -- Testing....
111
+ -- Testing zone....
112
+
113
+ -- Updating the user cummulative activity data with new data.
112
114
UPDATE activity_date_int_table
113
115
SET activity_date_int = activity_date_int | (1 << (CURRENT_DATE - ' 2024-07-22' ::date ))
114
116
WHERE userid = 456 ;
115
117
116
- with cte as (select generate_series(current_date - interval ' 7 day' , current_date - interval ' 1 day' , ' 1 day' ) rg)
118
+
119
+ -- Checking users last 7 day activity date wise
120
+
121
+ -- generate_series(current_date - interval '7 day', current_date - interval '1 day', '1 day')
122
+ -- activity_date_int >> (CURRENT_DATE - rg::date)) & 1
123
+
124
+ with cte as (select generate_series(0 , 6 ) rg)
117
125
select
118
- userid, rg,
119
- ( activity_date_int >> (CURRENT_DATE - rg:: date )) & 1 as active
126
+ userid,
127
+ count (case when ( activity_date_int >> (rg )) & 1 = 1 then 1 else 0 end) as week_active_lness
120
128
from cte cross join activity_date_int_table
121
- group by 1 ,2
122
- order by rg desc
129
+ group by 1
130
+
131
+
132
+
133
+ -- --------------------------------------------------------------------------------------------------------------------------------------------------------
134
+ -- TODO
135
+
136
+ CREATE OR REPLACE FUNCTION bit_counts (num BIGINT ) RETURNS INT AS $$
137
+ DECLARE
138
+ count INT := 0 ;
139
+ BEGIN
140
+ WHILE num > 0 LOOP
141
+ count := count + (num & 1 );
142
+ num := num >> 1 ;
143
+ END LOOP;
144
+ RETURN count;
145
+ END;
146
+ $$ LANGUAGE plpgsql;
147
+
148
+
149
+ SELECT
150
+ userid,
151
+ activity_date_int,
152
+ t .last_7_days_mask ,
153
+ bit_counts(activity_date_int & t .last_7_days_mask ) active_last_7_days
154
+ FROM activity_date_int_table
155
+ cross join (SELECT 1023 AS last_7_days_mask) t
156
+
157
+
158
+ SELECT
159
+ userid,
160
+ activity_date_int,
161
+ activity_date_int & cast(pow(2 , 12 ) as int ) - 1
162
+ -- LENGTH(REPLACE(((activity_date_int & ((1 << 11) - 1)::bigint))::bit(10))::text, '0', '')) AS last_10_days_data
163
+ FROM activity_date_int_table
0 commit comments