Calculate number of exercises in each category in access 2016 [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have two tables such as: Category and Exercise. In the first of them I have field names such as: CategoryID and CategoryName. In the second table there are field names such as: ExerciseID Exercise and CategoryID. I have to calculate the number of exercises in each category. For the exercises with provided data I have:
addition, subtraction, multiplication, division, enhancemen,
running, jumping, pole vault, shot put, literature, grammar.
The query has to output me:
Maths = 5
English = 2
PE = 4
How would I be able to calculate this within query in ms-access?

You should have a key to join the tables.
In addition to ExerciseID and Exercise, have a CategoryKey column which you can join to the Category table through CategoryID. Then you'd just query SELECT COUNT(*) as count FROM Exercise WHERE CategoryKey = 1 where 1 is the CategoryID you want to count. You can also do more complex joins in the future if you have new requirements.
Otherwise, you'll have to write a giant goofy sql switch statement, which I don't have much experience with, because it's usually not something you need to use if your table is structured correctly.

Related

How to merge two or more rows where some columns have same values and some have different values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Sample Data output after joining 2 different tables on ProductNumber
ProductNumber
QuantityOnHand
VendorID
1
5
401
1
5
501
I want to write a query that would return this output
ProductNumber
QuantityOnHand
VendorID
1
10
401 & 501
I'm still pretty new to SQL. Stuck on a homework problem here. I don't really know which aggregated function to use to make it work. Sum() but I don't want to add the vendorIDs. Concat() but they're from the same column name.
If it is SQL Server, then you need STRING_AGG function for cancatenating the same column in different rows and SUM for totalling:
SELECT ProductNumber,
SUM(QuantityOnHand),
STRING_AGG(VendorID, ' & ')
FROM Product
GROUP BY ProductNumber
I assumed the table name is Product, as you did not provide one. You can update it to your actual table name.

Find distinct from multiple columns [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I have 2 columns called price and PID in database called seatinginfo.
Both columns will have multiple of the same values like
pid 1,1,1,1,2,2,2,2,2,2,2,3,3,3,3,3
price 10,10,30,40,60,80,70,90,90,90,90 etc
I'm looking to make a query that will give me a list of all unique prices and pick just 1 or the max connected pid.
So example I only want one price,pid like 10,1 30,1 40,1 60,2 etc
Thanks
This is probably as simple as basic aggregation. Something like this should work based on the pretty vague details.
select price
, max(pid)
from YourTable
group by price

Replace one column by values of the same column in anther table in SQL server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables, one big and one small. Both contain columns ID and EffectiveDate.
The bigger table has more other columns and of course more rows than the smaller table.
Under the condition that ID for both tables are the same, the EffectiveDate column is earlier in the small table than the big table. I want to replace the EffectiveDate in the big table by the value of the EffectiveDate column from the small table.
What should I do?
Seems like a very basic SQL query....
UPDATE bt
SET EffectiveDate = st.EffectiveDate
FROM dbo.BiggerTable bt
INNER JOIN dbo.SmallerTable st ON bt.ID = st.ID
-- maybe you also need this condition, if not *ALL* EffectiveDate values in the
-- smaller table are indeed before the values in the bigger table
WHERE st.EffectiveDate < bt.EffectiveDate

SQL SELECT query From SELECT query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I encountered a small problem in my attempt on a database related to the classrooms of a school.I have a table with classrooms and their number of seats as in:
Table: classrooms
NAME SEATS
R1 20
R1 25
and a table associated to each classroom (R1,R2,R3) as in:
Table: R1
NAME DATE FREE
R1 11/6/2015 YES
R1 12/6/2015 NO
Table: R2
NAME DATE FREE
R2 11/6/2015 YES
R2 12/6/2015 YES
Is it possible to SELECT from "classrooms" the NAME (based on seats) and use the returned values as TABLE titles in another SELECT?
Something like:
SELECT NAME FROM (SELECT NAME FROM classrooms WHERE SEATS>20) WHERE DATE=11/6/2015 AND FREE=YES
The SELECT inside the brackets would return the names of the TABLES to which I apply the query for DATE and FREE.
Is that even possible?I would really appreciate any suggestion!
I think there is only one answer: execute your query in the client (ie Management Studio). The select query will have no impact for your tables so don't worry and try yourself.
Why do you have the original select statement? That does not seem necessary...
SELECT name FROM classrooms
WHERE type="xx" AND seats>="xx")
WHERE day="xx" AND class="xx"
AND free="xx"
You could do the same thing with that unless you need the subquery for some reason I can't see.

SQL Select and sort on relevance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to search an table column. and select all rows that contains a string.
I do that on this way:
select * from memos where contains (article, '"test*"')
The next problem is to order the acticles on relevance. So if a record contains 4 times the word of wordpart 'test' i want it on top and if it contains 3 times the word 'test' i want it below 4. And so order it on how many times a word is in a row.
Assuming the article is stored as a varchar() or nvarchar(), then you can do this
select *
from memos
where contains (article, '"test*"')
order by len(replace(article, 'test', 'test1')) - len(article) desc;
This replaces test with a string one character longer, measures the length, and then subtracts the original length. Voila. The number of times that test occurs. This should take place only on articles that have the search term.
I'm not sure if SQL Server has something like this built-in to the full text engine.