SQL Grouping by sequential occurrences of a value - sql

I have the below table with 2 columns
ID | Dept
1 | A
2 | A
3 | B
4 | B
5 | B
6 | A
I want to do a count such that the output should look as the table below.
Dept | Count
A | 2
B | 3
A | 1
Thanks for your help in advance!

Slightly different to Michael's, same result:
with cte1 as (
select id,
dept,
row_number() over (partition by dept order by id) -
row_number() over (order by id) group_num
from test),
cte2 as (
select dept,
group_num,
count(*) c_star,
max(id) max_id
from cte1
group by dept,
group_num)
select dept,
c_star
from cte2
order by max_id;
http://sqlfiddle.com/#!4/ff747/1

From your example, it looks like you're wanting to count sequential records for each department.
You can do this by combining the row number and the ordering Id.
create table tblDept (
id int not null,
dept varchar(50)
);
insert into tblDept values (1, 'A');
insert into tblDept values (2, 'A');
insert into tblDept values (3, 'B');
insert into tblDept values (4, 'B');
insert into tblDept values (5, 'B');
insert into tblDept values (6, 'A');
with orderedDepts as (
select
dept,
id,
row_number() over (partition by dept order by id) -
row_number() over (order by id) as rn
from tblDept
)
select
dept,
count(*) as num
from orderedDepts
group by
dept,
rn
order by
max(id)
Gives the output:
+------+-----+
| DEPT | NUM |
+------+-----+
| A | 2 |
| B | 3 |
| A | 1 |
+------+-----+
SQL Fiddle

You cannot do this with SQL. Count counts distinct items, so count in your case would give you a count of A and a count of B.
You can only count/group by values in the table, not by the order of rows. Order is not guaranteed in SQL if you don't use an order by anyway.

run this query for this:
SELECT Dept, count(*) FROM table_name group By Dept

Related

Is it possible to update rows randomly with a group of set values?

