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..!
Related
I have a weird question - I understand that Joins return matching data based on the 'ON' stipulation, however the problem I am facing is I need the Business date back for both tables but at the same time i need to join on the date in order to get the totals correct
See below code:
Select
o.Resort,
o.Business_Date,
Occupied,
Comps,
House,
ADR,
Room_Revenue,
Occupied-(Comps+House) AS DandT,
Coalesce(gd.Projected_Occ1,0) AS Projected_Occ1,
Occupied-(Comps+House)+Coalesce(gd.Projected_Occ1,0) as Total
from Occupancy o
left join Group_Details_HF gd
on o.Business_Date = gd.Business_Date
and o.Resort = gd.resort
UNION ALL
select
o.Resort,
o.Business_Date,
Occupied,
Comps,
House,
ADR,
Room_Revenue,
Occupied-(Comps+House) AS DandT,
Coalesce(gd.Projected_Occ1,0) AS Projected_Occ1,
Coalesce(Occupied-(Comps+House),0)+Coalesce(gd.Projected_Occ1,0) as Total
from Occupancy_Forecast o
FULL OUTER JOIN Group_Details_HF gd
on o.Business_Date = gd.Business_Date
and o.Resort = gd.resort
Currently, this gives me the desired results from the Occupancy and Occupancy forecast table however when the business date does not exist in the occupancy forecast table it ignores the group_details table, I need the results to combine the dates when they exist in both or give the unique results for each when there is no match
I have decided to create another pivot table storing the details from Group_Details_HF and then Union together the two tables which has given me the desired result rather than fiddling with the join :)
I have two tables in my Ms Access 2010 project: a table Sites with site information and modality they have, and a table Visits with information about the visit.
Table Sites:
SitesID, Name, City, Modality_A (yes/no), Modality_B (yes/no), Modality_C (yes/no)
Table Visits:
VisitsID, SitesID, Startdate, VisitorID, VisitTypeID, Modality (A/B/C)
I now want to get from every record in Sites
The number (count) of visits
The most recent Visits.Startdate
The corresponding VisitorID and VisitTypeID from the most recent Startdate.
When there is no Visit to a certain Site, the site still has to be on the list.
Last but not least, I want to be able to filter on Modality. So when I choose Modality A, I want only the sites with Sites.modality_A=yes and only the visits information with Modality A.
what i have so far (for Modality A):
SELECT DISTINCTROW Sites.SitesID, Sites.Name, Sites.City, Max(IIf([Visits].[Modality]="A", [Visits].[Startdate],Null)) AS MaxOfDate, Sum(IIf(Visits.Modality="A",1,0)) AS CountOfVisits, Max(IIf([Visits].[Modality]="A",[Visits].[VisitTypeID],Null)) AS MaxOfVisitTypeID, Max(IIf([Visits].[Modality]="A",[Visits].[VisitorID],Null)) AS MaxOfVisitorID
FROM Visits RIGHT JOIN Sites ON Visits.SitesID = Sites.SitesID
WHERE (((Sites.SitesID) In (select Sites.SitesID from Sites where (Sites.Modality_A=Yes))) GROUP BY Sites.SitesID, Sites.Name, Sites.City ORDER BY Max(IIf([Visits].[Modality]="A",[Visits].[Startdate],Null)) DESC;"
This works fairly well, but has two problems:
The MAX(visitorID) does not work, it gives not the visitor which did the last visit, but the visitorID with the highest number.
The MAX VisitTypeID does not work, it gives not the VisitTypeID from the last visit, but the VisitTypeID with the highest number
I can't find how to make this work. Any ideas? A complete other SQL then what I have so far is, of course, fine too :)
Thanks a lot!!
I would approach this as:
A join and group by to get the most recent date and count.
A join to get the additional information about the visitor.
A where clause for filtering on modality.
This looks like:
select sv.*, v.visitorid, v.visittypeid
from (select s.sitesId, s.name, s.modality,
count(v.visitId) as numvisits,
max(v.startdate) as maxdate
from sites as s left join
visits as v
on s.sitesid = v.sitesId
group by s.sitesId, s.name, s.modality
) as sv left join
visits as v
on v.sitesId = sv.sitesId and v.startdate = sv.maxdate
where sv.modality = "A";
Can someone please help me with this issue? I've scoured the Internet looking at dozens of examples, but i just can't find a solution that works.
I am using Access 2013. The problem is that I am trying to make a query that will highlight all part numbers from a supplier that either has customer back orders and/or overdue deliveries.
I am using three tables:
tbl_Inventory_Master which I require the part number, on hand stock value, and the supplier code.
For any back orders I need to join the tbl_Customer_Back_Order table as I need the count of back order lines and the sum of the back order quantity.
If the supplier has a late delivery, then I need to add the tbl_On_Order table showing the count of overdue deliveries and the sum of the overdue quantities.
The query is retrieving the data but the returned quantities are double what they should be.
SELECT
I.Inventory_Part_Num, I.Description, I.On_Hand_Stock,
COUNT (B.Part_Number) AS Back_Order_Count, SUM(B.Back_Order_Qty) as BO_Qty,
COUNT(O.Part_Number) AS Late_Deliveries_Count, SUM(O.Order_Qty) AS Late_Qty
FROM (tbl_Inventory_Master AS I
LEFT OUTER JOIN tbl_Customer_Back_Order AS B
ON I.Inventory_Part_Num = B.Part_Number)
LEFT OUTER tbl_On_Order AS O
ON I.Inventory_Part_Num = O.Part_Number
WHERE
I.Customer_Code = '274' AND
O.Due_Date < [ENTER TODAYS DATE IN FORMAT DD/MM/YYYY]
GROUP BY I.Inventory_Part_Num, I.Description, I.On_Hand_Stock
For example, for the part number 2022940 I should have 10 back order lines and an overdue quantity of 43. Instead, the query is returning 20 back order lines and an overdue quantity sum of 86.
From the on order table I have three orders totaling 144 pieces, instead the query is returning 960.
Can someone please advise, as this is driving me crazy?
You are joining along unrelated dimensions, so you need to aggregate before joining:
SELECT I.Inventory_Part_Num, I.Description, I.On_Hand_Stock,
B.Back_Order_Count, B.BO_Qty,
O.Late_Deliveries_Count, O.Late_Qty
FROM (tbl_Inventory_Master AS I LEFT OUTER JOIN
(SELECT B.Part_Number, COUNT(*) as Back_Order_Count,
SUM(B.Back_Order_Qty) as BO_Qty
FROM tbl_Customer_Back_Order AS B
GROUP BY B.Part_Number
) as B
ON I.Inventory_Part_Num = B.Part_Number
) LEFT JOIN
(SELECT O.Part_Number, COUNT(O.Part_Number) AS Late_Deliveries_Count,
SUM(O.Order_Qty) AS Late_Qty
FROM tbl_On_Order AS O
WHERE O.Due_Date < [ENTER TODAYS DATE IN FORMAT DD/MM/YYYY]
GROUP BY O.Part_Number
) as O
ON I.Inventory_Part_Num = O.Part_Number
WHERE I.Customer_Code = '274';
Notice the outer aggregation is no longer needed.
I have 1 table and 1 query that are joined by Player ID. I want to show only the latest test date result for height and weight columns in tblPlayerLogistics and Player Name and PlayerID from qryPlayersExtended
PlayerID is located in both table and query and they are joined.
I have playerID, height, weight,and testdate in tblplayerlogistics
I have PlayerID, Player Name in qryPlayersExtended
I would like a query that returns only one player record labeling the playerId and player name with the most current height and weight of each player determined by the testdate.
Query name: qryPlayersExtended
Table Name: tblPlayerLogistics
I have attached what I am trying to do in an image. This is an example of one player but I will have multiple players with multiple test dates
I have been struggling with this for weeks any help would be appreciated. I looked at this previous post but still couldnt figure it out
similar post
Assuming that there is not more than one test for a player on any one date. Warning, air code. Create a new query qryLastTestDate:
SELECT PlayerID, Max(TestDate) as LastTestDate FROM tblplayerlogistics Group By PlayerID Create a second query qryLastTest:
SELECT tblplayerlogistics.PlayerID, tblplayerlogistics.TestDate, tblplayerlogistics.height, tblplayerlogistics.weight FROM tblplayerlogistics INNER JOIN qryLastTestDate ON tblplayerlogistics.PlayerID = qryLastTestDate.PlayerID and tblplayerlogistics.TestDate = qryLastTestDate.LastTestDate
Your final query would be:
SELECT qryPlayersExtended.PlayerID, qryPlayersExtended.[Player Name], qryLastTest.TestDate, qryLastTest.Height, qryLastTest.Weight FROM qryPlayersExtended INNER JOIN qryLastTest ON qryPlayersExtended.PlayerID = qryLastTest.PlayerID; Add in additional fields as needed.
You will need two queries for this.
First, you need a query which gets the latest date per player. It will have two columns: PlayerId and Max(TestDate), Grouped by PlayerId. I've named this qryMaxPlayerTestDates. It will look something like this:
SELECT PlayerId, Max(TestDate) AS MaxDate
FROM tblPlayerLogistics
GROUP BY PlayerId;
Second, you join the PlayerId and Dates (MaxDate/TestDate) to get results limited by Max(TestDate). It will look something like this:
SELECT tblPlayerLogistics.PlayerId, tblPlayerLogistics.Height,
tblPlayerLogistics.Weight, tblPlayerLogistics.TestDate,
qryPlayersExtended.PlayerName
FROM qryPlayersExtended INNER JOIN (qryMaxPlayerTestDates
INNER JOIN tblPlayerLogistics
ON (qryMaxPlayerTestDates.MaxDate = tblPlayerLogistics.TestDate)
AND (qryMaxPlayerTestDates.PlayerId = tblPlayerLogistics.PlayerId))
ON qryPlayersExtended.PlayerId = qryMaxPlayerTestDates.PlayerId;
If your dates are not duplicated per Player (No Players have more than one test per date, or two tests on same date have different times), you will get one row per Player in the result with the Height/Weight for the latest test date.
I am a New ASP.NET Developer and I am trying to develop a simple suggestion box system. I have the following part of my database desing:
User Table: Username, Name, DivisionCode... etc
Division Table: SapCode, Division
SuggestionLog Table: ID, Title, Description, submittedDate, Username
(The first attribute is the primary key in each table and the attribute (submittedDate) is of DateTime data type)
Now, I need to develop a table that shows suggestions for the last three months. I already developed a query that shows the Employee Name, Username, Division, Suggestion Title, Suggestion Description. All what I want now is to show the Month. For example, to show the suggestions for the last three months, the Month column should show: Jan-2012, Dec-2011, Nov-2011 So how to do that?
My current SQL query:
SELECT dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsLog.Username,
dbo.employee.Name, dbo.Divisions.DivisionShortcut
FROM dbo.Divisions INNER JOIN
dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN
dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID
The desired output is to display:
Employee Name, Username, Division, SuggestionTitle, SuggstionDescription, SuggestionType Month(submissionDate)
I reformatted you query so it would fit on the page without scrolling.
Hopefully this provides what you need. It uses DATENAME to get the month and year parts from the current date and DATEPART to do the "three months ago" calculation.
Note that DATEPART doesn't behave as you might expect - it counts the number of period-end boundaries (in this case months) - hence the condition is
...WHERE DATEDIFF(month,SafetySuggestionsLog.submittedDate,getdate()) < 3
because the last three months have two month-end boundaries between them.
I also added an ORDER BY clause.
SELECT dbo.SafetySuggestionsLog.Title,
dbo.SafetySuggestionsLog.Description,
dbo.SafetySuggestionsType.Type,
dbo.SafetySuggestionsLog.Username,
dbo.employee.Name,
dbo.Divisions.DivisionShortcut,
left(datename(month,SafetySuggestionsLog.submittedDate),3)
+ '-'
+ datename(year,SafetySuggestionsLog.submittedDate) AS SubmittedMonth
FROM dbo.Divisions
INNER JOIN dbo.employee
ON dbo.Divisions.SapCode = dbo.employee.DivisionCode
INNER JOIN dbo.SafetySuggestionsLog
ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username
INNER JOIN dbo.SafetySuggestionsType
ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID
WHERE DATEDIFF(month,SafetySuggestionsLog.submittedDate,getdate()) < 3
ORDER BY SafetySuggestionsLog.submittedDate DESC
It might also be worth noting that you don't have to fully qualify the name of all the columns in the query - it's valid SQL to alias the input tables like so:
...INNER JOIN dbo.SafetySuggestionsLog AS log
You can then refer to column names by alias in the query - e.g.
log.Username
instead of
dbo.SafetySuggestionsLog.Username
which makes it a bit easier to read.