How to use sum function based on other columns in MySQL - mysql

This is my table:
SubjectCode | Unit | Instructor
IS 26 3 James
IS 26 3 James
The result should be:
SubjectCode | Unit | Instructor
IS 26 3 James
My code:
msql = "SELECT Sum(Unit) AS Unit" & _
" From classprogram " & _
" WHERE Instructor= '" & cboInstructor.Text & "'and Semester='" & cboSemester.Text & "'" & _
" GROUP BY CONCAT_WS(' ', Instructor, SubjectCode);"
I want the answer of that is 3.
How can I proceed further?

Related

Mismatch with Dlookup using multiple criteria and Yes/NO

I have a table that has multiple booths assigned to a process and a Yes/NO field for the primary booth for that process.
Process | Booth | Primary
Buff1 | 8 | No
Buff1 | 5 | Yes
etc.
I am trying to automate a form to fill in the primary booth on a field when the process gets selected using Dlookup.
This is the code I am using:
Me.Booth.Value = DLookup("Booth", "BoothSource", "BoothSource.Process = '" & Me.Process & "' AND BoothSource.Primary = '" & "True" & "'")
I am sure it is the Yes/No field that I'm having problems with. Please help.
True is not a string, so:
Me.Booth.Value = DLookup("Booth", "BoothSource", "BoothSource.Process = '" & Me.Process & "' AND BoothSource.Primary = True")
Normally, table name is left out in the criteria:
Me!Booth.Value = DLookup("Booth", "BoothSource", "[Process] = '" & Me!Process.Value & "' AND [Primary] = True")

how to query full name with null values in MS Access?

This is my code in ms access in giving the full name with null values
lname for last name
fname for first name
mname for middle name (change into initial)
x for name extensions like Jr., Sr., III ect.
this is the code.
[lname] & ", " & [fname] & " " & IIf(IsNull([x]),"",([x]) & ", ") & " " & IIf(IsNull(Left([mname],1)),"",(Left([mname],1)) & ".")
but when i try it to mysql it does not give any data. anyone is free to edit or reconstruct my code to fit the qualifications in mysql query. SALAMAT, MABUHAY

DCount returning 0 but should return 1

I have the following DCount code in my VBA:
If DCount("*", "tbl2Employee_Order", _
"[Operation_Date] = #" & Operation_Date & _
"# AND [Employee_ID]= " & Employee_ID & _
" AND [Order_ID] = " & Order_ID & _
" AND [Model_Operation_ID] = " & MO_ID) = 0 Then
'some code to insert into tbl2Employee_Order
Else
'some code to update the existing record
End If
However, my DCount always returns 0, even if the record already exists. The following:
Debug.Print Operation_Date, Employee_ID, Order_ID, MO_ID, DCount("*", "tbl2Employee_Order", "[Operation_Date] = #" & Operation_Date & "# AND [Employee_ID]= " & Employee_ID & " AND [Order_ID] = " & Order_ID & " AND [Model_Operation_ID] = " & MO_ID)
Returns the expected values such as:
08/05/2015 2 526 1107 0
Apart from the last one, which is expected to be 1 (record already exists).
tbl2EmployeeOrder has this record:
Operation_Date: 08/05/2015
Employee_ID: 2
Order_ID: 526
Model_Operation_ID = 1107
Interestingly enough, it used to work without problem a few weeks ago, suddenly it behaves as if the record doesn't already exist.
Edit:
The following:
Debug.Print TypeName(Operation_Date), TypeName(Employee_ID), TypeName(Order_ID), TypeName(MO_ID)
Results in:
Date Integer Integer Integer
And those are also the variable types in the tbl2Employee_Order.
Similarly, if I use DLookup (with some column name) instead of DCount I get a Null value returned.
did you change regional settings ? how about
If DCount("*", "tbl2Employee_Order", _
"[Operation_Date] = #" & Format(Operation_Date,"mm/dd/yyyy") & _
"# AND [Employee_ID]= " & Employee_ID & _
" AND [Order_ID] = " & Order_ID & _
" AND [Model_Operation_ID] = " & MO_ID) = 0 Then
remove the date criteria to see if it works

SQL SELECT Query on related table

