counting in sql in subquery in the table - oracle10g

DNO DNAME
----- -----------
1 Research
2 Finance
EN ENAME CITY SALARY DNO JOIN_DATE
-- ---------- ---------- ---------- ---------- ---------
E1 Ashim Kolkata 10000 1 01-JUN-02
E2 Kamal Mumbai 18000 2 02-JAN-02
E3 Tamal Chennai 7000 1 07-FEB-04
E4 Asha Kolkata 8000 2 01-MAR-07
E5 Timir Delhi 7000 1 11-JUN-05
//find all departments that have more than 3 employees.
My try
select deptt.dname
from deptt,empl
where deptt.dno=empl.dno and (select count(empl.dno) from empl group by empl.dno)>3;

here is the solution
select deptt.dname
from deptt,empl
where deptt.dno=empl.dno
group by deptt.dname having count(1)>3;

select
*
from departments d
inner join (
select dno from employees group by dno having count(*) > 3
) e on d.dno = e.dno
There are many approaches to this problem but almost all will use GROUP BY and the HAVING clause. That clause allows you to filter results of aggregate functions. Here it is used to choose only those records where the count is greater than 3.
In the query structure used above the group by is handled on the employee table only, then the result (which is known as a derived table) is joined by an INNER JOIN to the departments table. This inner join only allows matching records so this has the effect of filtering the departments table to only those which have a count() of greater than 3.
An advantage of this query structure is fewer records are joined, and also that all columns of the departments table are available for reporting. Disadvantage of this structure is the the count() of employees per department isn't visible.

Related

How to simplify a join of 2 tables in HIVE and count values

I have two tables in HIVE, "orders" and "customers". I want to get top n user names of users who placed most orders (in status "CLOSED"). Orders table has key order_customer_id, column order_status and customers has key customer_id and name consists of 2 columns customer_fname and customer_lname.
ORDERS
order_customer_id, order_status
1,CLOSED
2,CLOSED
3,INPROGRESS
1,INPROGRESS
1,CLOSED
2,CLOSED
CUSTOMERS
customer_id, customer_fname, customer_lname
1,Mickey, Mouse
2,Henry, Ford
3,John, Doe
I tried this code:
select c.customer_id, count(o.order_customer_id) as COUNT, concat(c.customer_fname," ",c.customer_lname) as FULLNAME from customers c join orders o on c.customer_id=o.order_customer_id where o.order_status='CLOSED' group by c.customer_id,FULLNAME order by COUNT desc limit 10;
this does not work - returns error.
I was able to get the result by first creating a 3rd table:
create table id_sum as select o.order_customer_id,count(o.order_id) as COUNT from orders o join customers c on c.customer_id=o.order_customer_id where order_status='CLOSED' group by o.order_customer_id;
1833 6
5493 5
1363 5
1687 5
569 4
1764 4
1345 4
Then I joined the tables:
select s.*,concat(c.customer_fname," " ,c.customer_lname) from id_sum s join customers c on s.order_customer_id = c.customer_id order by count desc limit 20;
This resulted in desired output:
customer_id, order_count, full_name
1833 6 Ronald Smith
5493 5 Mary Cochran
1363 5 Kathy Rios
1687 5 Jerry Ellis
569 4 Mary Frye
1764 4 Megan Davila
1345 4 Adam Wilson
Is there a way how to write it in one command or more effectively?
The subquery with alias sq creates a relation with two columns order_count and customer_id calculating for each customer_id the total number of orders. This is then joined with the CUSTOMERS table. The result is sorted descending and limited to (the top) 10 rows.
SELECT c.customer_id, sq.order_count, concat(c.customer_fname," " ,c.customer_lname) as full_name
FROM CUSTOMERS c JOIN (
SELECT COUNT(*) as order_count, order_customer_id FROM ORDERS
WHERE order_status = 'CLOSED'
GROUP BY order_customer_id
) sq on c.customer_id = sq.order_customer_id
ORDER BY sq.order_count desc LIMIT 10
;
The idea is to use a subquery instead of a third table.

can you helpe me to display the latest data on each group

I have this datatables:
table1
id category
-------------
1 a
2 b
3 c
table2
id heading category_id
----------------------
1 name 1
2 adddress 2
3 phone 3
4 email 1
I want to group this table and display the latest data for that the following query was I used:
SELECT news.id,news.image,news.heading,news.description,
news.date,news.category_id,categories.category
FROM `news`
INNER JOIN categories On news.category_id=categories.id
group by category_id
But I didnt get the latest data that I entered.
Try the query below:
SELECT *
FROM table2 AS tb2 LEFT JOIN table1 AS tb1 ON tb2.category_id = tb1.id
ORDER BY tb1.id
GROUP BY tb2.category_id

Postgres how to maintain order of rows using CTEs