I have a work assignment table that I would like help with. What I would like to do is randomly assign peoples names to the rows in the table. For example, the table currently looks like:
TASK |NAME
1 Get Chicken |
2 Clean Chicken|
3 Cook Chicken |
4 Eat Chicken |
5 Eat Corn |
6 Takeout Trash|
I have 4 employees that I want to assign these tasks to, but do not want to show any favoritism. Here is what that table looks like:
NAME
John
Lucy
Fred
Jasmine
How can I randomly update the NAME field based on the above names?
edit based on comments. I changed the number of tasks to something not divisible by 4. In this case the number of tasks is now 6. I want to make it to where no one can get 2 or more tasks more then the rest of their colleagues. But in this case, it's ok for someone to have 1 more task then their colleagues. he result should be something like (but random):
TASK |NAME
1 Get Chicken |John
2 Clean Chicken|Jasmine
3 Cook Chicken |Lucy
4 Eat Chicken |Fred
5 Eat Corn |Fred
6 Takeout Trash|Jasmine
Here is a pure SQL way to do it.
MERGE INTO so_tasks t USING (
WITH numbered_tasks AS ( SELECT t.*,
row_number() OVER (ORDER BY dbms_random.value) task_number,
count(*) OVER () total_tasks FROM so_tasks t ),
numbered_employees AS ( SELECT e.*,
row_number() OVER (ORDER BY dbms_random.value) employee_number,
count(*) OVER () total_employees FROM so_employees e)
SELECT nt.task,
ne.name
FROM numbered_tasks nt
INNER JOIN numbered_employees ne
ON ne.employee_number-1 = mod(nt.task_number-1, ne.total_employees) ) u
ON ( t.task = u.task )
WHEN MATCHED THEN UPDATE SET t.name = u.name;
It sorts each list randomly and assigned a number to each row in each list. It then gets the row from the employee list whose number matched the task number MOD the total number of employees.
Here is a fully example:
Create tables
CREATE TABLE so_tasks
( task VARCHAR2(30) NOT NULL PRIMARY KEY,
name VARCHAR2(30) );
INSERT INTO so_tasks ( task ) VALUES ('Get Chicken');
INSERT INTO so_tasks ( task ) VALUES ('Clean Chicken');
INSERT INTO so_tasks ( task ) VALUES ('Cook Chicken');
INSERT INTO so_tasks ( task ) VALUES ('Eat Chicken');
INSERT INTO so_tasks ( task ) VALUES ('Eat Corn');
INSERT INTO so_tasks ( task ) VALUES ('Takeout Trash');
CREATE TABLE so_employees
( name VARCHAR2(30) NOT NULL PRIMARY KEY );
INSERT INTO so_employees ( name ) VALUES ('John');
INSERT INTO so_employees ( name ) VALUES ('Lucy');
INSERT INTO so_employees ( name ) VALUES ('Fred');
INSERT INTO so_employees ( name ) VALUES ('Jasmine');
COMMIT;
Merge
MERGE INTO so_tasks t USING (
WITH numbered_tasks AS ( SELECT t.*,
row_number() OVER (ORDER BY dbms_random.value) task_number,
count(*) OVER () total_tasks FROM so_tasks t ),
numbered_employees AS ( SELECT e.*,
row_number() OVER (ORDER BY dbms_random.value) employee_number,
count(*) OVER () total_employees FROM so_employees e)
SELECT nt.task,
ne.name
FROM numbered_tasks nt
INNER JOIN numbered_employees ne
ON ne.employee_number-1 = mod(nt.task_number-1, ne.total_employees) ) u
ON ( t.task = u.task )
WHEN MATCHED THEN UPDATE SET t.name = u.name;
Results
SELECT * FROM so_tasks;
+---------------+---------+
| TASK | NAME |
+---------------+---------+
| Get Chicken | John |
| Clean Chicken | Jasmine |
| Cook Chicken | Lucy |
| Eat Chicken | Fred |
| Eat Corn | Jasmine |
| Takeout Trash | Fred |
+---------------+---------+
Your exact assignments for each task will be different, but there will never be more than a one task difference between any two employees.
You can give the tasks random sequential numbers and the employees another random sequential number and then join the two tables using those numbers and then use a MERGE statement to update the table correlating on the ROWID pseudo-column to uniquely identify each task.
Oracle Setup:
CREATE TABLE table_name ( task VARCHAR2(20), name VARCHAR2(20) );
INSERT INTO table_name ( TASK )
SELECT 'Get Chicken' FROM DUAL UNION ALL
SELECT 'Clean Chicken' FROM DUAL UNION ALL
SELECT 'Cook Chicken' FROM DUAL UNION ALL
SELECT 'Eat Chicken' FROM DUAL UNION ALL
SELECT 'Eat Corn' FROM DUAL UNION ALL
SELECT 'Takeout Trash' FROM DUAL;
CREATE TABLE employees ( NAME ) AS
SELECT 'John' FROM DUAL UNION ALL
SELECT 'Lucy' FROM DUAL UNION ALL
SELECT 'Fred' FROM DUAL UNION ALL
SELECT 'Jasmine' FROM DUAL;
Merge:
MERGE INTO table_name dst
USING (
WITH random_tasks ( rid, rn ) AS (
SELECT ROWID,
ROW_NUMBER() OVER ( ORDER BY DBMS_RANDOM.VALUE )
FROM table_name
),
random_names ( name, rn, num_employees ) AS (
SELECT name,
ROW_NUMBER() OVER ( ORDER BY DBMS_RANDOM.VALUE ),
COUNT(*) OVER ()
FROM employees
)
SELECT rid,
name
FROM random_tasks t
INNER JOIN
random_names n
ON ( MOD( t.rn, n.num_employees ) + 1 = n.rn )
) src
ON ( src.rid = dst.ROWID )
WHEN MATCHED THEN
UPDATE SET name = src.name;
Result:
SELECT * FROM table_name;
TASK | NAME
:------------ | :------
Get Chicken | John
Clean Chicken | Jasmine
Cook Chicken | Fred
Eat Chicken | Lucy
Eat Corn | Fred
Takeout Trash | Lucy
db<>fiddle here
Assuming you're fine with a PL/SQL solution (you could do it in a single update statement but unless it's performance critical, I'd find the loop easier to follow)
begin
for src in (select t.task_id, e.name
from (select t.*,
row_number() over (order by dbms_random.value) rnk
from task t) t
join
(select e.*,
row_number() over (order by dbms_random.value) rnk,
count(*) over () num_emps
from employee e) e
on( mod( t.rnk, e.num_emps ) = e.rnk - 1 ) )
loop
update task
set name = src.name
where task_id = src.task_id;
end loop;
end;
/
Basically, this is randomly sorting both lists and then going down the list of tasks and assigning the next employee to that task. If the number of tasks isn't a multiple of the number of employees, some employees will get an extra task but no employee will have more than 1 more task than another.

Insert column with Max in sql

