How should I go about comparing all rows in a sql table against a "master record" using vb.net? - sql

Let's say, for example, this is a quiz where the answers are already loaded into a database from different students. Inside of the table with various student entries containing various student's answers there is a row "masterentry" with all of the correct answers.
What I would like to do is code a button that will pull the master row on click and compare every other row against it. Starting with the first row, every time the column matches the master entry 1 point is assigned to a declared integer. When the row has completely been read the integer is stored in a "totalpoints" column. Then it moves on to the next row of answers.
I hope this makes sense the way I worded it, if there is an easier way to go about it I am open to suggestions. I don't really need any help establishing the connection to the sql database. Any help would be greatly appreciated, thanks in advance for anyone's time. I can provide more details if it would be helpful.

Just a rough guess based on vague idea of schema. You would just call this query from .NET (preferably stored in stored procedure, passing in a #QuizID) unless you really want to do it less efficiently by looping after pulling all of the data to the client...
;WITH master AS (SELECT a1 = Answer1, a2 = Answer2, ...
FROM dbo.Quiz
WHERE QuizID = #QuizID
AND Answer = 'masterentry'
)
SELECT a.StudentID,
Score = CASE WHEN a.Answer1 = master.a1 THEN 1 ELSE 0 END
+ CASE WHEN a.Answer2 = master.a2 THEN 1 ELSE 0 END
+ ...
FROM dbo.Quiz AS a
CROSS JOIN master
WHERE a.QuizID = #QuizID
AND a.Answer <> 'masterentry' -- or a.StudentID IS NOT NULL?
There may be some shortcuts here based on the data types. E.g. if the quiz is true/false there are some little tricks you can do to reduce the ... part.

Related

In Access 2010, how do I write an update query to "fill in the blanks"?

Here is a picture explaining what I have and what I'm looking for.
Tables and data describing the question.
This originally seemed like a very simple update query but it has proven I really don't understand how the query works. Can someone please explain the SQL I would need to move all the data from tblImport.Addtl_Initiator to the respective blanks under tblEntries.Initiator, and the data from tblImport.Addtl_Date_Initiated to the respective blanks under tblEntries.Date_Initiated? Essentially just moving the data to the left one column, but in a different table.
Additional information: There is a unique ID field on each of the tables not shown in the picture.
Try it with Nz.
UPDATE tblentries
INNER JOIN tblimport
ON tblentries.eid = tblimport.eid
AND tblentries.initiator = tblimport.initiator
AND tblentries.date_initiated = tblimport.date_initiated
SET tblentries.initiator = nz(tblimport.initiator, tblimport.addtl_initiator),
tblentries.date_initiated = nz(tblimport.date_initiated, import.addtl_date_initiated)
This link might also be helpful: coalesce alternative in Access SQL
Use IsNull to get the non-blank values:
SELECT
IsNull(Initiator, Addtl_Inititator) AS Initiator,
IsNull(Date_Initiated, Addtl_Date_Initiated) AS Date_Initiated,
FROM
tblImport

Access Check If DataSet contains string via vba Macro

Hello Stackoverflow community!
I am currently confronted with writing somthing, that automatically cleans up a database after certain requirements. Here is what I have to do:
If a cell in one table contains a certain substr (lets say "Mrs."), in another table a Togglebox is supposed to be checked (Yes if "Mrs." is contained and No if not)
I already worked out something that sets The Toggleboxes to -1 in that column, but I have to include a check, if it even has to be changed and in which row. For that I wanted to bring up an If-construction and a variable that would contain the ID of the first table's row to determine, which of the Rows in the second table have to be changed.
So the thought is like this:
If 1st_Table contains "Mrs." Then
CurrentDb.Execute "UPDATE 2nd_Table SET gender = -1 WHERE foo = 1st_Table.ID_Var"
1st_Table.ID_Var would contain the ID of the row of the first table
Now the more or less obvious question: How do I accomplish that?
This is bascally the first time working with VBA/Access AND SQL so I have no closer thoughts on how to do that.
Thanks in advance
Greetings
geisterfurz007
P.S. The variable names will be changed in the final version; they are just to visualize. Just saw that they were colored by SO.
Try this (SQL update query):
UPDATE 2nd_Table INNER JOIN 1st_Table ON 2nd_Table.ID = 1st_Table.ID
SET 2nd_Table.gender = -1
WHERE 1st_Table.PersonName LIKE '*Mrs.*'
ID -> reference between both tables
PersonName -> column in 1st table containing Mrs.

How do I increase a counter by 1 for each repeated value?

As part of my course in university I have to make a database in Microsoft Access which is somewhat limiting me on what I'm trying to do. I have a table that has the information of whether a player in a team was present for a fixture or not using the values "P", "R", and "M" (Played, Reserves, Missed). I want to make a query that counts a value of 1 for each value of P or R and a separate one for M, so that when I make a report that prints off a membership card, it shows the amount of fixtures they've played in and the amount of fixtures that they have missed.
Sorry if this isn't clear, I'll try to explain myself further if you ask but I'm not very good with this stuff. Thank you.
Edit: I'll use screenshot links if that's okay, here is the Fixture Attendance entity that shows if a member of a team attended a game or not. I'm making a membership card based off this one. I want to be able to display the No. of fixtures played by the member and the No. of fixtures missed based off the values in the above entity and use that information in a form I'm going to create. That will be a subform inside my Membership Card form.
I'm presumably really bad at explaining this - I understand Access is rarely used in the real world so I'm not sure why I'm doing this in the first place and don't feel like I'm getting any real knowledge of working with databases.
You should use the COUNT function.
http://office.microsoft.com/en-us/access-help/count-data-by-using-a-query-HA010096311.aspx
I am guessing that you want something like this:
select playerid, sum(iif(fixture in ("P", "R"), 1, 0)) as NumPR,
sum(iif(figure = "M", 1, 0)as NumM
from table t
group by playerid;
The key idea here is putting the conditional part (iif()) inside the sum().
CASE WHEN can be used to translate the codes into 1's and 0's. Then use SUM with a GROUP BY to sum them.
SELECT player_id, SUM(Played), SUM(Reserve), SUM(Missed)
FROM
(SELECT player_id,
CASE WHEN present = 'P' THEN 1 ELSE 0 AS Played,
CASE WHEN present = 'R' THEN 1 ELSE 0 AS Reserve,
CASE WHEN present = 'M' THEN 1 ELSE 0 AS Missed
FROM fixtures)
GROUP BY player_id;

Selecting top n Oracle records with ROWNUM still valid in subquery?

I have the following FireBird query:
update hrs h
set h.plan_week_id=
(select first 1 c.plan_week_id from calendar c
where c.calendar_id=h.calendar_id)
where coalesce(h.calendar_id,0) <> 0
(Intention: For records in hrs with a (non-zero) calendar_id
take calendar.plan_week_id and put it in hrs.plan_week_id)
The trick to select the first record in Oracle is to use WHERE ROWNUM=1, and if understand correctly I do not have to use ROWNUM in a separate outer query because I 'only' match ROWNUM=1 - thanks SO for suggesting Questions that may already have your answer ;-)
This would make it
update hrs h
set h.plan_week_id=
(select c.plan_week_id from calendar c
where (c.calendar_id=h.calendar_id) and (rownum=1))
where coalesce(h.calendar_id,0) <> 0
I'm actually using the 'first record' together with the selection of only one field to guarantee that I get one value back which can be put into h.plan_week_id.
Question: Will the above query work under Oracle as intended?
Right now, I do not have a filled Oracle DB at hand to run the query on.
Like Nicholas Krasnov said, you can test it in SQL Fiddle.
But if you ever find yourself about to use where rownum = 1 in a subquery, alarm bells should go off, because in 90% of the cases you are doing something wrong. Very rarely will you need a random value. Only when all selected values are the same, a rownum = 1 is valid.
In this case I expect calendar_id to be a primary key in calendar. Therefor each record in hrs can only have 1 plan_week_id selected per record. So the where rownum = 1 is not required.
And to answer your question: Yes, it will run just fine. Though the brackets around each where clause are also not required and in fact only confusing (me).

Convert row data into columns Access 07 without using PIVOT

I am on a work term from school. I am not very comfortable using SQL, I am trying to get a hold of it....
My supervisor gave me a task for a user in which I need to take row data and make columns. We used the Crosstab Wizard and automagically created the SQL to get what we needed.
Basically, we have a table like this:
ReqNumber Year FilledFlag(is a checkbox) FilledBy
1 2012 (notchecked) ITSchoolBoy
1 2012 (checked) GradStudent
1 2012 (notchecked) HighSchooler
2 etc, etc.
What the user would like is to have a listing of all of the req numbers and what is checked
Our automatic pivot code gives us all of the FilledBy options (there are 9 in total) as column headings, and groups it all by reqnumber.
How can you do this without the pivot? I would like to wrap my head around this. Nearest I can find is something like:
SELECT
SUM(IIF(FilledBy = 'ITSchoolboy',1,0) as ITSchoolboy,
SUM(IIF(FilledBy = 'GradStudent',1,0) as GradStudent, etc.
FROM myTable
Could anyone help explain this to me? Point me in the direction of a guide? I've been searching for the better part of a day now, and even though I am a student, I don't think this will be smiled upon for too long. But I would really like to know!
I think your boss' suggestion could work if you GROUP BY ReqNumber.
SELECT
ReqNumber,
SUM(IIF(FilledBy = 'ITSchoolboy',1,0) as ITSchoolboy,
SUM(IIF(FilledBy = 'GradStudent',1,0) as GradStudent,
etc.
FROM myTable
GROUP BY ReqNumber;
A different approach would be to JOIN multiple subqueries. This example pulls in 2 of your categories. If you need to extend it to 9 categories, you would have a whole lot of joining going on.
SELECT
itsb.ReqNumber,
itsb.ITSchoolboy,
grad.GradStudent
FROM
(
SELECT
ReqNumber,
FilledFlag AS ITSchoolboy
FROM myTable
WHERE FilledBy = "ITSchoolboy"
) AS itsb
INNER JOIN
(
SELECT
ReqNumber,
FilledFlag AS GradStudent
FROM myTable
WHERE FilledBy = "GradStudent"
) AS grad
ON itsb.ReqNumber = grad.ReqNumber
Please notice I'm not suggesting you should use this approach. However, since you asked about alternatives to your pivot approach (which works) ... this is one. Stay tuned in case someone else offers a simpler alternative. :-)