I am attempting to determine the total number of people who attended events hosted by a specific group.
I want to display the total number of attendees, the maximum attendance allowed, the event title, event date, and the event contact person. I tried the following query in different ways, but keep getting the "enter paramenter value" dialog box in MS Access 2007. What do I need to change?
SELECT sum(eventAttendance.attended) AS attendanceTotal, events.max, events.title, events.date, events.eventContact, events.unit
FROM (client INNER JOIN eventAttendance ON client.clientID=eventAttendance.clientID) INNER JOIN events ON eventAttendance.ID=events.id
WHERE events.unit='CTL'and eventAttendance.attended = 'yes'
GROUP BY attendanceTotal, events.max, events.title, events.date, events.eventContact, events.unit;
Thank you.
Table Relationships
SELECT
events.[max],
events.title,
events.[date],
events.eventContact,
events.unit,
Count(eventAttendance.attended) AS attendanceTotal
FROM
(client INNER JOIN eventAttendance
ON client.clientID=eventAttendance.clientID)
INNER JOIN events ON eventAttendance.ID=events.id
WHERE
events.unit='CTL'
AND eventAttendance.attended = 'yes'
GROUP BY
events.[max],
events.title,
events.[date],
events.eventContact,
events.unit;
I changed the aggregate function from Sum() to Count(). Notice the aggregate function does not get included in the GROUP BY clause --- the GROUP BY lists only the fields (or field expressions) which determine the groups, NOT any aggregate functions.
I also added square brackets around the field names max and date because those are both reserved words --- bracketing the names reduces the risk of confusing the database engine.
Edit: Your picture which shows the relationships also shows you have additional reserved words as field names. Suggest you download Allen Browne's Database Issue Checker Utility and check your application with it. It will warn you about reserved words and also other potential "gotcha" issues.
Related
I'm writing (as an exercise) a system where users get a list of vacations and can choose which vacations to "follow."
The UI for each vacation should convey two pieces of information:
How many users are currently following this vacation, and
Whether I (the logged-in user) am following this vacation.
For example, in this screenshot the vacation is followed by some other user, but not by me:
If, on the other hand, I were the one following this vacation, it would have looked like this:
(Note the darker color of the Follow button.)
I would like to obtain a list of all the vacations in the database, along with the above information for each vacation. To this end, my database (MySQL) contains three tables, user, vacation and user_vacation. To obtain the number of followers, I join vacation and user_vacation as follows:
SELECT
vacation.*,
COUNT(user_id) AS followers
FROM
user_vacation
RIGHT JOIN `vacation` ON vacation_id = vacation.id
GROUP BY
vacation.id;
However, I can't think of a way to add to the result set a boolean column that says whether a given user (the same one for all the rows in the result set) is following the vacation.
You can calculate a boolean using:
SELECT v.*,
COUNT(uv.user_id) AS followers,
MAX( uv.user_id = ? ) as me_following
FROM vacation v LEFT JOIN
user_vacation uv
ON uv.vacation_id = .id
GROUP BY v.id;
The ? is intended to be a parameter that identifies the user you care about.
Note that I replaced the RIGHT JOIN with a LEFT JOIN. That is usually easier to follow.
PS. Now you've intrigued me with Coyhaique.
What I have is 3 tables (starred ids may be null):
ITEMS
id|name|cost
EVENTS
id|name|date|assignment|items
ASSIGNMENTS
id|name|start|items
where ITEMS contains lines of cost - an event may cost X and the assignment/project owning that event may have its own lines of cost. All Items in Events and assignments tables are referenced as comma separated lists of ids.
Given an Assignment, I'd like to get
ASSIGNMENTS.NAME|EVENTS.NAME|ITEMS.NAME|ITEMS.COST
Assignment A management 10.00
Assignment A event A travel exp 60.00
Assignment A event A day cost 100.00
Assignment A event B day cost 90.00
I tried the subquery way, building a subquery that returns a list of Items with
SELECT assignments.name, events.name,
concat(events.items, ",", assignments.items)
FROM assignments left join events
ON find_in_set(events.assignment, assignments.id) where assignments.id=2
but that way I would get the assignment item listed twice and, what's worst, I would get a line (assignment without event) with a field starting with comma.
I also tried joining twice the same table, but then MySQL remembered me that I cannot do it.
Any idea on how to solve this?
try this way
SELECT assignments.name, events.name,
concat(events.items, ",", assignments.items)
FROM assignments left join events
ON events.assignment = assignments.id
where assignments.id=2
I'm having trouble creating a view for one of my MYSQL assignments. I understand how to create a view technically, as in, the commands to do so. (I have already done a few other different views for this assignment) My problem is with how to design this particular view... I don't know how to with the knowledge I have/The way I designed my tables.
So, I have 2 relevant tables(There are 2 others but I don't think they are needed for this problem): Attendance and Scholar. I need to create a view where all scholars are listed as well as the date where they were an invited speaker. However, if they were never an Invited Speaker, the date should have a null value shown. So I need to select First Name and LastName from Scholar and ADate from Attendance. Attendance has the column AttendanceType that can be either Invited Speaker or Chairman. Attendance also has the foreign key LName, relating to LastName, and ADate obviously. I can't conceptually think of have to do this, I thought that using a join, which I'm not that experienced with would be the right choice but it didn't work...
Here's what I attempted
CREATE VIEW InvitedScholars
AS SELECT FirstName,LastName,ADate
FROM Scholar LEFT JOIN Attendance ON AttendanceType='Invited Speaker'
WHERE Lname=LastName;
This only gave me Invited Speakers, not all Scholars... I don't know how to progress... any advice would be appreciated.
You need to do your left join on the Last Name (assuming this is your key on both tables). See the SQL below:
CREATE VIEW InvitedScholars
AS SELECT FirstName,LastName,ADate
FROM Scholar LEFT JOIN Attendance ON Scholar.LastName = Attendance.LName
AND Attendance.AttendanceType = 'Invited Speaker';
It appears you have your join and where clauses mixed up. You want to join the the two tables on the last name (which invites another issue if you have two speakers with the same last name) and filter by AttendanceType
FROM Scholar LEFT JOIN Attendance ON Lname=LastName
WHERE AttendanceType='Invited Speaker'
I have two tables - clients and - group
I need to get county and zip from clients and group-assigned from group
When I search, I cannot get distinct results, that is, instead of the output showing 100 clients with zipcode 12345 in jones county in main st group.
I need to have each zip and county listed once by group. I have googled and attempted many ways but it is just beyond me.
Can anyone assist in steering me to the correct way
Adding GROUP BY group, city, zip to the end of your query should get you what you need. It will only return unique combinations of the three.
Presumably you have something like:
select g.*, c.county, c.zip
from clients c join groups g on <some join condition>
You want one result per group. So, add a group by clause such as:
group by g.id -- assuming id uniquely identifies each group
This will give an arbitrary value for the other fields, which may be sufficient for what you are doing. (This uses a MySQL features called Hidden Columns.)
I have a table with the following fields (for example);
id, reference, customerId.
Now, I often want to log an enquiry for a customer.. BUT, in some cases, I need to filter the enquiry based on the customers country... which is in the customer table..
id, Name, Country..for example
At the moment, my application shows 15 enquiries per page and I am SELECTing all enquiries, and for each one, checking the country field in customerTable based on the customerId to filter the country. I would also count the number of enquiries this way to find out the total number of enquiries and be able to display the page (Page 1 of 4).
As the database is growing, I am starting to notice a bit of lag, and I think my methodology is a bit flawed!
My first guess at how this should be done, is I can add the country to the enquiryTable. Problem solved, but does anyone else have a suggestion as to how this might be done? Because I don't like the idea of having to update each enquiry every time the country of a contact is changed.
Thanks in advance!
It looks to me like this data should be spread over 3 tables
customers
enquiries
countries
Then by using joins you can bring out the customer and country data and filter by either. Something like.....
SELECT
enquiries.enquiryid,
enquiries.enquiredetails,
customers.customerid,
customers.reference,
customers.countryid,
countries.name AS countryname
FROM
enquiries
INNER JOIN customers ON enquiries.customerid = customers.customerid
INNER JOIN countries ON customers.countryid = countries.countryid
WHERE countries.name='United Kingdom'
You should definitely be only touching the database once to do this.
Depending on how you are accessing your data you may be able to get a row count without issuing a second COUNT(*) query. You havent mentioned what programming language or data access strategy you have so difficult to be more helpful with the count. If you have no easy way of determining row count from within the data access layer of your code then you could use a stored procedure with an output parameter to give you the row count without making two round trips to the database. It all depends on your architecture, data access strategy and how close you are to your database.