Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit or First x but by group [CORE2689] #1142

Closed
firebird-automations opened this issue Oct 18, 2009 · 6 comments
Closed

Limit or First x but by group [CORE2689] #1142

firebird-automations opened this issue Oct 18, 2009 · 6 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Ivan (patuljak)

Is replaced by CORE1688

first n is include in sql statement but if I want selected first n records for each group which is in group by clause I need usig row_number() over (partition by )
but I don't if firebird support row_number(other database support this)

CREATE TABLE employees (
employee_id integer PRIMARY KEY,
employee_name text
)

CREATE TABLE departments (
department_id integer PRIMARY KEY,
department_name text
)

CREATE TABLE emp_dep (
ID_employee integer,
ID_department integer,
date_of_insert date default NOW(),
mytime time default NOW(),
PRIMARY KEY(ID_employee, ID_department)
)

select emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc
limit by group(t3.department_name,3)

for first 3

or
select first 3 by group(t3.department_name) emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc

with one or more parameters for limit or first.

@firebird-automations
Copy link
Collaborator Author

Modified by: Ivan (patuljak)

description: first n is include in sql statement but if I want selected first n records for each group which is in group by clause I need usig row_number() over (partition by )
but I don't if firebird support row_number(other database support this)

CREATE TABLE employees (
employee_id integer PRIMARY KEY,
employee_name text
)

CREATE TABLE departments (
department_id integer PRIMARY KEY,
department_name text
)

CREATE TABLE emp_dep (
ID_employee integer,
ID_department integer,
date_of_insert date default NOW(),
mytime time default NOW(),
PRIMARY KEY(ID_employee, ID_department)
)

select emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc
limit by group(t3.department_name,3)

for first 3

or
select first 3 by group(t3.department_name) emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc

=>

first n is include in sql statement but if I want selected first n records for each group which is in group by clause I need usig row_number() over (partition by )
but I don't if firebird support row_number(other database support this)

CREATE TABLE employees (
employee_id integer PRIMARY KEY,
employee_name text
)

CREATE TABLE departments (
department_id integer PRIMARY KEY,
department_name text
)

CREATE TABLE emp_dep (
ID_employee integer,
ID_department integer,
date_of_insert date default NOW(),
mytime time default NOW(),
PRIMARY KEY(ID_employee, ID_department)
)

select emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc
limit by group(t3.department_name,3)

for first 3

or
select first 3 by group(t3.department_name) emp_dep.date_of_insert, emp_dep.mytime, t3.department_name , t2.employee_name from employees t2,departments t3,emp_dep
where emp_dep.ID_employee=t2.employee_id and emp_dep.ID_department=t3.department_id
group by emp_dep.date_of_insert,t3.department_name , t2.employee_name, emp_dep.mytime
order by emp_dep.date_of_insert desc, emp_dep.mytime desc

with one or more parameters for limit or first.

@firebird-automations
Copy link
Collaborator Author

Commented by: @dyemanov

Wouldn't it be better to ask for the standard compliant "row_number() over (partition by)" instead of introducing "first by group"? If you agree, I will close this ticket in favor of CORE1688.

@firebird-automations
Copy link
Collaborator Author

Commented by: Ivan (patuljak)

ok, then close.

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

Link: This issue is replaced by CORE1688 [ CORE1688 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Duplicate [ 3 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant