BIRT Report, SQL - sql

I am new to BIRT, and I guess my question is very easy, but I can’t see how to accomplish my objective…
The task is to create a report for different courts on their requests containing different information about courts’ employees. DataSetRow[“COURT”] represents the heading of the report and it changes according to employees’ courts, usually it changes its title alphabetically, it depends on results I get and a court which requests that information. I need to make it dependent on a USER who needs the report. For this purpose I need PERSONAL_ID columns from USER_ID and HR to match and therefore DataSetRow[“COURT”] should become the same number which COURT column has. I have no idea how to define this for a USER…
Report Parameters:
DATA_FORM,
USER_ID
There are two important tables in the database:
USERS: which contains USER_ID column and PERSONAL_ID column.
And HR: The same PERSONAL_ID and C_COURT, which I need.
SELECT
H.PERSONAL_ID,
SURNAME||' '||NAME||' '||SNAME AS NAME,
DECODE_UNI (H.T_COURT, H.C_COURT) AS COURT,
DECODE_UNI (HA.T_COURT, HA.C_COURT) AS C_COURT,
TO_CHAR (TO_DATE(BIRTH_DATE, 'yyyymmdd'),'yyyy') AS BIRTH_DATE,
DECODE_UNI (HA.T_POSITION, HA.C_POSITION) AS POSITION,
DECODE_UNI (HA.T_DEPARTMENT, HA.C_DEPARTMENT) AS DEPARTMENT,
DECODE_UNI (HW.T_AWARD_TYPE, HW.C_AWARD_TYPE) AS AWARD_TYPE,
TO_CHAR (HW.AWARD_DATE, 'dd.mm.yyyy') AS AWARD_DATE,
HW.AWARD_DESC,
U.PERSONAL_ID AS PERSONAL_USER
FROM
HR H, HR_APPOINTMENT HA, HR_AWARD HW, USERS U
WHERE
H.PERSONAL_ID = HA.PERSONAL_ID
AND H.PERSONAL_ID = HW.PERSONAL_ID
AND HA.ACTIVE = 1
AND H.C_STATE = 1
AND HA.STATUS_LAST = 1
AND H.REC_DATE <= TO_DATE (?, 'dd.mm.yyyy')
AND USER_ID = ?
Thank you in advance!

It's complicated to answer without having the whole picture, but as you described the case it seems a join is missing in the query:
AND H.PERSONAL_ID=U.PERSONAL_ID

Related

SQL: nested query with tuple constructor

I have some difficulties dealing with an SQL exercise for my Intro to Database course. The SQL standard we mainly use is the Oracle one (the one compatible with Apex).
I have the following SQL database (primary keys bold):
TEENAGER(SSN, Name, Surname, BirthDate, CityOfResidence, Sex)
ACTIVITY(ActivityCode, AName, Description, Category)
SUMMER-CAMP(CampCode, CampName, City)
SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP(SSN,ActivityCode, CampCode,
SubscriptionDate)
This is what the exercise asks:
"For each teenager, born before 2005, who subscribed to activities
organized by at least 5 different summer camps, show name, surname,
birth date of the teenager and the name of each summer camp to which
the teenager subscribed to all the different activities organized by
the camp."
I do not have any problem finding the SSNs of the teenagers born before 2005 and who subscribed to at least 5 camps and I am able to find the number of different activities organized by the camp. How do I manage to use this information to find the final result?
Now, this is my attempt to a solution (I added two in-line comments with "#" for clarity):
FROM TEENAGER T, SUMMER-CAMP SC, SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP STAISC
WHERE T.SSN = STAISC.SSN AND STAISC.CampCode = SC.CampCode
AND SSN IN (SELECT T.SSN #born before 2005 and at least 5 camps
FROM TEENAGER T, SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP STAISC
WHERE T.BirthDate < TO_DATE('01/01/2005', 'DD/MM/YYYY')
AND T.SSN = STAISC.SSN
GROUP BY T.SSN
HAVING COUNT(DISTINCT STAISC.CampCode) > 4)
GROUP BY STAISC.CampCode, T.SSN
HAVING (STAISC.CampCode, COUNT(DISTINCT ActivityCode)) IN (SELECT CampCode, COUNT(DISTINCT ActivityCode) #number of activities in camps
FROM SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP
GROUP BY CampCode)```
As you can see, I am using a tuple constructor in the outer-most query in a HAVING clause to try and use the information about the total number of activities organised in a camp. Am I allowed to do that and would it work? (The professor did not give us any database since in the exam we will have to write down the query without being able to run it).
Thanks in advance!
I answer to my own question since I found a correct solution:
SELECT T.SSN, SC.CampCode, T.Name, T.Surname, T.BirthDate, SC.CampName
FROM TEENAGER T, SUMMER-CAMP SC, SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP STAISC
WHERE BirthDate < TO_DATE('01/01/2005', 'DD/MM/YYYY')
AND T.SSN = STAISC.SSN AND STAISC.CampCode = SC.CampCode
AND T.SSN IN(SELECT SSN FROM SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP
GROUP BY SSN
HAVING COUNT(DISTINCT CampCode))
GROUP BY T.SSN, SC.CampCode
HAVING COUNT(DISTINCT STAISC.ActivityCode) = (SELECT COUNT(ActivityCode)
FROM SUBSCRIPTION-TO-ACTIVITY-IN-SUMMER-CAMP STAISC2
WHERE STAISC.CampCode = STAISC2.CampCode)

SQL Query Removing Dups

Using GoldMine CRM and wrote this query to try and find out people in our system based on a record Type, Lead Creation Date and Last Contact Date. Now when running the query it brings up multiple results say for John Smith it is showing me all of his information rather than just the last contacted date which I want. I'll paste the query below any help would be much appreciated.
SELECT DISTINCT CONTACT1.CONTACT AS Contact_Name,
Min(CONTACT1.CONTACT),
CONTACT1.KEY1 AS Rec_Type,
CONTACT1.CREATEON AS Lead_Type,
CONTHIST.LASTDATE AS Last_Contact,
CONTHIST.RESULTCODE AS Stamp
FROM CONTACT1
INNER JOIN CONTHIST
ON CONTACT1.ACCOUNTNO = CONTHIST.ACCOUNTNO
WHERE CONTACT1.KEY1 = 'Lead'
GROUP BY CONTACT1.CONTACT,
CONTACT1.KEY1,
CONTACT1.CREATEON,
CONTHIST.LASTDATE,
CONTHIST.RESULTCODE
HAVING Count(CONTACT1.CONTACT) = 1
ORDER BY CONTACT1.CREATEON

SQL query: confusing query

I have the following tables:
Flights(flight_num, source_city, dest_city)
Departures(flight_num, date, plane_type)
Passengers(passenger_id, passenger_name, passenger_address)
Bookings(passenger_id, flight_num, date, seat_number)
And I want to find the number of departures for each type of plane for all flights that leave from Burbank. (Make sure the plane¬¬_type is also part of the result.)
So far I have
SELECT D.plane_type, COUNT(*)
FROM Departures D, Flights F
WHERE F.source_city = “Burbank”
AND F.flight_num = D.flight_num
GROUP BY D.plane_type
But I am not sure how to incorporate the fact that the planes might change for the same flight number?
You can not achieve that behaviour if yours plane_type changes with no rules.

Need to pull only last date in table that stores change dates SQL / ODBC

Hope somebody can help me with this. I'm trying to pull a list of forthcoming titles (I work in publishing) via ODBC/ms query. I want (amongst other things) to show their internal status (approved, prepress etc.). The database stores the change dates for the status'. I seem to be getting one line per status per title. So if the title has changed status 6 times, I will get 6 lines. But I only want to show the latest status...
The date is in BL_PROJECT_TO_STATUS.STATUS_DATE (I've inserted a date criteria beneath, just to make it more visible).
How can this be done? I'm very new to ODBC and would appreciate it a lot.
SELECT DISTINCT
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL,
MEDIATYPE.DESCRIPTION, BL_PROJECT_STATUS.DESCRIPTION
FROM
FIRMA1.BL_PROJECT BL_PROJECT, FIRMA1.BL_PROJECT_STATUS BL_PROJECT_STATUS,
FIRMA1.BL_PROJECT_TO_STATUS BL_PROJECT_TO_STATUS, FIRMA1.MEDIATYPE MEDIATYPE
WHERE
BL_PROJECT.PROJECT_ID = BL_PROJECT_TO_STATUS.PROJECT_ID AND
BL_PROJECT_TO_STATUS.STATUS_ID = BL_PROJECT_STATUS.CODE AND
BL_PROJECT.MEDIATYPE = MEDIATYPE.ID AND
((BL_PROJECT.PROJECT_TYPE = 2) AND
(BL_PROJECT.EXP_PUB_DATE Between SYSDATE AND (SYSDATE+90)) AND
(BL_PROJECT_TO_STATUS.STATUS_DATE = {ts '2013-11-20 00:00:00'}))
ORDER BY
BL_PROJECT.EXP_PUB_DATE, BL_PROJECT.EAN, BL_PROJECT.TITEL
Here is the general idea. You can adapt it with your table and field names.
select somefields
from sometables
join
(select something, max(datetimefield) maxdt
from table1
where whatever
group by something ) temp on table1.datetimefield = maxdt
etc

sql queries to show the most popular record

I have four tables
Car (car_ registration_no, class, type_code)
Rental_history (rent_date, car_registration_no, rent_location_code, return_location_code)
Type (type_code, make, model)
Location (location_code, branch_name)
I need a query to show the most popular car rented by location.
I need a query to show the total rentals at each location for the previous month?
My code so far is as follows, but I couldn't complete it:
SELECT car.class, car.type_code , type.make, type.model
FROM car , type, rental_history
where rental_history.car_registration_no = car.car_registration_no
and car.type_code = type.type_code
You will need to join the tables and calculate the numbers. Let's start off with an easier query to point you in the right direction.
This will show you how many times a "type_code" car has been rented per location (untested, may contain errors)
SELECT
count(car.car_registration_no) as rental_num,
car.type_code,
rental_history.rent_location_code
FROM car
LEFT JOIN rental_history ON (rental_history.car_registration_no = car.car_registration_no)
GROUP BY car.type_code, rental_history.rent_location_code;
I'm using a left join here because there may be cars that have not been rented and won't have any history. Instead of not showing up, you will have a "0" for number of rentals.
Edit:
For the second query it's actually very straightforward. You need to group by location, filter on date and use COUNT (again, untested):
SELECT
count(rental_history.car_registration_no) as rental_num,
rental_history.rent_location_code
FROM rental_history
WHERE rent_date >= '2012-03-01' AND rent_date < '2012-04-01'
GROUP BY rental_history.rent_location_code;
Join All the table and use count..!