I have a question for which I found a rather long solution of which I don't think that it is good practise.
However I feel there must be a syntax available that is able to solve this issue right away without too many complexities.
I bumped into this problem a few times already, and I'd like to know for once and for all.
I have a table called "T_STOP_LOSS", in which I have three columns "LIVES_FROM", "LIVES_TO" and "SL_VALUE" (All number types + the column containing ID with AutoNumber type).
I want to retrieve the record containing the SL_VALUE where a variable value lies in the interval between LIVES_FROM and LIVES_TO.
ID LIVES_FROM LIVES_TO NR_COUNTRIES_FROM NR_COUNTRIES_TO SL_VALUE
1 0 999 2 2 39,3
12 0 999 3 3 34,6
23 0 999 4 4 29,6
34 0 999 5 9 25,7
45 0 999 10 100 22,17
46 1000 1499 2 2 31,1
2 1000 1499 3 3 27,4
13 1000 1499 4 4 23,3
24 1000 1499 5 9 20,4
35 1000 1499 10 100 17,5
36 1500 1999 2 2 23,6
47 1500 1999 3 3 20,7
3 1500 1999 4 4 17,7
14 1500 1999 5 9 15,4
25 1500 1999 10 100 13,3
26 2000 2999 2 2 23,6
37 2000 2999 3 3 20,7
48 2000 2999 4 4 17,7
4 2000 2999 5 9 15,4
15 2000 2999 10 100 13,3
Normally I would use:
lNr_Lives = <Something> '(Retrieved via Textbox)
sSQL_Select = "SELECT SLVALUE FROM T_STOP_LOSS WHERE LIVES_FROM <= " & LNr_Lives & " AND LIVES_TO >= " & LNr_Lives & ";"
However, LIVES_FROM and LIVES_TO are related to the table NR_LIVES through a foreign key.
The above statement will never work because table 'NR_LIVES' contains an ID field as Primary Key, which is used to link to the T_STOP_LOSS table.
(The columns in NR_LIVES are named: ID, NR_LIVES_FROM and NR_LIVES_TO (not that it matters)).
ID NR_LIVES_FROM NR_LIVES_TO
1 0 999
2 1000 1499
3 1500 1999
4 2000 2999
Please tell me, what would you consider to be the briefest way to access the real value of LIVES_FROM and LIVES_TO that is stored in the NR_LIVES table and get my SELECT statement working the way I expect it (Return the record that contains the SL for the right interval).
I hope that my question is clear. If not, don't hesitate to mention.
Looks to me like you want to do some simple join queries. First check out these references here and here to get you more familiar with the approach.
Secondly, there is generally no reason to have the extra 'Lookup' columns in the child tables as these values can be accessed from an appropriate JOIN query.
I suspect the query you are looking for is:
SELECT SLVALUE FROM
T_STOP_LOSS
INNER JOIN
NR_LIVES
ON T_STOP_LOSS.ID=NR_LIVES.ID
WHERE
NR_LIVES_FROM <= LNr_Lives
AND
NR_LIVES_TO >= LNr_Lives ;
Thanks to Nicolas, I found what I needed.
Dim cQueries As clsQueries
Dim oRst As DAO.Recordset
Dim sSQL_Select As String
Dim LNr_Lives As Long
Dim iNr_Countries As Integer
Dim i As Integer
If Not IsNull(Me.txt_Nr_Lives) Then
LNr_Lives = Me.txt_Nr_Lives
Else
MsgBox "No number of lives defined!", vbCritical
Exit Sub
End If
If Not IsNull(Me.cbb_Nr_Countries) Then
iNr_Countries = Me.cbb_Nr_Countries
Else
MsgBox "No number of countries defined!", vbCritical
Exit Sub
End If
Set cQueries = New clsQueries
sSQL_Select = "SELECT " & _
"* " & _
"FROM " & _
"T_STOP_LOSS " & _
"INNER JOIN " & _
"NR_LIVES ON (T_STOP_LOSS.LIVES_FROM = NR_LIVES.ID) " & _
" WHERE " & _
"NR_LIVES.NR_LIVES_FROM <= " & LNr_Lives & _
" AND " & _
"NR_LIVES.NR_LIVES_TO >= " & LNr_Lives & _
" AND " & _
"T_STOP_LOSS.NR_COUNTRIES_FROM <= " & iNr_Countries & _
" AND " & _
"T_STOP_LOSS.NR_COUNTRIES_TO >= " & iNr_Countries & ";"
Set oRst = cQueries.Select_Statement(sSQL_Select)
oRst.MoveFirst
Do While Not oRst.EOF
Me.txt_FSL = oRst![SL_VALUE]
oRst.MoveNext
Loop

Using loop in a Access Query

I have a table named OT Hours which has the following column
EmpId, Date, Hours.
I need to insert a constant value in the hour column for 12 months prior to the current date for 6 employees.
Can I use a for loop in the query?
If yes, Please provide me with an example.
As of now, I can do it in VBA as follows:
Dim j As Integer
For j = -11 To 0
DoCmd.RunSQL "INSERT INTO tblOTHours (employeeNumber, theDate, HoursType, Position, hoursQuantity) VALUES ('" & S.sanitize(txtEmployeeNumber) & "',DateAdd('m'," & j & ",Format(Now(),'mm/dd/yyyy')),'OT1','" & cmb_position.value & "'," & Round(Val(rs("Avg")) / 12, 1) & ")"
Next
Note: I am using MS Access. Can I do this function in the query itself?
It can be useful to have a numbers table that holds integers from 1 or 0 to a suitably high number. Your query could take advantage of this table like so:
"INSERT INTO tblOTHours (theDate, employeeNumber, HoursType, [Position], hoursQuantity) " _
& "SELECT DateAdd('m',Number,Date()), '" & S.sanitize(txtEmployeeNumber) & "','OT1','" _
& cmb_position.value & "'," & Round(Val(rs("Avg")) / 12, 1) _
& " FROM Numbers " _
& "WHERE Numbers.Number<11"