Trouble with SQL query using COUNT - count

list days appearing in reservation where only red
boats have been reserved using COUNT aggregate function.
Here is the table
servant table:
sname rating
Joe 4
Bob 2
Tim 9
Mike 1
Lewis 5
boat table:
bname color rating
Ace orange 6
Bethany red 5
Cruiser green 9
WindySea red 8
reservation table:
sname bname day
Bob Ace Monday
Bob Bethany Wednesday
Bob WindySea Saturday
Tim Ace Sunday
Tim Bethany Wednesday
Tim Cruiser Wednesday
Mike Ace Monday

SELECT r.day
FROM reservation r
JOIN boat b
ON r.bname = b.bname
GROUP BY r.day
HAVING COUNT(CASE WHEN b.color <> 'red' THEN 1 ELSE NULL END) = 0
AND COUNT(CASE WHEN b.color = 'red' THEN 1 ELSE NULL END) > 0 -- optional

Related

Lead function group by in oracle

I want to group by lead function by two column. Here is my table data.
Id Name_Id Name Item_Id Item_Name date
1 1 Car 1 SUV 1-Jan-2015
2 1 Car 1 SUV 12-March-2015
3 1 Car 1 SUV 20-April-2015
4 1 Car 2 Sport 23-April-2015
5 2 Bike 1 SUV 18-July-2015
6 2 Bike 1 SUV 20-Aug-2015
7 2 Bike 2 Sport 18-Sept-2015
8 2 Bike 3 Honda 20-OCT-2015
And I need result from above table like.
Id Name_Id Name Item_Id Item_Name start date end date
1 1 Car 1 SUV 1-Jan-2015 20-April-2015
2 1 Car 2 Sport 20-April-2015 23-April-2015
3 2 Bike 1 SUV 18-July-2015 20-Aug-2015
4 2 Bike 2 Sport 20-Aug-2015 18-Sept-2015
5 2 Bike 3 Honda 18-Sept-2015 20-OCT-2015
Any suggestion really appreciated.
I don't think you need to use LEAD here. The CTE below computes, for each Item_Id, the earliest and latest date. This is then joined to your original table to restrict to records corresponding to the earliest Item_Id. At the same time, the end date is also pulled in during the join.
WITH cte AS (
SELECT Name,
Item_Id,
MIN(date) AS start_date,
MAX(date) AS end_date
FROM yourTable
GROUP BY Name, Item_Id
)
SELECT t1.Id, t1.Name_Id, t1.Name, t1.Item_Id, t1.Item_Name,
t2.start_date,
t2.end_date
FROM yourTable t1
INNER JOIN cte t2
ON t1.Item_Id = t2.Item_Id AND
t1.Name = t2.Name AND
t1.date = t2.start_date

SQLite - SELECT DISTINCT of one column and get the others

This has been touched on before on this website. I want distinct on group but I also want to get the other fields too. what I need is the lowest id of each group, but instead I get the highest. I've tried variod SQL queries and the nearest 2 that work are
1)
select *
from reminder
group by Eventgroup
order by autoid
2)
SELECT distinct Autoid,EventDate,Subject,birthdate,Eventgroup
from reminder
group by Eventgroup
order by autoid
Data:
EventDate Subject birthdate Eventgroup autoid
09/10/2017 Joes Birthday 09/10/1995 4 9
13/07/2017 Bill Birthday 13/07/1999 2 8
04/04/2017 Tony Birthday 04/04/1993 3 7
09/10/2016 Joes Birthday 09/10/1995 4 6
13/07/2016 Bill Birthday 13/07/1999 2 5
04/04/2016 Tony Birthday 04/04/1993 3 4
09/10/2015 Joes Birthday 09/10/1995 4 3
13/07/2015 Bill Birthday 13/07/1999 2 2
04/04/2015 Tony Birthday 04/04/1993 3 1
both of these queries return
09/10/2017 Joes Birthday 09/10/1995 4 9
13/07/2017 Bill Birthday 13/07/1999 2 8
04/04/2017 Tony Birthday 04/04/1993 3 7
what I want is the earliets dates such as
09/10/2015 Joes Birthday 09/10/1995 4 3
13/07/2015 Bill Birthday 13/07/1999 2 2
04/04/2015 Tony Birthday 04/04/1993 3 1
Join the table with a subquery that finds the earliest date for each event group.
SELECT a.*
FROM reminders a
JOIN (SELECT eventgroup, MIN(eventdate) mindate
FROM reminders
GROUP BY eventgroup) b
ON a.eventgroup = b.eventgroup AND a.eventdate = b.mindate
This is the same structure as the second query in this answer in the duplicate question.
DEMO

How do I substitute values from a second table (multiple times) in a single query in Oracle SQL/APEX