I did not know how to insert column with max.
Select id,MAX(salary),Min(Salary)
from C
GROUP BY id;
it is give me the all id with it is maximum
and I want just the id with maximum and minimum of salary!!
Several options for you that only require a single scan of the table:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE C ( ID, SALARY ) AS
SELECT 1, 100 FROM DUAL
UNION ALL SELECT 2, 110 FROM DUAL
UNION ALL SELECT 3, 100 FROM DUAL
UNION ALL SELECT 4, 110 FROM DUAL
UNION ALL SELECT 5, 90 FROM DUAL
Query 1 - Get a single ID:
SELECT *
FROM (
SELECT ID, SALARY
FROM c
ORDER BY SALARY DESC
)
WHERE ROWNUM = 1
Results:
| ID | SALARY |
|----|--------|
| 2 | 110 |
Query 2 - Get a single ID (alternate method that will get min and max IDs):
SELECT MAX( ID ) KEEP ( DENSE_RANK LAST ORDER BY SALARY ) AS MAX_SALARY_ID,
MAX( SALARY ) AS MAX_SALARY,
MIN( ID ) KEEP ( DENSE_RANK FIRST ORDER BY SALARY ) AS MIN_SALARY_ID,
MIN( SALARY ) AS MIN_SALARY
FROM C
Results:
| MAX_SALARY_ID | MAX_SALARY | MIN_SALARY_ID | MIN_SALARY |
|---------------|------------|---------------|------------|
| 4 | 110 | 5 | 90 |
Query 3 - Get all the IDs with the maximum salary:
SELECT ID, SALARY
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY DESC ) AS RNK
FROM C
)
WHERE RNK = 1
Results:
| ID | SALARY |
|----|--------|
| 2 | 110 |
| 4 | 110 |
Query 4 - Get all IDs for min and max salary:
SELECT LISTAGG( CASE MIN_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MIN_SALARY_IDS,
MAX( CASE MIN_RANK WHEN 1 THEN SALARY END ) AS MIN_SALARY,
LISTAGG( CASE MAX_RANK WHEN 1 THEN ID END, ',' ) WITHIN GROUP ( ORDER BY ID ) AS MAX_SALARY_IDS,
MAX( CASE MAX_RANK WHEN 1 THEN SALARY END ) AS MAX_SALARY
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY ASC ) AS MIN_RANK,
RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
FROM C
)
Results:
| MIN_SALARY_IDS | MIN_SALARY | MAX_SALARY_IDS | MAX_SALARY |
|----------------|------------|----------------|------------|
| 5 | 90 | 2,4 | 110 |
Query 5:
SELECT ID,
SALARY,
CASE WHEN MIN_RANK = 1 THEN 'MIN'
WHEN MAX_RANK = 1 THEN 'MAX' END AS MIN_MAX
FROM (
SELECT ID,
SALARY,
RANK() OVER ( ORDER BY SALARY ASC ) AS MIN_RANK,
RANK() OVER ( ORDER BY SALARY DESC ) AS MAX_RANK
FROM C
)
WHERE MIN_RANK = 1 OR MAX_RANK = 1
Results:
| ID | SALARY | MIN_MAX |
|----|--------|---------|
| 2 | 110 | MAX |
| 4 | 110 | MAX |
| 5 | 90 | MIN |
Select id,
salary
from C
where salary = (select MAX(salary)
from C)
you can use first_value or last_value
The FIRST_VALUE analytic function is similar to the FIRST analytic
function, allowing you to return the first result from an ordered set.
https://oracle-base.com/articles/misc/first-value-and-last-value-analytic-functions
create table C (id int, salary int);
insert into c values(1, 1);
insert into c values(2, 2);
insert into c values(3, 3);
insert into c values(4, 4);
insert into c values(5, 5);
Select distinct first_value(id) over ( order by salary desc)
from C
FIRST_VALUE(ID)OVER(ORDERBYSAL
1 5

How to GROUP entries BY uninterrupted sequence?

CREATE TABLE entries (
id serial NOT NULL,
title character varying,
load_sequence integer
);
and data
INSERT INTO entries(title, load_sequence) VALUES ('A', 1);
INSERT INTO entries(title, load_sequence) VALUES ('A', 2);
INSERT INTO entries(title, load_sequence) VALUES ('A', 3);
INSERT INTO entries(title, load_sequence) VALUES ('A', 6);
INSERT INTO entries(title, load_sequence) VALUES ('B', 4);
INSERT INTO entries(title, load_sequence) VALUES ('B', 5);
INSERT INTO entries(title, load_sequence) VALUES ('B', 7);
INSERT INTO entries(title, load_sequence) VALUES ('B', 8);
Is there a way in PostgreSQL to write SQL that groups data by same title segments after ordering them by load_sequence.
I mean:
=# SELECT id, title, load_sequence FROM entries ORDER BY load_sequence;
id | title | load_sequence
----+-------+---------------
9 | A | 1
10 | A | 2
11 | A | 3
13 | B | 4
14 | B | 5
12 | A | 6
15 | B | 7
16 | B | 8
AND I want groups:
=# SELECT title, string_agg(id::text, ',' ORDER BY id) FROM entries ???????????;
so result would be:
title | string_agg
-------+-------------
A | 9,10,11
B | 13,14
A | 12
B | 15,16
You can use the following query:
SELECT title, string_agg(id::text, ',' ORDER BY id)
FROM (
SELECT id, title,
ROW_NUMBER() OVER (ORDER BY load_sequence) -
ROW_NUMBER() OVER (PARTITION BY title
ORDER BY load_sequence) AS grp
FROM entries ) AS t
GROUP BY title, grp
Calculated grp field serves to identify slices of title records having consecutive load_sequence values. Using this field in the GROUP BY clause we can achieve the required aggregation over id values.
Demo here
There's a trick you can use with sum as a window function running over a lagged window for this.
The idea is that when you hit an edge/discontinuity you return 1, otherwise you return 0. You detect the discontinuities using the lag window function.
SELECT title, string_agg(id::text, ', ') FROM (
SELECT
id, title, load_sequence,
sum(title_changed) OVER (ORDER BY load_sequence) AS partition_no
FROM (
SELECT
id, title, load_sequence,
CASE WHEN title = lag(title, 1) OVER (ORDER BY load_sequence) THEN 0 ELSE 1 END AS title_changed FROM entries
) x
) y
GROUP BY partition_no, title;

Inner Join + select the most recent

I have been trying to do the bellow query but honestly it's driving me crazy.
I have 2 Tables on MS SQL CE 4.0
Table 1 Name: Items
ID
Item_Code
Logged_by
Description
ID | Item_Code | Logged_by | Description
1 | A | Pete | just an A
2 | B | Mary | Seams like a B
3 | C | Joe | Obviously this is a C
4 | D | Pete | This is another A
Table 2 Name: Item_Comments
ID
Item_Code
Comment
Date
ID | Item_Code | Comment | Date
1 | B | Done | 2014/08/08
2 | A | Nice A | 2014/08/08
3 | B | Send 1 More | 2014/08/09
4 | C | Done | 2014/08/10
5 | D | This is an A | 2014/08/10
6 | D | Opps Sorry | 2014/08/11
The wanted result: I'm looking to join the most recent comment from Item_Comments to the Items Table
ID | Item_Code | Logged_by | Description | Comment
1 | A | Pete | just an A | Nice A
2 | B | Mary | Seams like a B | Send 1 More
3 | C | Joe | Obviously this is a C | Done
4 | D | Pete | This is another A | Opps Sorry
I did this query but I'm getting all the information =( mixed.
SELECT *
FROM Items t1
JOIN
(SELECT Item_Code, Comment, MAX(date) as MyDate
FROM Item_Comments
Group By Item_Code, Comment, Date
) t2
ON Item_Code= Item_Code
ORDER BY t1.Item_Code;
Do you know any way to do this ?
Try:
select x.*, z.comment
from items x
join (select item_code, max(date) as latest_dt
from item_comments
group by item_code) y
on x.item_code = y.item_code
join item_comments z
on y.item_code = z.item_code
and y.latest_dt = z.date
Fiddle test: http://sqlfiddle.com/#!6/d387f/8/0
You were close with your query but in your inline view aliased as t2 you were grouping by comment, leaving the max function to not actually aggregate anything at all. In t2 you should have just selected item_code and max(date) and grouped only by item_code, then you can use that to join into item_comments (y and z in my query above).
This is a second way of doing this using a subquery, however I would stick to the above (a join w/ an inline view):
select i.*, c.comment
from items i
join item_comments c
on i.item_code = c.item_code
where c.date = (select max(x.date)
from item_comments x
where x.item_code = c.item_code)
order by i.id
Fiddle test: http://sqlfiddle.com/#!6/d387f/11/0
Note if you run this inside piece you get every single record:
SELECT Item_Code, Comment, MAX(date) as MyDate
FROM Item_Comments
Group By Item_Code, Comment, Date
You want only the most recent comment. Assuming this is SQL Server 2008 or earlier, this get's you the most recent date for each Item_Code:
SELECT Item_Code, MAX(date) as MyDate
FROM Item_Comments
Group By Item_Code
Now you need to join that back and look up the comment on that date:
SELECT C.*
FROM Item_Comments C
INNER JOIN
(SELECT Item_Code, MAX(date) as MyDate
FROM Item_Comments
Group By Item_Code
) t2
ON C.Item_Code= t2.Item_Code
AND C.date = t2.MyDate
Now you can use that to join back to your original table:
SELECT t1.*, LatestComment.*
FROM Items t1
INNER JOIN
(
SELECT C.*
FROM Item_Comments C
INNER JOIN
(SELECT Item_Code, MAX(date) as MyDate
FROM Item_Comments
Group By Item_Code
) t2
ON C.Item_Code= t2.Item_Code
AND C.date = t2.MyDate
) LatestComment
On LatestComment.Item_Code = t1.Item_Code
Depending on the actual database you are using, this can get much simpler. Thats why you need to tag your database and version.
Try this,
create table items (id int, item_code char(1), logged_by varchar(10), description varchar(30));
insert into items values (1, 'A', 'Pete', 'just an A');
insert into items values (2, 'B', 'Mary', 'Seams like a B');
insert into items values (3, 'C', 'Joe', 'Obviously this is a C');
insert into items values (4, 'D', 'Pete', 'This is another A');
create table item_comments (id int, item_code char(1), comment varchar(20), date date);
insert into item_comments values (1, 'B', 'Done', '2014/08/08');
insert into item_comments values (2, 'A', 'Nice A', '2014/08/08');
insert into item_comments values (3, 'B', 'Send 1 More', '2014/08/09');
insert into item_comments values (4, 'C', 'Done', '2014/08/10');
insert into item_comments values (5, 'D', 'This is an A', '2014/08/10');
insert into item_comments values (6, 'D', 'Opps Sorry', '2014/08/11');
select * from items;
select * from item_comments;
select * from (select i.logged_by,i.id,i.item_code,i.description,ic.comment
,row_number() over(partition by i.id order by i.id )as Rnk
from items i inner join item_comments ic
on i.item_code=ic.item_code and i.id in(1,3)) x
where x.Rnk=1
union
select * from (select i.logged_by,i.id,i.item_code,i.description,ic.comment
,row_number() over(partition by i.id order by i.id )as Rnk
from items i inner join item_comments ic
on i.item_code=ic.item_code and i.id in(2,4)
) x where x.Rnk=2 order by item_code

double sorted selection from a single table

I have a table with an id as the primary key, and a description as another field.
I want to first select the records that have the id<=4, sorted by description, then I want all the other records (id>4), sorted by description. Can't get there!
select id, descr
from t
order by
case when id <= 4 then 0 else 1 end,
descr
select *, id<=4 as low from table order by low, description
You may want to use an id <= 4 expression in your ORDER BY clause:
SELECT * FROM your_table ORDER BY id <= 4 DESC, description;
Test case (using MySQL):
CREATE TABLE your_table (id int, description varchar(50));
INSERT INTO your_table VALUES (1, 'c');
INSERT INTO your_table VALUES (2, 'a');
INSERT INTO your_table VALUES (3, 'z');
INSERT INTO your_table VALUES (4, 'b');
INSERT INTO your_table VALUES (5, 'g');
INSERT INTO your_table VALUES (6, 'o');
INSERT INTO your_table VALUES (7, 'c');
INSERT INTO your_table VALUES (8, 'p');
Result:
+------+-------------+
| id | description |
+------+-------------+
| 2 | a |
| 4 | b |
| 1 | c |
| 3 | z |
| 7 | c |
| 5 | g |
| 6 | o |
| 8 | p |
+------+-------------+
8 rows in set (0.00 sec)
Related post:
Using MySql, can I sort a column but have 0 come last?
select id, description
from MyTable
order by case when id <= 4 then 0 else 1 end, description
You can use UNION
SELECT * FROM (SELECT * FROM table1 WHERE id <=4 ORDER by description)aaa
UNION
SELECT * FROM (SELECT * FROM table1 WHERE id >4 ORDER by description)bbb
OR
SELECT * FROM table1
ORDER BY
CASE WHEN id <=4 THEN 0
ELSE 1
END, description