VB.NET adding Rows and columns - mysql

I'm after a way to add multiple amounts in different columns in a certain date_code, i've used the select sum to add amounts in a single column, but i have no an idea on how to add multiple column rows up, if you get what I mean, my sql database is as follows:
Date amount1 amount2 amount3 amount4 amount5
02/10/2014 2 3 6 0 5
02/10/2014 3 1 2 0 3
02/10/2014 0 4 6 6 2
02/10/2014 2 0 9 3 6
02/10/2014 7 0 8 2 9
Im need the total of all amounts in the same date range.
Total would be: 71
Any help would be great,
Many Thanks,
Pete
This is the code i've got so far which adds the amount, but only in one column:
connect() cmd.CommandText = "SELECT SUM(amount1) FROM sqlccmbuildtimecount WHERE datevar = '" & temp_day & "/" & temp_month & "/" & temp_year & "'"
endvalue.Text = cmd.ExecuteScalar() disconnect()
i've set temp_day to "02", temp_month to "10" and temp_year to "2014"

Sum is not 71, should be 89
Like the_lotus said,
do:
SELECT SUM(amount1) + SUM(amount2) + SUM(amount3) + SUM(amount4) + SUM(amount5) FROM sqlccmbuildtimecount WHERE datevar = '" & temp_day & "/" & temp_month & "/" & temp_year & "'"

Related

How to use sum function based on other columns in 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?

Need help to update a sql table from excel sheet using vb script

Thank you in advance.
My aim is to update sql table from excel. So i wrote a vbscript to get sql table from my-sql to excel on a single click. Table looks like this:-
id rule length isactive date
1 rule 1 3 1 NULL
2 rule 2 5 0 NULL
3 rule 3 6 1 NULL
4 rule 4 7 1 8/10/2015
5 rule 5 3 0 NULL
Now i will update length, isactive, date fields and this data should be updated in the table on a single click. Please help me in this script. I tried update query which is failing with run time error-
80040e14.
part of script is
For iCount = 2 To 6
intRLength = CInt(objSheet.Cells(iCount, 3).Value)
intID = CInt(objSheet.Cells(iCount, 1).Value)
strQuerie = "UPDATE" & strDatabase & "." & strTable & "SET retentionlength=" & intRLenth & "where id= " & intID
rs.Open strQuerie ' --- Getting run time error- 80040e14 message here
Next
That's most probably because of spacing issue in some part of your formed query
Your query should rather look like
strQuerie = "UPDATE " & strDatabase & "." & strTable & " SET retentionlength=" & intRLenth & " where id= " & intID
Otherwise, as it stands; your query would look like below which clearly way wrong
UPDATEDB_NAME.TABLE_NAMESET retentionlength=somevaluewhere id= someid

Access vba random function not working

I have a sub that is supposed to take a number of assignments (this number changes daily) and assign them to 7 associates. There are some conditions to this:
If the # of assignment is less than 7, it assigns all of them to a random associate.
If the # is divisible by 7, it assigns an equal number to each.
If it is not divisible by 7, it assigns equally and then gives the remainder to a random associate.
The problem is the random part. I really don't understand how random works in vba, or at least it seems like it should be super easy, but it's not (maybe). But I have this written and it's not working. (Associates(Int(Rnd() * 7) + 1)). Here is my relevant code:
Earlier in the sub I create an array of the associates and I use some dcounts to get the total assignments for that day:
Dim Associates(6) As Integer
Associates(0) = 4687 'Anita
Associates(1) = 4247 'Alberto
Associates(2) = 2167 'Jeff
Associates(3) = 4334 'Lisa
Associates(4) = 4441 'Carrie
Associates(5) = 2052 'Bobby
Associates(6) = 4657 'Simona
'
Dim Person As Variant
'
TotalPop = DCount("LNo", "qry_PT_Assign")
FractionPop = Int(TotalPop / 7)
LeftPop = TotalPop - (FractionPop * 7)
'
and then I try to actually assign them.
'Assign to Associates
If TotalPop < 7 Then
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
ElseIf LeftPop = 0 Then
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
Else
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
End If
As per my comment, try generating the random number first, assign it to a variable, and then pass the variable into Associates().
Dim rndInt as Integer
rndInt = Int(Rnd() * 7) + 1
Associates(rndInt)
Then as #Chips said, you can use
Debug.print rndInt
Or
Msgbox rndInt
to check its value
That way you'll be able to see what number is actually being generated
The trick is to use a negative seed that changes constantly.
So add a time dependant seed to Rnd which changes for every unique id like in this sample select query:
SELECT
Table1.ID,
Table1.SomeField,
Table1.AnotherField,
Rnd(-Timer()*[ID]) AS RandomIndex
FROM
Table1
ORDER BY
Rnd(-Timer()*[ID]);
In your code, the expression could be something like this:
.. " & (Associates(Int(Rnd(-Timer()*" & [ID] & ") * 7) + 1)) & " ..

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"