To set the stage, Tables are set up as such for this question:
-----------
Table: Answers
ID - Integer - Auto
Answer - Integer
QuestionID - Integer - FK from the QuizMaster table
StudentID - Integer - FK from the Students table
Table: QuizMaster
ID - Integer - Auto
Answer - Integer
Table: Students
ID - Integer - Auto
StudentName - Varchar(50)
----------
I would like to have an either a sql statement (1st) or a stored procedure (2nd) that would give me the percent correct for each student on the test.
In Access I could cheat and use this:
Code BlockSELECT Students.StudentName,
Count(Students.StudentName) AS TotalCorrect,
(SELECT Count(QuizMaster.ID) FROM QuizMaster;) AS TotalQuestions,
([TotalCorrect])/([TotalQuestions])*100 AS PercentValue
FROM (Answers INNER JOIN Students
ON Answers.StudentID = Students.ID)
INNER JOIN QuizMaster
ON (Answers.Answer = QuizMaster.Answer)
AND (Answers.QuestionID = QuizMaster.ID)
GROUP BY Students.StudentName
ORDER BY Students.StudentName, PercentValue;
Which is composed of the following...
Code Block
SELECT Students.StudentName,
Count(Students.StudentName) AS TotalCorrect
FROM (Answers INNER JOIN Students
ON Answers.StudentID = Students.ID)
INNER JOIN QuizMaster
ON (Answers.Answer = QuizMaster.Answer)
AND (Answers.QuestionID = QuizMaster.ID)
GROUP BY Students.StudentName;
SELECT Count(QuizMaster.ID) AS TotalQuestions
FROM QuizMaster;
([TotalCorrect])/([TotalQuestions])*100 AS PercentValue
But... that wont fly in SQL Server...What would work without using temp tables?
What would be the EASIEST way to do this?
DECLARE @TotalQuestionsAsked as Integer;
DECLARE @StudentName as VarChar(50);
DECLARE @QuestionsCorrect as Integer;
DECLARE @PercentCorrect as Float;
set @TotalQuestionsAsked = select COUNT(*)FROM QuizMaster;
set @StudentName = select StudentName from Students where ID = @StudentID;
set @QuestionsCorrect = select count(Answers.ID) from Answers LEFT JOIN QuizMaster on Answers.QuestionID = QuizMaster.ID where StudentID = @StudentID AND QuizMaster.Answer = Answers.Answer;
set @PercentCorrect = (CAST (@QuestionsCorrect AS FLOAT) / CAST (@TotalQuestionsAsked AS FLOAT)) * 100.0;
select @QuestionsCorrect as TotalCorrect, @StudentName as Student, @TotalQuestionsAsked as NumQuestions, @PercentCorrect as CorrectPct;
END
But I need this as a datasource for a datagrid that would show all students etc... so using a parameter isn't really what I want.
So... could a stored procedure work for this?
in the below sql why is year(classdate) " + " a " + " MONTH(classdate) a math command giving me 2006 - 12 = 167 and not "2006/12" as text? please help me cmdGetCat = New SqlDataAdapter("SELECT DISTINCT year(classdate) " + " a " + " MONTH(classdate) AS monthcode FROM dbo.classT INNER JOIN dbo.classgiven ON dbo.classT.classcode = dbo.classgiven.classcode WHERE (dbo.classT.discount = '-1') AND (dbo.classT.coned IS NOT NULL) ", conNorthwind)
How do i update a sql table so that the result cannot be less than zero?for example, lets say I have the column "Number"I have a sql update statement that subtracts 1 from number:"Update oTable SET Number = (Number - 1)"Except that Number cannot be less than zero. Is there a way to do this in sql statement so that I don't have to have a select statement just to check that Number is greater than zero to begin with? Thanks
I have a database for one of my websites, a picture rating site. Anyways, right now there are quicte a few tables, and I was wondering how to give the server a break and was wondering if this was possible:
Basicly I have a members table, and a votes table. Members will rate other users pcitures on a scale of one to ten, then the votes will be inserted into the votes table. The only problem with this is that calcuating all the votes a user has can put a straing on the server. I was wondering if it would be possible to create a math column in the members table that would automaticly figure out the users average and having it stored in a field in the members table, so all I would have to do is query the members average located in the mebers table, rather than tallying all the votes in the votes table for each member.
Hope this makes sense, a tutorial or any suggestions would be great!
select convert(float,'1.2334e+006')1233400.0select convert(decimal(20,2),'1.2334e+006')Server: Msg 8114, Level 16, State 5, Line 1Error converting data type varchar to numeric.can I set some options arithabort etc to have a workaround to thisproblem?Thanks.
I'm not a professional dba or dbd, but I'm proficient in the basics ofdatabase design and sql. I want to create a database of mathdefinitions, and I'm wondering how one would go about creating adatabase that contains mathematical notation (and I'm not just talkingabout basic symbols where I could get away with ascii code). I needto be able to insert a wide variety of mathematical expressions, fromfractions to integrals, into fields (just like you can enter in-linemath symbols in MS Word using equation editor). I have no clue how togo about this. Is it a matter of developing certain programmingskills/languages? Would such a capabliltiy be proprietary (dbms-specific)? Is it possible at all? Any help would be appreciated.Thank you.
Hi AllI'm trying to find a math function (if it exists) in SQL Server. If itdoesnt exist, then maybe someone can tell me what its called so I cando a bit more reading on itBasically I want to do this:Parameter Components1 12 23 1, 24 45 1, 46 2, 47 3, 48 89 1, 8and so onI'd like to be able to call a function and it would return true orfalse like sofunctionname(1, 9) = trueso 1 is a component of 9functionname(2, 9) = falseso 2 is not a component of 9functionname(4, 5) = trueso 4 is a component of 5If anyone could tell me if it exists in C#, VB.NET, VB6 or VBScript,I'd appreciate it!Thanks in advanceSam
Below is my table layout, query, and results. I need to perform some date math to display how much time is elapsed between locations as patients are checked into each care_unit. I believe I could use the unique careunit_key but am not sure how to go about this.
[srm].[CDMAB_CAREUNITS]
[CAREUNIT_KEY] [decimal](37, 0) NOT NULL,
[EPISODE_KEY] [decimal](38, 0) NULL,
[CARE_UNIT] [char](20) NULL,
[ROOM_NO] [char](5) NULL,
[BED_NO] [char](5) NULL,
[DATE_IN] [datetime] NULL,
[TIME_IN] [char](6) NULL
select
cu.careunit_key,
cu.care_unit,
convert(nvarchar,e.ADMISSION_DATE, 101) as Admit_Date,
convert(nvarchar,e.EPISODE_DATE, 101) as Discharge_Date,
convert(nvarchar,cu.date_in,101) as date_in,
substring(cu.time_in, 1,5) as time_in,
convert(nvarchar,cu.date_in,101) +
convert(datetime,substring(cu.time_in, 1,5)) as date_time_in
FROM srm.episodes e
inner join srm.CDMAB_CAREUNITS cu on cu.episode_key = e.episode_key
inner join srm.item_header ih on ih.item_key = e.episode_key
inner join srm.patients p on p.patient_key = ih.logical_parent_key
Does anyone know if there is such a quary that can be written which would add up(or any math functions) a line of cells (on different rows) similar to that of working with a excel document?
If so please steer me towards the correct syntax for this.
I have a column coming from DB2. It is the time is stored as 6 decimals of a second. So the value I got coming in is 44846(DT_I8), which needs to be divided by 60 to get minutes (747 minutes remainder 26 seconds). Then divide 747 by 60 to get 12 hours remainder 27 minutes. Thus the time is 12:27:26.
I have got a dervived column doing (DT_R8)(SUBSTRING((DT_WSTR,8)PSCCLOGTIM,1,5)) / 60 but the answer I am getting out is 747.433333. Close, but not close enough. Now I assume my problem has something to do with the Math being done on actual numbers not Time based numbers.
Anyone got any ideas on where I am going wrong and what the expression should look like?
I have been trying to make a database that counts up and down votes (like eBay ratings or reddit votes). I think (hope) I have got the database design right. I know that you can perform math functions in SQL, but I want to use two COUNT()s from the same table and subtract one (the down votes) from the other (the up votes). I have been learning ASP.NET 2.0 and it's going well, but I really need help with this. I asked a question on this forum before and the answers were great and really helpful. If anyone can help that would be great. Thank you. Jack.
The business rule is, the sales manager is commissioned on the avg. numberof appointments set up per salesrep per day during the month.I have 2 tables: The UserLog table records only 1 entry per day per user(salesrep). This will log how many salesreps worked a particular day. Thesecond table logs any appointments set up.UserLog: ID, UserName, EnteredTimeAppointment: ApptID, EnteredTime, ApptDateI figured that, for a given date ranged, I could1. sum the number of appointments2. sum the number of days worked2. sum the salesreps / number of days = avg number of salesreps per day3. number of appointments / avg number of salesreps per day = avg numberof appointments per sales repBut this logic is flawed. If I average out every day and then take anaverage of this daily average, I get a different result. Any ideas on howto best solve this problem?Thanks.
The value in the table of one DB is 17869 sq. ft. Now to insert this value in a new table of other database the reporting basis is 1000 i.e I need to do 17869/1000 = 17.8 so I have to take the value as 18. Another thing to be kept in mind is the value in the new table should have leading Zeroes. If the value is 18 it should be displayed as 0000018 ( data type in new table is Varchar(7) and in old table char (9) ). What can be the best way to implement this??
I have a prob with the following code... in that the value of the amount of the year 2001 is 0 so it is showing me division by zero prob can any one change the code and let me know the details
declare @year1 int declare @year2 int declare @month int
set @year1 = 2002 set @year2 = 2001 set @month =9
select case when sum(case WHEN a.oper_year = @year1 THEN a.amount else 0 end) = sum(case WHEN a.oper_year = @year2 THEN a.amount else 0 end) then 0 else ( (sum(case WHEN a.oper_year = @year1 THEN a.amount else 0 end) - sum(case WHEN a.oper_year = @year2 THEN a.amount else 0 end))/ sum(case WHEN a.oper_year = @year2 THEN a.amount else 0 end) )* 100 end as Percentage, from oper_sundata a, oper_type_new b where a.site_id = b.sun_site
hi, how can i make mathematical operations with the DateTime format from thw Sql? -- this is the format 9/6/2007 11:09:00 PM -- how can i substract 30 days from that date and know the resulting one? if i have two dates, how can i know what number of hours and minutes are between them (if they are fewer than 24), or what number of days and hours and minutes are (if the difference is grater than 24 hours) please help me, thanks
In my query, there's a mathematical expression that takes a value from one table and divides it by another value (X). The problem is that X can be 0 sometimes and then I get an error. How can I prevent errors like this for the case of X=0? In access I would use IIF function, but it doesn't appear in SQL SERVER views. Thanks.
This query is part of a larger query that updates a table that holds statistics for reporting. It yields actual Unit per Minute by plant by month. Some of the plants don't produce anything in certain months, so I'm ending up with a Divide by Zero error. I think I just need to stick another CASE statement in for each month, but that seems like it could get pretty ugly.
Any suggestions on how to improve this?
SELECT FL.REPORT_PLANT, [JAN]= SUM(CASE WHEN MONTH(PC.MNTHYR) = 1 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 1 THEN PC.HOURS*60 ELSE 0 END), [FEB]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 2 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 2 THEN PC.HOURS*60 ELSE 0 END), [MAR]= SUM(CASE WHEN MONTH(PC.MNTHYR) = 3 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 3 THEN PC.HOURS*60 ELSE 0 END), [APR]= SUM(CASE WHEN MONTH(PC.MNTHYR) = 4 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 4 THEN PC.HOURS*60 ELSE 0 END), [MAY]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 5 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 5 THEN PC.HOURS*60 ELSE 0 END), [JUN]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 6 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 6 THEN PC.HOURS*60 ELSE 0 END), [JUL]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 7 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 7 THEN PC.HOURS*60 ELSE 0 END), [AUG]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 8 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 8 THEN PC.HOURS*60 ELSE 0 END), [SEP]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 9 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 9 THEN PC.HOURS*60 ELSE 0 END), [OCT]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 10 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 10 THEN PC.HOURS*60 ELSE 0 END), [NOV]=SUM(CASE WHEN MONTH(PC.MNTHYR) = 11 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 11 THEN PC.HOURS*60 ELSE 0 END), [DEC]= SUM(CASE WHEN MONTH(PC.MNTHYR) = 12 THEN PC.TONS * 2000 / PM.EA_WT ELSE 0 END)/ SUM(CASE WHEN MONTH(PC.MNTHYR) = 12 THEN PC.HOURS*60 ELSE 0 END) FROM PRODUCTION_CMPLT PC INNER JOIN FACILITY_LINES FL ON PC.MANUF_SITE = FL.MANUF_SITE AND PC.PROD_LINE = FL.PROD_LINE INNER JOIN PROD_MASTER PM ON PC.PRODUCT=PM.PRODUCT WHERE YEAR(PC.MNTHYR) = YEAR(GETDATE()) AND PM.UOM<>'LB' GROUP BY FL.REPORT_PLANT
hi in a view, i create a field like field1/field2. in case that field2 is zero, i've got division by zero error. how can i change the value to 0 when field2 is 0 for avoiding get error.
It is common for me to need to create ratios from data in my database such as
SELECT ( list_value / sale_price ) as ratio FROM values
The value returned is always an integer whether decimal is cast or not. IE if sale_price is > list_value then 1 or 0 is returned instead of the percentage (ratio) as expected. Only whole numbers are returned. BTW. Same is true in postgres db I have as well. What is it that I am doing wrong?
I have a table in which i have two colums say discription and counts. the table has 10 rows. This table is created by extracting data from other table means its not a table that exist in system.
Now in my last row i want discription as '%mailed' and count as row1/row2