Suppose I have an employee table “DB_EMPLOYEE” with the following values for EMPID and NAME
EMPID NAME
0 Bob
1 Joe
2 Carl
3 Wendy
Next, I have another table listing audits, “ICQA_AUDITS” with the following values for the columns RECORD_ID, AUDITOR_ID, PACKER_ID, SHIPPER_ID
RECORD_ID AUDITOR_ID PACKER_ID SHIPPER_ID
0 0 1 3
1 1 2 3
2 3 1 2
I can write the following query to get the list of audits with the auditor name substituted for the AUDITOR_ID:
SELECT
emp.NAME,
aud.PACKER_ID,
aud.SHIPPER_ID
FROM
ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
aud.AUDITOR_ID = emp.EMPID
The formatted output would look something like this:
RECORD_ID NAME PACKER_ID SHIPPER_ID
0 Bob 1 3
1 Joe 2 3
2 Wendy 1 2
My question is this: How might I also get the PACKER_ID and SHIPPER_ID replaced by the appropriate names? Desired output would be the following:
RECORD_ID NAME PACKER_ID SHIPPER_ID
0 Bob Joe Wendy
1 Joe Carl Wendy
2 Wendy Joe Carl
I'm trying to format a report region in APEX and I'm not seeing the easy way to do this. Any help will be much appreciated.
Thanks!
Edit
Expanded example: Suppose that instead of just having one NAME attribute, the DB_EMPLOYEE table has FIRSTNAME and LASTNAME, as follows:
EMPID FIRSTNAME LASTNAME
0 Bob Bobbington
1 Joe Josephson
2 Carl Carlton
3 Wendy Van Dorfenstein
I can write the example query as:
SELECT
emp.LASTNAME || ', ' || emp.FIRSTNAME as "Auditor Name",
aud.PACKER_ID,
aud.SHIPPER_ID
FROM
ICQA_AUDITS aud, DB_EMPLOYEE emp
WHERE
aud.AUDITOR_ID = emp.EMPID
The output would then be:
RECORD_ID Auditor Name PACKER_ID SHIPPER_ID
0 Bobbington, Bob 1 3
1 Josephson, Joe 2 3
2 Van Dorfenstein, Wendy 1 2
The desired output would then be:
Record Auditor Name Packer Name Shipper Name
0 Bobbington, Bob Josephson, Joe Van Dorfenstein, Wendy
1 Josephson, Joe Carlton, Carl Van Dorfenstein, Wendy
2 Van Dorfenstein, Wendy Josephson, Joe Carlton, Carl
Try this query:
SELECT
a.RECORD_ID as id,
b.name as auditor_name,
c.name as packer_name,
d.name as shiper_name
FROM ICQA_AUDITS a
LEFT JOIN DB_EMPLOYEE b ON b.EMPID = a.AUDITOR_ID
LEFT JOIN DB_EMPLOYEE c ON c.EMPID = a.PACKER_ID
LEFT JOIN DB_EMPLOYEE d ON d.EMPID = a.SHIPPER_ID
WHERE a.RECORD_ID = 1
The response must be like this:
id auditor_name packer_name shiper_name
1 Joe Carl Wendy
Change "WHERE" statement to your needs. Good luck.

How to get the last two rows

The table is
I need to get the last two event associates for each event
event_id event_date event_associate
1 2/14/2014 ben
1 2/15/2014 ben
1 2/16/2014 steve
1 2/17/2014 steve // this associate is the last but one for event 1
1 2/18/2014 paul // this associate is the last for event 1
2 2/19/2014 paul
2 2/20/2014 paul // this associate is the last but one for event 2
2 2/21/2014 ben // this associate is the last for event 2
3 2/22/2014 paul
3 2/23/2014 paul
3 2/24/2014 ben
3 2/25/2014 steve // this associate is the last but one for event 3
3 2/26/2014 ben // this associate is the last for event 3
I need to find out who was the last but one event_associate for each event . The result should be
event_id event_associate rn
1 steve 2
1 paul 1
2 paul 2
2 ben 1
3 steve 2
3 ben 1
I tried
SELECT t.* , ROW_NUMBER() OVER (PARTITION BY event_associate ORDER BY event_date DESC) rn
FROM mytable t
QUALIFY rn < 3
"for each event" -> PARTITION BY event_id
"last but one" -> ORDER BY event_date DESC
you have to count the number of row in your data base and then use this query
sql=sql+" LIMIT "+NumberOfRowsToShowInTables+" OFFSET "+(countrow- NumberOfRowsToShowInTables);
where countrow is he number of row in your database
NumberOfRowsToShowInTables equal 2 as you mentioned
sql is your normal query without limitation

How to find the second highest salary grouped by the business in r

what i want is the output should contain for each business the second highest salary entry only....
for example:
customer_id name sales firstname lastname income business
6 Priyank Dwivedi 2 Priyank Dwivedi 650000 PES
4 Monika Maurya 3 Monika Maurya 200000 ITS
1 Rahul Ranjan 3 Rahul Ranjan 1000000 PES
7 Ambreen Khan 1 Ambreen Khan 800000 PES
3 P Paul 3 P Paul 500000 ITS
5 Sunny Tiwari 2 Sunny Tiwari 900000 Analytics
2 Mayank Agarwal 3 Mayank Agarwal 300000 PES
8 Shashank Rawat 1 Shashank Rawat 100000 Analytics
What I want as output is:
customer_id name sales firstname lastname income business
4 Monika Maurya 3 Monika Maurya 200000 ITS
8 Shashank Rawat 1 Shashank Rawat 100000 Analytics
7 Ambreen Khan 1 Ambreen Khan 800000 PES
that is second highest salary from each business.
... one solution might be:
res <- t(sapply(unique(data[, "business"]),
function(x, data){
# this are the subsets
d <- data[x==data[, "business"], ]
# order it and take second
d[order(d[, "income"], decreasing=TRUE)[2], ]
}, data=data))
res
with data as your data

Resources