I have 2 tables
students:
id | name | age
1 abc 20
2 xyz 21
scores:
id | studentid | marks
1 1 20
2 2 22
3 2 20
4 1 22
5 1 20
where studentid is foreign key to students table
When a do
select studentid
from scores
where marks=20;
I get the following result
1, 2, 1
But if want the name of the student name and when I do a join using
select t1.name
from students t1
inner join scores t2 on t1.id = t2.studentid
where t2.marks=20;
I get xyz,abc,abc Though the ouput is correct is there any way I can maintain the order in which scores are listed in the scores table? I should get abc,xyz,abc as output. I tried using subquery as well
SELECT name
FROM students
WHERE ID IN ( select studentid from scores where marks=20) ;
but that also did not give me correct order. How can this be achieved using CTEs (common table expressions)? I tried the follownig cte but it did not work
with cte as(
select t2.id, t1.name
from students t1
inner join scores t2 on t1.id = t2.studentid
where t2.marks=20)
select name from cte order by id
You can order by a column not present in select list:
select t1.name
from students t1
inner join scores t2 on t1.id = t2.student_id
where t2.marks=20
order by t2.id;
name
------
abc
xyz
abc
(3 rows)

Limit for inner Join Table

I have a scenario where I am joining three tables and getting the results.
My problem is i have apply limit for joined table.
Take below example, i have three tables 1) books and 2) Customer 3)author. I need to find list of books sold today with author and customer name however i just need last nth customers not all by passing books Id
Books Customer Authors
--------------- ---------------------- -------------
Id Name AID Id BID Name Date AID Name
1 1 1 ABC 1 A1
2 2 1 CED 2 A2
3 3 2 DFG
How we can achieve this?
You are looking for LATERAL.
Sample:
SELECT B.Id, C.Name
FROM Books B,
LATERAL (SELECT * FROM Customer WHERE B.ID=C.BID ORDER BY ID DESC LIMIT N) C
WHERE B.ID = ANY(ids)
AND Date=Current_date

AdventureWorks SQL conflicting results issue

I'm working with the AdventureWorks example DB - we're running SQL Server 2008R2, so I assume that's the edition of AdventureWorks (I have read-only access). I'm trying to get a list of sales managers so that I can then determine a couple employee/manager relationships.
I'm getting two sets of three differently named people, with the same job title, with their CurrentFlag set to 1 (active) with slightly different queries. I do notice that one result group has the same contactID and employeeID, but I'm not sure what this may indicate.
So the question is: Why am I getting completely different results with these two queires? I would think I'd get six results for each - the queries are matching employee table Titles.
SQL Query 1:
select
c.FirstName,
c.LastName,
c.ContactID,
e.EmployeeID,
e.Title,
c.Title,
e.CurrentFlag
from Person.Contact c
inner join HumanResources.Employee e
on c.ContactID = e.ContactID
where
e.Title like '%Sales Manager%'
SQL Query 2:
SELECT
e.EmployeeID,
(c.FirstName + ' ' + c.LastName) as 'First Name and Last Name',
e.Title
FROM HumanResources.Employee e
INNER JOIN Person.Contact c
ON e.EmployeeID = c.ContactID
Where
e.Title LIKE '%Manager%'
AND
e.Title LIKE '%Sales%'
ORDER BY e.EmployeeID;
UPDATE: These are my results:
SQL Query 1:
------- ------- ---- --- ---------------------------- ---- --
Stephen Jiang 1011 268 North American Sales Manager NULL 1
Amy Alberts 1013 284 European Sales Manager NULL 1
Syed Abbas 1012 288 Pacific Sales Manager Mr. 1
SQL Query 2:
--- --- ----------- ---------------------------- --- --
268 268 Gary Drury North American Sales Manager Mr. 1
284 284 John Emory European Sales Manager Mr. 1
288 288 Julie Estes Pacific Sales Manager Ms. 1
The only diffrents i can see is this:
where
e.Title like '%Sales Manager%'
And this:
Where
e.Title LIKE '%Manager%'
AND
e.Title LIKE '%Sales%'
The first query says that bring me all titles that has '%Sales Manager%' you can have for ex this output:
Account Sales Manager
some Sales Manager
Sales Manager something else
The second question says bring me all the titles that has '%Manager%' and '%Sales%' so you can for ex have:
Sales Account Manager
some Sales some Manager some
Sales Manager some else thing
Manager Sales
And this join can not be corrent
INNER JOIN Person.Contact c
ON e.EmployeeID = c.ContactID
Don't you mean:
INNER JOIN Person.Contact c
ON e.ContactID= c.ContactID
The first query will match the rows where substring "Sales Manager" is present. But second one can match rows like "Managers of Sales Dep" as well. I mean the second doesn't care about positions of the words in the srting.
I believe that the results of first query is a subset of the results of second one.
UPDATE
You use different columns in JOIN clause, so it's normal that you got different results.