looking for query help on this. what would be the query for the below results.
List of all apartments that were occupied on March 1, 2015 sorted by complex and apartment number.
ComplexName ApartmentNumber
-----------------------------
Fox Run 101
Fox Run 102
Fox Run 204
Oak Meadows 103
Villa Maria 11
Villa Maria 12
List of all tenants that had a current lease on March 1, 2015 sorted by property and apartment number
Example results:
ComplexName ApartmentNumber GivenName FamilyName
----------------------------------------------------------
Fox Run 101 Shannon McCoy
Fox Run 102 Larry Thomas
Fox Run 204 Mark Patterson
Oak Meadows 103 Jose Ortiz
Villa Maria 11 Cassie Lee
Villa Maria 12 Robert Woodward
Sum of all rent paid in 2015 grouped by property and month
Example results:
ComplexName Month Rent Revenue
--------------------------------------
Fox Run March 1250.00
Oak Meadows March 1500.00
Oak Meadows April 700.00
Villa Maria March 1200.00
Villa Maria April 600.00
What are you table names and column names, it is difficult to give advice without these: For example if your table was called apartments and had a column occupieddate and appartment number
SELECT *
FROM Apartments
WHERE
occupieddate ='03/01/2015'
ORDER BY
Appartment number;
Could be an answer to your first question
Related
I need to create column with name(s) (Supervisors - can be multiple supervisors at the same time, but also there might not be supervisor at all) from JSON format column, that not in 2 other column with names (Employee and Client).
Id
Employee
Client
AllParticipants
1
Justin Bieber
Ariana Grande
[{"ParticipantName":"Justin Bieber"},{"ParticipantName":"Ariana Grande"}]
2
Lionel Messi
Christiano Ronaldo
[{"ParticipantName":"Christiano Ronaldo"},{"ParticipantName":"Lionel Messi"}]
3
Nicolas Cage
Robert De Niro
[{"ParticipantName":"Robert De Niro"},{"ParticipantName":"Nicolas Cage"},{"ParticipantName":"Brad Pitt"}]
4
Harry Potter
Ron Weasley
[{"ParticipantName":"Ron Weasley"},{"ParticipantName":"Albus Dumbldor"},{"ParticipantName":"Harry Potter"},{"ParticipantName":"Lord Voldemort"}]
5
Tom Holland
Henry Cavill
[{"ParticipantName":"Henry Cavill"},{"ParticipantName":"Tom Holland"}]
6
Spider Man
Venom
[{"ParticipantName":"Venom"},{"ParticipantName":"Iron Man"},{"ParticipantName":"Superman"},{"ParticipantName":"Spider Man"}]
7
Andrew Garfield
Leonardo DiCaprio
[{"ParticipantName":"Tom Cruise"},{"ParticipantName":"Andrew Garfield"},{"ParticipantName":"Leonardo DiCaprio"}]
8
Dwayne Johnson
Jennifer Lawrence
[{"ParticipantName":"Jennifer Lawrence"},{"ParticipantName":"Dwayne Johnson"}]
The output column I need:
Supervisors
NULL
NULL
Brad Pitt
Albus Dumbldor, Lord Voldemort
NULL
Iron Man, Superman
Tom Cruise
NULL
I've tried to create extra columns to use Case expression after that, but it seems too complex.
SELECT *,
JSON_VALUE(w.AllParticipants,'$[0].ParticipantName') AS ParticipantName1,
JSON_VALUE(w.AllParticipants,'$[1].ParticipantName') AS ParticipantName2,
JSON_VALUE(w.AllParticipants,'$[2].ParticipantName') AS ParticipantName3,
JSON_VALUE(w.AllParticipants,'$[3].ParticipantName') AS ParticipantName4
FROM Work AS w
I'm wondering if there is an easy way to compare values and extract only unique ones.
I have tables similar to the three below. I need to join the first two tables based on id, and then join the third table based on second name. However the last table needs a filter where the city should be equal to London unless age is empty in which case the city should equal Manchester.
I tried the code below using CASE statement but it is not working. I am new to SQL so I was not sure how can I combine a where statement with an if clause where the filter for the selection changes depending on whether there is data in a different column than the one used to filter by. The DBMS I am using Toad for Oracle.
FIRST.NAME.TABLE
ID FIRST_NAME ENTRY_DATE
1 JOHN 09/09/2019
2 NICOLA 09/09/2019
3 PATRICK 05/09/2019
4 JOAN 01/09/2019
5 JAKE 09/09/2019
6 AMELIA 01/09/2019
7 CAMERON 09/09/2019
SECOND.NAME.TABLE
ID SECOND_NAME ENTRY_DATE
1 BROWN 09/09/2019
2 SMITH 09/09/2019
3 COLE 05/09/2019
4 HOUSTON 01/09/2019
5 FARRIS 09/09/2019
6 HATHAWAY 01/09/2019
7 JONES 09/09/2019
CITY.AGE.TABLE
CITY SECOND_NAME AGE
LONDON BROWN 24.00
LONDON SMITH
MANCHESTER COLE 30.00
MANCHESTER HOUSTON 66.00
LONDON FARRIS
LONDON HATHAWAY 32.00
GLASGOW JONES 28.00
MANCHESTER SMITH 32.00
LONDON FARRIS 62.00
SELECT FN.ID,
FN.FIRST_NAME,
SN.SECOND_NAME,
AC.CITY,
AC.AGE
FROM FIRST.NAME.TABLE AS FN
INNER JOIN SECOND.NAME.TABLE SN
ON FN.ID=SN.ID
INNER JOIN CITY.AGE.TABLE AS CA
ON SN.SECOND NAME=AC.SECOND_NAME
WHERE FN.ENTRY_DATE='09-SEP-19'
AND SN.ENTRY_DATE='09-SEP-19'
AND (CASE WHEN AC.CITY='LONDON' AND AC.AGE IS NOT NULL
THEN AC.CITY='LONDON'
ELSE AS.CITY='MANCHESTER' END)
You can express this as boolean logic:
WHERE FN.ENTRY_DATE = DATE '2019-09-09' AND
SN.ENTRY_DATE = DATE '2019-09-09' AND
(AC.AGE IS NOT NULL AND AC.CITY = 'LONDON' OR
AC.AGE IS NULL AND AC.CITY = 'MANCHESTER'
)
This answers your question about how to implement the logic using SQL. However, I'm not sure that is the logic that you really want. I speculate that you really want a LEFT JOIN to the age table.
So I'm new to this site because I just recently started a data basing class, so I'm still learning the basics, but I need a little bit of help. So these are the two problems I have.
List of all apartments that were occupied on March 1, 2015 sorted by complex and apartment number. Example Results:
complexName apartmentNumber
Fox Run 101
Fox Run 102
Fox Run 204
Oak Meadows 103
Villa Maria 11
Villa Maria 12
List of all tenants that had a current lease on March 1, 2015 sorted by property and apartment number
Example results:
complexName apartmentNumber givenName familyName
Fox Run 101 Shannon McCoy
Fox Run 102 Larry Thomas
Fox Run 204 Mark Patterson
Oak Meadows 103 Jose Ortiz
Villa Maria 11 Cassie Lee
Villa Maria 12 Robert Woodward
My SQL for the first problem is...
SELECT DISTINCT name AS 'complexName', number AS 'apartmentNumber'
FROM week9wildwood.Complex AS c
INNER JOIN week9wildwood.Apartment AS a ON c.complexID = a.Complex_complexID
INNER JOIN week9wildwood.Lease AS l ON a.number = l.Apartment_number
WHERE startDate BETWEEN '2015-03-01' AND '2016-03-01'
ORDER BY name, number;
But I keep getting this back...
complexName apartmentNumber
Fox Run 102
Fox Run 103
Fox Run 104
Oak Meadows 102
Oak Meadows 103
Villa Maria 11
Villa Maria 21
I'm not sure what I'm doing wrong, and why it's coming back with different data. I also feel like the querie for the first problem, is almost the same for the second problem, but the wording of it has me hesitant. Any suggestions would be greatly appreciated!
Untested but maybe just make sure the end date is greater than March 15, 2015
select name AS 'complexName', number AS 'apartmentNumber'
from week9wildwood.Complex AS c
inner join
week9wildwood.Apartment AS a
on c.complexID = a.Complex_complexID
inner join
week9wildwood.Lease AS l
on a.number = l.Apartment_number
where end_date > 2015-03-15
ORDER BY name, number;
Have you tried Group By?
SELECT DISTINCT name AS 'complexName', number AS 'apartmentNumber'
FROM week9wildwood.Complex c
INNER JOIN week9wildwood.Apartment a
ON c.complexID = a.Complex_complexID
INNER JOIN week9wildwood.Lease l
ON a.number = l.Apartment_number
WHERE startDate BETWEEN '2015-03-01' AND '2016-03-01'
GROUP BY 1
ORDER BY name, number;
I am creating a database for a library with the following four tables.
Table 1 - book
isbn title author
111-2-33-444444-5 Pro JavaFX Dave Smith
222-3-44-555555-6 Oracle Systems Kate Roberts
333-4-55-666666-7 Expert jQuery Mike Smith
Table 2 - copy
code isbn duration
1011 111-2-33-444444-5 21
1012 111-2-33-444444-5 14
1013 111-2-33-444444-5 7
2011 222-3-44-555555-6 21
3011 333-4-55-666666-7 7
3012 333-4-55-666666-7 14
Table 3 - student
no name school embargo
2001 Mike CMP No
2002 Andy CMP Yes
2003 Sarah ENG No
2004 Karen ENG Yes
2005 Lucy BUE No
Table 4 - loan
code no taken due return
1011 2002 2015.01.10 2015.01.31 2015.01.31
1011 2002 2015.02.05 2015.02.26 2015.02.23
1011 2003 2015.05.10 2015.05.31
1013 2003 2014.03.02 2014.03.16 2014.03.10
1013 2002 2014.08.02 2014.08.16 2014.08.16
2011 2004 2013.02.01 2013.02.22 2013.02.20
3011 2002 2015.07.03 2015.07.10
3011 2005 2014.10.10 2014.10.17 2014.10.20
I have been looking into working out the frequency of book loans throughout this database. I am looking for create a query that will show me the frequency of all books that have been loaned 2 or more times.
I understand that I would have to take 'code' from loan and find rows with the same code, but I am completely lost on how to work out the frequency. Could anyone help me out here?
The project I'm asking about is for sending an email to teachers asking what books they're using for the classes they're teaching next semester, so that the books can be ordered. I have a query that compares the course number of this upcoming semester's classes to the course numbers of historical textbook orders, pulling out only those classes that are being taught this semester. That's where I get lost.
I have a table that contains the following:
Professor
Course Number
Year
Book Title
The data looks like this:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 12 1111 Pride and Prejudice
smith 11 1222 Infinite Jest
smith 10 1333 The Bible
smith 13 1333 The Bible
smith 12 1222 The Alchemist
smith 10 1111 Moby Dick
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 10 1333 Everything is Illuminated
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
johnson 10 1333 Great Expectations
johnson 9 1222 Proust on the Shore
Here's what I need the code to do "on paper":
Group the records by professor. Determine every unique course number in that group, and group records by course number. For each unique course number, determine the highest year associated. Then spit out every record with that professor+course number+year combination.
With the sample data, the results would be:
professor year course number title
--------- ---- ------------- -------------------
smith 13 1111 Pride and Prejudice
smith 13 1111 The Fountainhead
smith 13 1222 The Alchemist
smith 13 1333 The Bible
johnson 12 1222 The Tipping Point
johnson 11 1333 Anna Kerenina
johnson 12 1222 The Savage Detectives
johnson 11 1333 In Search of Lost Time
I'm thinking I should make a record set for each teacher, and within that, another record set for each course number. Within the course number record set, I need the system to determine what the highest year number is - maybe store that in a variable? Then pull out every associated record so that if the teacher ordered 3 books the last time they taught that class (whether it was in 2013 or 2012 and so on) all three books display. I'm not sure I'm thinking of record sets in the right way, though.
My SQL so far is basic and clearly doesn't work:
SELECT [All].Professor, [All].Course, Max([All].Year)
FROM [All]
GROUP BY [All].Professor, [All].Course;
Use your query as a subquery and INNER JOIN it back to the [ALL] table to filter the rows.
SELECT
a.Professor,
a.Year,
a.Course,
a.title
FROM
[ALL] AS a
INNER JOIN
(
SELECT [All].Professor, [All].Course, Max([All].Year) AS MaxOfYear
FROM [All]
GROUP BY [All].Professor, [All].Course
) AS sub
ON
a.Professor = sub.Professor
AND a.Course = sub.Course
AND a.Year = sub.MaxOfYear;