Avatar of plusone3055
plusone3055Flag for United States of America

asked on 

SQL Syntax to count number of instances in the database

greetings im working on some inventory control and looking for the way to perform this action ...

i have 50 avaialble seats for training on my given days..

in my database when someone takes a spot  on a given day  the program rus this query  

Insert into trainingdates  (userID, roomId, DateId,SeatsId)


so when a manager is setting training for thier  employees  they have a dropdown menu to select the room, the date,  how many seats they need

What i want to do is query Trainingdates table  and count every instance where
the same room and date match from their dropdowns (giving me the total number of instances) .. so  that lets say there are already  20  slots taken
I can takethe numbe from this query  subtract it from 50 - 20 = 30  and compare it the number of seats requested.. If  the user wanted 30 or less they can still keep that choice.. of not they cant beucause thats overselling

 

SQL

Avatar of undefined
Last Comment
plusone3055
Avatar of David Kroll
David Kroll
Flag of United States of America image

Is SeatsId the number of seats they requested, or would there be 5 records if they requested 5 seats?

If it is the latter, you would do

select count(userID)
from trainingdates
where roomId = <selected room id>
and dateid = <selected date>

If it is the former, you would do

select sum(seatsid)
from trainingdates
where roomId = <selected room id>
and dateid = <selected date>
Avatar of plusone3055
plusone3055
Flag of United States of America image

ASKER

if a user selected 5 seats there would be 5 records... one record for each seat
IE
USERID     ROOMID  DATEID  SEATID
1               1              1           1-1
 1              1              1           1-2

ect ect

There is also a primary key that autogenerated  when this infor is inserted into the   database called USERRAININGDATEID so

USERTRINAINGDATEID     USERID     ROOMID  DATEID  SEATID
            1                              1               1              1          1-1
            2                             1              1              1           1-2
            3                             1              1              1           1-3

which would you go with  now ?
======================================================
also  how about a separate  query to also produce the list of each seat in that instance so I can whip something up to grey out that seat number when they select that day

(if you wnat me to post that as a separate question I will :) )
 
ASKER CERTIFIED SOLUTION
Avatar of David Kroll
David Kroll
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of plusone3055
plusone3055
Flag of United States of America image

ASKER

select seatid
from trainingdates
where roomId = <selected room id>
and dateid = <selected date>

that will return each seat  IE 1-1 1-2 and 1-3
Avatar of stalhw
stalhw

SELECT CASE WHEN 50 - COUNT(*) >= @askedNumberOfPlace THEN 'Approved' ELSE ' END
FROM trainingdates
WHERE roomid= @askedRoomID
AND dateid= @askedDateID
Avatar of David Kroll
David Kroll
Flag of United States of America image

plusone:

yes that will return each seat.
Avatar of plusone3055
plusone3055
Flag of United States of America image

ASKER

thank you for all your assistance
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo