help with query in DB2 - sql

i would like your help with my query.I have a table employee.details with the following columns:
branch_name, firstname,lastname, age_float.
I want this query to list all the distinct values of the age_float
attribute, one in each row of the result table, and beside each in the second field show the
number of people in the details table who had ages less than or equal to that value.
Any ideas? Thank you!

You can use OLAP functions:
SELECT DISTINCT age_float,
COUNT(lastname) OVER(ORDER BY age_float) AS number
FROM employee_details
COUNT(lastname) OVER(ORDER BY age_float) AS number orders rows by age, and returns employees count whose age <= current row age
or a simple join:
SELECT A.age_float, count(lastname)
FROM (SELECT DISTINCT age_float FROM employee_details) A
JOIN employee_details AS ED ON ED.age_float <= A.age_float
GROUP BY A.age_float

Related

Identifying a Distinct Count for a Column Without Using Group By

I'm trying to figure out how to get the distinct count of something that's conditional and doesn't use group by. I've got a table that has columns as seen here:
Employeeid, Training_Course_name, CompletedDate
Some of the courses have the word Rope in them.
I want to take the number of completed courses per person with the word "Rope" in the title and divide it by the number of unique courses there are that have the word rope in the title. If there are 15 unique course names that have the word rope in the title, regardless of who they're assigned to, I want to come up with that number and have it divided into the number of completed rope courses per person.
You can use conditional aggregation:
select count(distinct case when Training_Course_name like '%rope%'
then Training_Course_name
end) as courses_with_rope
This will help you to solve your problem
Declare #UniqueCourses As TABLE(Course As VarChar(32))
Select #UniqueCourses = Training_Course_Name
From
(SELECT DISTINCT Training_Course_Name
FROM Employess
WHERE Training_Course_Name LIKE '%Rope%') A
SELECT
EmpId,
(SELECT COUNT(1) FROM Employees innerEmployees
WHERE innerEmployees.EmpId = outerEmployees.EmpId AND
innerEmployees.CompletedDate is not null
) AS Completed Courses
From Employees outerEmployees
You can get the course with the word rope in them with this query
SELECT Employeeid, Training_Course_name, CompletedDate
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
And a distinct count like this
SELECT Employeeid, Training_Course_name, CompletedDate,
count(distinct Training_Course_name) as distinct_names
FROM Table_Name_You_Did_Not_Say
WHERE Training_Course_name LIKE '%rope%'
Anything "by employee id" would require a group by -- so what exactly is your requirement?

join and group by in SQL

I have two tables that I was going to join, but I understand it's more efficient to use CREATE VIEW. This is what I have:
CREATE OR REPLACE VIEW view0_joinedTablesGrouped
AS
Select table1.*,table2.*
FROM table1
inner join table2 on table1.col =
table2.matchingcol
group by table2.matchingcol;
which causes the following error:
ERROR: column "table1.col" must appear in the GROUP BY clause or be
used in an aggregate function
LINE 3: Select table.*,table2.*
Group By cannot do what you are trying to do.
Consider a simple table:
Name Age
-------
Ann 10
Bill 10
Chris 11
If you try to group by age with:
Select * from Table group by Age
What, exactly, do you expect to appear in the Name column for Age=10? Ann, or Bill or both or neither or ....? There is no good answer.
So, when you group by, every column in the output has to be an aggregate – that means a function of every row in the group.
So these are valid:
Select Age, Count(*) from Table group by Age
Select Age, Max( Length(Name)) from Table group by Age
Select Age, Max(Name) from Table group by Age
But this is impossible to do, and isn't valid:
Select Age,Name from Table group by Age
So your select * is the problem -- you can't just select column values because when you group by there's a whole group of column values for every output row, and you can't stuff all those values into one column of one row.
As for using a view, #systemjack's comment is correct.

SQL: Get the first value

I have two tables:
patients(ID, Firstname, Lastname, ...)
records(ID, Date, Time, Version)
I want to (inner) join these tables, so I have the records with patient data, but in the column for Version I want always the first value that was recorded for the patient (so with the minimum of date and time dependent on the patient (id)). I tried with subquery but HANA doesn't allow ORDER-BY or LIMIT clause in subqueries.
How can I implement this with SQL? (HANA SQL)
Kind regards and thanks in advance.
HANA supports window functions, so you can join against a derived table that picks the first version:
select p.*, r.id, r.date, r.time, r.version
from patients p
join (
select id, date, time, version, patient_id,
row_number() over (partition by patient_id order by version) as rn
from records
) r on p.id = r.patient_id and r.rn = 1
The above assumes that the records table has a column patient_id that contains the id of the patients table to which that record belongs to.

GROUP BY Function Issue

I have the below example:
SELECT name, age, location, SUM(pay)
FROM employee
GROUP BY location
This as expected will give me an error:
ORA-00979: not a GROUP BY expression
How can I get around this? I need to group by one maybe two columns but need to return all columns even if they're not used in the GROUP BY clause, I've looked at sub-queries to get around it but have had no luck so far.
You can use analytic functions:
SELECT name
, age
, location
, pay
, SUM(pay) over (partition by location order by location ) total
FROM employee
So, you can return all rows even if they are not used in the grouping.
So you want to know the total pay by location, and you want to know the names and ages of employees at each location? How about:
SELECT e.NAME,
e.AGE,
e.LOCATION,
t.TOTAL_LOCATION_PAY
FROM EMPLOYEE e
INNER JOIN (SELECT LOCATION,
SUM(PAY) AS TOTAL_LOCATION_PAY
FROM EMPLOYEE
GROUP BY LOCATION) t
ON (t.LOCATION = e.LOCATION)
Share and enjoy.
(Group b[http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html] Must have an aggregate function in every column that is not in the group by clause. When you are grouping, means that you want one row per group. Distinct values of the columns in the clause appear in the final result set.
This is because oracle can't know which of the values for the column that you don't have in the group by to retrieve. Consider this:
A X
B X
Select col1, col2 from myTable group by col2; -- incorrect
Select min(col1), col2 from myTable group by col2; -- correct
Why is the first incorrect? Because oracle can't know whether to retrieve A or B for the X value you have to specify it. i.e. MIN, MAX, etc.
There is an alternative to this named analytic functions that allow you to work under windows of your result set.
Now if you want total employee pay by location, and every employee you may want this.
SELECT name, age, location, SUM(pay) OVER(PARTITION BY location)
FROM employee
I believe this is better than #Bob Jarvis query as you only query the table once. Please correct me if I'm wrong. He also has employees and employee. Typo?

SQL query, distinct rows needed

I have the following table structured like this:
So basically as you can see, the department goes through name changes every couple of years. Look at number 16 for example. I want a select query that will only get the name when the date is the greatest. How do I do that?
select ID, Name from departments o
where o.thedate=
(select max(i.thedate) from departments i where o.id=i.id)
SELECT ID,
First(Name) AS FirstOfName, First(DateChange) AS FirstOfDateChange
FROM departments
GROUP BY ID
ORDER BY First(DateChange) DESC;
What is the primary key for this table? This does a subquery the same table with a name comparison.
SELECT
id,
name,
date
FROM table
WHERE name = (SELECT TOP 1 name
FROM table AS subtable
WHERE subtable.name = table.name
ORDER BY date DESC)
SELECT d.*
FROM Departments d
INNER JOIN (SELECT pk
FROM Departments
GROUP BY ID
HAVING theDate=MAX(theDate)) m ON m.pk=d.pk
WHERE [Name]="Department"