Query to sum calls - sql

I have table contain call durations of a telecom company.
ex:
Table 1
| callerid | receiverid | call duration
| 1 | 2 | 5
| 1 | 2 | 2
| 2 | 3 | 4
| 1 | 5 | 2
i need to query above table so the result table after query:
Table 2
| callerid | receiverid | call duration
| 1 | 2 | 7
| 2 | 3 | 4
| 1 | 5 | 2

use below
select callerid, receiverid, sum(call_duration) call_duration
from your_table
group by callerid, receiverid
if applied to sample data in your question - output is

Related

SQL query many-to-many association table that have only one single associated item

I have promotionCategories table below;
+----+--------------+-------------+
| id | promotion_id | category_id |
+----+--------------+-------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 4 | 2 |
| 6 | 4 | 3 |
| 7 | 5 | 2 |
+----+--------------+-------------+
I have no idea how to query something like
Any promotion that have only one category and it is category_id = 1
expect result id 1, 4 which there are promotion_id 1,3 that have only single category_id = 1
We can use aggregation here:
SELECT promotion_id
FROM yourTable
GROUP BY promotion_id
HAVING MIN(category_id) = MAX(category_id) AND -- only one category
MIN(category_id) = 1; -- that category is 1

Count rows in table that are the same in a sequence

I have a table that looks like this
+----+------------+------+
| ID | Session_ID | Type |
+----+------------+------+
| 1 | 1 | 2 |
| 2 | 1 | 4 |
| 3 | 1 | 2 |
| 4 | 2 | 2 |
| 5 | 2 | 2 |
| 6 | 3 | 2 |
| 7 | 3 | 1 |
+----+------------+------+
And I would like to count all occurences of a type that are in a sequence.
Output look some how like this:
+------------+------+-----+
| Session_ID | Type | cnt |
+------------+------+-----+
| 1 | 2 | 1 |
| 1 | 4 | 1 |
| 1 | 2 | 1 |
| 2 | 2 | 2 |
| 3 | 2 | 1 |
| 3 | 1 | 1 |
+------------+------+-----+
A simple group by like
SELECT session_id, type, COUNT(type)
FROM table
GROUP BY session_id, type
doesn't work, since I need to group only rows that are "touching".
Is this possible with a merge sql-select or will I need some sort of coding. Stored Procedure or Application side coding?
UPDATE Sequence:
If the following row has the same type, it should be counted (ordered by ID).
to determine the sequence the ID is the key with the session_ID, since I just want to group rows with the same session_ID.
So if there are 3 rows is in one session
row with the ID 1 has type 1,
and the second row has type 1
and row 3 has type 2
Input:
+----+------------+------+
| ID | Session_ID | Type |
+----+------------+------+
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 2 |
+----+------------+------+
The squence is Row 1 to Row 2. This three row should output
Output:
+------------+------+-------+
| Session_ID | Type | count |
+------------+------+-------+
| 1 | 1 | 2 |
| 3 | 2 | 1 |
+------------+------+-------+
You can use a difference of id and row_number() to identify the gaps and then perform your count
;with cte as
(
Select *, id - row_number() over (partition by session_id,type order by id) as grp
from table
)
select session_id,type,count(*) as cnt
from cte
group by session_id,type,grp
order by max(id)

SQL generate unique ID from rolling ID

I've been trying to find an answer to this for the better part of a day with no luck.
I have a SQL table with measurement data for samples and I need a way to assign a unique ID to each sample. Right now each sample has an ID number that rolls over frequently. What I need is a unique ID for each sample. Below is a table with a simplified dataset, as well as an example of a possible UID that would do what I need.
| Row | Time | Meas# | Sample# | UID (Desired) |
| 1 | 09:00 | 1 | 1 | 1 |
| 2 | 09:01 | 2 | 1 | 1 |
| 3 | 09:02 | 3 | 1 | 1 |
| 4 | 09:07 | 1 | 2 | 2 |
| 5 | 09:08 | 2 | 2 | 2 |
| 6 | 09:09 | 3 | 2 | 2 |
| 7 | 09:24 | 1 | 3 | 3 |
| 8 | 09:25 | 2 | 3 | 3 |
| 9 | 09:25 | 3 | 3 | 3 |
| 10 | 09:47 | 1 | 1 | 4 |
| 11 | 09:47 | 2 | 1 | 4 |
| 12 | 09:49 | 3 | 1 | 4 |
My problem is that rows 10-12 have the same Sample# as rows 1-3. I need a way to uniquely identify and group each sample. Having the row number or time of the first measurement on the sample would be good.
One other complication is that the measurement number doesn't always start with 1. It's based on measurement locations, and sometimes it skips location 1 and only has locations 2 and 3.
I am going to speculate that you want a unique number assigned to each sample, where now you have repeats.
If so, you can use lag() and a cumulative sum:
select t.*,
sum(case when prev_sample = sample then 0 else 1 end) over (order by row) as new_sample_number
from (select t.*,
lag(sample) over (order by row) as prev_sample
from t
) t;

Assigning a unique ID for several rows based on certain values in postgresql

I have a CTE that returns something similar to what you see below:
Date | ID | Category
--------------------
1/1/18 | 1 | Delivery
1/2/18 | 2 | Return
1/4/18 | 3 | Delivery
1/5/18 | 4 | Middle
1/6/18 | 5 | Return
1/7/18 | 6 | Delivery
I am looking to add a unique and incrementing ID that starts with category = 'Delivery' and ends/resets after category = 'Return'.
For example...
Date | ID | Category | New ID
-------------------------------
1/1/18 | 1 | Delivery | 1
1/2/18 | 2 | Return | 1
1/4/18 | 3 | Delivery | 2
1/5/18 | 4 | Middle | 2
1/6/18 | 5 | Return | 2
1/7/18 | 6 | Delivery | 3
...
Most of my experience is with MySQL, so I could do this pretty easily with a stored procedure, but having a hard time figuring it out with postgres.
Note, this is all defined by CTE and is held in-query - I do not have an actual underlying table to UPDATE
Use progressive sum() to calculate the number of instances of the Delivery category:
with my_data(date, id, category) as (
values
('1/1/18', 1, 'Delivery'),
('1/2/18', 2, 'Return'),
('1/4/18', 3, 'Delivery'),
('1/5/18', 4, 'Middle'),
('1/6/18', 5, 'Return'),
('1/7/18', 6, 'Delivery')
)
select *, sum((category = 'Delivery')::int) over (order by id) as new_id
from my_data
date | id | category | new_id
--------+----+----------+--------
1/1/18 | 1 | Delivery | 1
1/2/18 | 2 | Return | 1
1/4/18 | 3 | Delivery | 2
1/5/18 | 4 | Middle | 2
1/6/18 | 5 | Return | 2
1/7/18 | 6 | Delivery | 3
(6 rows)

How to select if similar field count is the maximum in the table?

I want to select from a table if row counts of similar filed is maximum depends on other columns.
As example
| user_id | team_id | isOk |
| 1 | 1 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 1 |
| 4 | 1 | 1 |
| 5 | 2 | 1 |
| 6 | 2 | 1 |
| 7 | 2 | 1 |
| 8 | 3 | 1 |
| 9 | 3 | 1 |
| 10 | 3 | 1 |
| 11 | 3 | 0 |
So i want to select team 1 and 2 because they all have 1 value at isOk Column,
i tried to use this query
SELECT Team
FROM _Table1
WHERE isOk= 1
GROUP BY Team
HAVING COUNT(*) > 3
But still i have to define a row count which can be maximum or not.
Thanks in advance.
Is this what you are looking for?
select team
from _table1
group by team
having min(isOk) = 1;