| Author |
Topic |
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-13 : 05:37:56
|
| HI all, I'm new to sql and this is my first post. I have a table that contains the following data. studentid , classid, groupid, date,time,attendI have a query that will pull records for a selected student from a combo box(on a form).... For each student, the records would be as follows, for every class a day, thay would be mark as absent, present or excused.I'm wondering if it possible to create a query that will for a selected student, search each different date and the attended status, i want to return a number for every date that has all absents?If you would like me to post the database say let me know it might help you alot better,Thanks Chris Lynch |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-05-13 : 05:51:33
|
| Try thisSelect date,count(attend) from table where attend='absent' group by dateMadhivananFailing to plan is Planning to fail |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-13 : 06:07:03
|
| Thanks that looks good but, i don't know if i done it right. Correct me if i'm wrong. created a new query and just pasted that code into the SQL part. but when i run it nothing happens. IS it possible to attach anything here?If not this is the altered code to suite my database...SELECT Attendance.Date, Count(Attendance.Attended) AS CountOfAttendedFROM AttendanceWHERE (((Attendance.Attended)=' absent'))GROUP BY Attendance.DateThanksChris Lynch |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-13 : 06:12:29
|
| I take that back its given me results now. I'll just test it out and see if it doing what i want it to doI'll post back here later Thanks againChris Lynch |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-13 : 06:25:27
|
| OK i'm back. Your legend you wouldn't believe how many different forum said i couldn't do that....This is my code so far.SELECT Attendance.Date, Count(Attendance.Attended) AS CountOfAttended, Student.StudentNameFROM Student INNER JOIN (Groups INNER JOIN (Classes INNER JOIN Attendance ON Classes.ClassID = Attendance.ClassID) ON Groups.GroupID = Attendance.GroupID) ON Student.StudentID = Attendance.StudentIDWHERE (((Attendance.Attended)=' absent'))GROUP BY Attendance.Date, Student.StudentNameHAVING (((Student.StudentName)="Chris_Lynch"));This returns the nuber of classes chris lynch misses in any day. So i'm have way there now..... This is all i need to do, it want it to only return the value if the day has all absents only...Now its saying chrislynch adsent two times on the 11/05/2005......but he wasn't absent for the day as the other classes are marked as presentThis result i want to get is, when i open the query something like this appears(field names)Date Count Name11/05/2005 1 ChrisLynchthis would mean that chrislynch has all absent on the 11/05/2005Hope this make it clear enoughThanks Chris Lynch |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-13 : 10:09:39
|
Hi again, i'm beginning to think that what i want to do is not possible, but i have come up with a solution. I have it so far that it will the query will show date on a student if they have been marked absent... I have added another field which i want to count the number to different classes entered for that day, but it doesn't work correctly, it only show the number of classes which have a absent in the record. so what i need to do is show in the query the date, number of classes absent student name and number of classes on that date.... This is the code so far but the total number of classes gives the wrong number can any one help...SELECT Attendance.Date, Count(Attendance.Attended) AS CountOfAttended, Student.StudentName, Count(Classes.ClassID) AS CountOfClassIDFROM Student INNER JOIN (Groups INNER JOIN (Classes INNER JOIN Attendance ON Classes.ClassID = Attendance.ClassID) ON Groups.GroupID = Attendance.GroupID) ON Student.StudentID = Attendance.StudentIDWHERE (((Attendance.Attended)=' absent'))GROUP BY Attendance.Date, Student.StudentName, Student.StudentIDHAVING (((Student.StudentID)=[Forms]![Frm]![cboStudent])); ThanksChris Lynch |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-13 : 13:16:59
|
One dont use the word date or time for a field name.Two you dont need two fields for date and time just use a datetime field. for the example I used classdate.Select distinct studentid, Convert(varchar(30),classdate,101) as classdateInto #notabsentFrom classes Where attend <> 'absent' and studentid = @studentid select studentid, Count(*) as NumberOfDaysAbsentFrom classesWhere Convert(varchar(30),classdate,101) not in (Select classdate from #notabsent)Group by studentid,Convert(varchar(30),classdate,101)HAving studentid = @studentid sorry got carried awayJimUsers <> Logic |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-05-13 : 13:23:06
|
quote: Originally posted by JimLsorry got carried awayJimUsers <> Logic
As long as it's not around live ordinance....Brett8-) |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-13 : 13:35:03
|
Been their done that! JimUsers <> Logic |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-14 : 07:43:37
|
| Hi JimL could you please explain what happens in the sql you gave as i'm trying to learn. I'm using seperate time and date because its easier to work with it like that for now anyway.......I haven't a clue what the convert does and stuff.So if you have a chance can you explain what it is doing pleaseChris Lynch |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-05-14 : 14:35:26
|
From Books Online:quote: Data Type ConversionIn Transact-SQL, two levels of data type conversions are possible: When data from one object is moved to, compared with, or combined with data from another object, the data may have to be converted from the data type of one object to the data type of the other.When data from a Transact-SQL result column, return code, or output parameter is moved into a program variable, it must be converted from the Microsoft® SQL Server™ data type to the data type of the variable. There are two categories of data type conversions: Implicit conversions are invisible to the user. SQL Server automatically converts the data from one data type to another. For example, if a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.Explicit conversions use the CAST or CONVERT functions. The CAST and CONVERT functions convert a value (a local variable, a column, or another expression) from one data type to another. For example, the following CAST function converts the numeric value of $157.27 into a character string of '$157.27':CAST ( $157.27 AS VARCHAR(10) )CAST is based on the SQL-92 standard and is preferred over CONVERT.When converting from the data type of one SQL Server object to another, some implicit and explicit data type conversions are not supported. For example, an nchar value cannot be converted to an image value at all. An nchar can only be converted to binary using explicit conversion; an implicit conversion to binary is not supported. An nchar can be either explicitly or implicitly converted to nvarchar. When handling sql_variant data types, SQL Server supports implicit conversions of objects with other data types to sql_variant type. However, SQL Server does not support implicit conversions from sql_variant data to an object with another data type.For more information about supported conversions between SQL Server objects, see CAST and CONVERT. When converting between an application variable and an SQL Server result set column, return code, parameter, or parameter marker, the supported data type conversions are defined by the database application programming interface.
Here is an example for you of more date types possible with the convert function:SET NOCOUNT ONDECLARE @min INT, @max INT, @date DATETIMESELECT @min = 1, @max = 131, @date = GETDATE()SELECT @dateWHILE @min <= @maxBEGIN IF @min BETWEEN 15 AND 19 OR @min = 26 OR @min BETWEEN 27 AND 99 OR @min BETWEEN 115 AND 119 OR @min BETWEEN 122 AND 125 OR @min BETWEEN 127 AND 129 BEGIN GOTO NEXT_LOOP END SELECT @min, CONVERT(VARCHAR,@date,@min)NEXT_LOOP:SELECT @min = @min + 1END MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
C_H_I_L
Starting Member
9 Posts |
Posted - 2005-05-16 : 05:01:13
|
| I don't want to convert anything, why are ye telling me about the convert thing? |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-05-16 : 07:44:22
|
Hello??? Anyone home?????? quote: I haven't a clue what the convert does and stuff.So if you have a chance can you explain what it is doing please
That was a quote from you. You really should read your own posts before you hit the "Submit Reply" button.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-16 : 07:47:21
|
| C_H_I_LYou said you wanted to learn the right way.Using a single DateTime field is the right way. This will allow you to do comparisons and timelines.Look up DateADD and DateDiff in books on line.You can also output it in time only or date only using convert.What my code does is to make a Exclusion table to lock out all dates that do not have all absences for that day. Then the second select gives you your count. JimUsers <> Logic |
 |
|
|
|