So trying to keep this section very brief:
I have two tables. Tbl1 has the financial accounts by year for each company in seperate rows.
Table2 has each company only once and all the financial data is now in one row.
How do i do that? Currently attempting it with Collections have a second attempt going with Arrays.
Hi guys so i have two tables Figs1 and Sabi. Figs1 is set up like: NIF,PeriodEnding, Materials, Depreciation, Non-Trading Income, Total Interest, Pretax Profit, TotalEmpRemu.
So you'll get repeating Company IDs with each each of their financials as the rows.
In Sabi it changes to each company has 1 row and all that data is in columns e.g. PeriodEnding_Latest, PeriodEnding -1, PeriodEnding -2 etc. till -6. I have made a collection for each column in Figs1 and i want to update the table Sabi in the correct order.
So PeriodEnding collection will have {(31/12/2018), (31/12/2017), (31/12/2016), (31/12/2015), (31/12/2014), (31/12/2013)}
Those values need to go to PeriodEnding_Latest, PeriodEnding -1, PeriodEnding -2 etc.
I have the update SQL Statement and filled it with variables:
SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '& DateFiled1 &', SabiFigures1.[Closing Date Year - 1] = '& DateFiled2 &', " & _
"SabiFigures1.[Closing Date Year - 2] = '& DateFiled3 &', SabiFigures1.[Closing Date Year - 3] = '& DateFiled4 &', SabiFigures1.[Closing Date Year - 4] = '& DateFiled5 &, " & _
"SabiFigures1.[Closing Date Year - 5] = '& DateFiled6 &', SabiFigures1.[Material costs th EUR Last avail yr] = '2933', SabiFigures1.[Material costs th EUR Year - 1] " & _
"= '2791', SabiFigures1.[Material costs th EUR Year - 2] = '3721', SabiFigures1.[Material costs th EUR Year - 3] = '3021', SabiFigures1.[Material costs th EUR Year - 4] " & _
"= '3005', SabiFigures1.[Material costs th EUR Year - 5] = '1890', SabiFigures1.[Depreciation th EUR Last avail yr] = '49', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
"'52', SabiFigures1.[Depreciation th EUR Year - 2] = '47', SabiFigures1.[Depreciation th EUR Year - 3] = '42', SabiFigures1.[Depreciation th EUR Year - 4] = '54', " & _
"SabiFigures1.[Depreciation th EUR Year - 5] = '63', SabiFigures1.[Financial revenue th EUR Last avail yr] = Null, SabiFigures1.[Financial revenue th EUR Year - 1] " & _
"= Null, SabiFigures1.[Financial revenue th EUR Year - 2] = Null, SabiFigures1.[Financial revenue th EUR Year - 3] = Null, SabiFigures1.[Financial revenue th EUR Year " & _
"- 4] = Null, SabiFigures1.[Financial revenue th EUR Year - 5] = Null, SabiFigures1.[Financial expenses th EUR Last avail yr] = Null, SabiFigures1.[Financial expenses " & _
"th EUR Year - 1] = Null, SabiFigures1.[Financial expenses th EUR Year - 2] = Null, SabiFigures1.[Financial expenses th EUR Year - 3] = Null, " & _
"SabiFigures1.[Financial expenses th EUR Year - 4] = Null, SabiFigures1.[Financial expenses th EUR Year - 5] = Null, SabiFigures1." & _
"[P/L before tax th EUR Last avail yr] = '407', SabiFigures1.[P/L before tax th EUR Year - 1] = '252', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
"= '1076', SabiFigures1.[P/L before tax th EUR Year - 3] = '597', SabiFigures1.[P/L before tax th EUR Year - 4] = '329', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
"'102', SabiFigures1.[Cost of employees th EUR Last avail yr] = '1226', SabiFigures1.[Cost of employees th EUR Year - 1] = '1205', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
"= '1310', SabiFigures1.[Cost of employees th EUR Year - 3] = '1157', SabiFigures1.[Cost of employees th EUR Year - 4] = '1319', SabiFigures1.[Cost of employees th EUR Year - 5] = '1342' " & _
"WHERE (((SabiFigures1.[NIF Code])='A01011550'));"
db.Execute SQL
The code pretty much goes to first table: Figs1 and get the first regnumber it will then go to Sabi where i have prepopulated the unqiue NIFs. If it finds a correspondencing NIF in Figs1 and Sabi then it should fill out the variables from the collection however, i dont know how to do variable "variables". E.g. the base structure of the variable should be DateFiled but as it loops through the collection of PeriodEndings it should change from DateFiled1 till Datefiled6.
Set rsFigs1 = CurrentDb.OpenRecordset("Select * FROM Figs1Ready ORDER BY NIF, PeriodEnding DESC;")
If Not (rsFigs1.EOF And rsFigs1.BOF) Then
rsFigs1.MoveFirst
Do Until rsFigs1.EOF = True
NIF = rsFigs1!NIF
Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
If NIF = nIFF Then
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
Set PeriodEnding = RSToColl(rstDef, "PeriodEnding")
Set Materials1 = RSToColl(rstDef, "Materials")
Set Depreciation1 = RSToColl(rstDef, "Depreciation")
Set NonTrading1 = RSToColl(rstDef, "Non-Trading Income")
Set TotalInterest = RSToColl(rstDef, "Total_Interest_Charges")
Set Pretax = RSToColl(rstDef, "Pretax_Profit")
Set TotalRemu = RSToColl(rstDef, "Total_Empl_Remu_000")
For i = 1 To 6
VariableName = "DateFiled" & i
Next i
For Each Period In PeriodEnding
Debug.Print TypeName(Period)
Next
End If
SQL = 'The massive SQL statement above
db.Execute SQL
rsFormat.MoveNext
Loop
End If
rsFigs1.MoveNext
Loop
End If
rstDef.Close
Set rstDef = Nothing
rsFormat.Close
Set rsFormat = Nothing
rsFigs1.Close
Set rsFigs1 = Nothing
Maybe i'm looking at the problem in the wrong way any help and pointers would be appreciated. Thanks in advance. Added some pictures hopefully it helps
To Gustav who first suggested an Array i can do something like this:
This will bring back every year(row) individually. I would like to update a row in one go to save on time instead updating each yearly value. Pretty much updating each row 6 times which would take a lot longer I imagine.
Set rsFormat = CurrentDb.OpenRecordset("Select * FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
rstDef.MoveLast
rstDef.MoveFirst
varRecord = rstDef.GetRows(rstDef.RecordCount)
For intI = 0 To 5 'UBound(varRecord, 2)
For intJ = 0 To UBound(varRecord, 1)
Debug.Print varRecord(intJ, intI)
Next intJ
Next intI
'whole row updated here after each value of the array is passed to variables for each year and financial value.
rstDef.Close
Set rstDef = Nothing
rsFormat.MoveNext
Loop
End If
Hi there so i figured it out. The code is very bulky so anyone who can make it a bit more streamlined and flexible e.g. it the columns i need changed would be much appreciated.
Dim db As DAO.Database
Set db = CurrentDb
Dim rsFormat As DAO.Recordset
Dim rsFigs1 As Object
Dim qdfDef As DAO.QueryDef
Dim rstDef As Object
Dim varRecord As Variant
Dim NIF As String
Dim nIFF As String
Dim intI As Integer
Dim intJ As Integer
Dim RegNum, LatestDate, Date1, Date2, Date3, Date4, Date5, LatestMaterial, Material1, Material2, Material3, Material4, Material5, LatestDepreciation, Depreciation1, Depreciation2 As String
Dim Depreciation3, Depreciation4, Depreciation5, LatestTrading, Trading1, Trading2, Trading3, Trading4, Trading5, LatestTotalInterest, TotalInterest1, TotalInterest2, TotalInterest3 As String
Dim TotalInterest4, TotalInterest5, LatestPreTaxProfit, PreTaxProfit1, PreTaxProfit2, PreTaxProfit3, PreTaxProfit4, PreTaxProfit5, LatestTotEmpRem, TotEmpRem1, TotEmpRem2, TotEmpRem3 As String
Dim TotEmpRem4, TotEmpRem5, SQL As String
Set rsFormat = CurrentDb.OpenRecordset("Select [NIF Code] FROM SabiFigures1;")
If Not (rsFormat.EOF And rsFormat.BOF) Then
rsFormat.MoveFirst
Do Until rsFormat.EOF = True
nIFF = rsFormat![NIF Code]
Set qdfDef = CurrentDb.QueryDefs("PopulateSabiFigures1")
qdfDef.Parameters("NIF: ").Value = nIFF
Set rstDef = qdfDef.OpenRecordset()
rstDef.MoveLast
rstDef.MoveFirst
varRecord = rstDef.GetRows(rstDef.RecordCount)
For intI = 0 To 5
For intJ = 0 To UBound(varRecord, 1)
Debug.Print varRecord(intJ, intI)
On Error Resume Next
If intI = 0 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
LatestDate = varRecord(intJ, intI)
ElseIf intJ = 2 Then
LatestMaterial = varRecord(intJ, intI)
ElseIf intJ = 3 Then
LatestDepreciation = varRecord(intJ, intI)
ElseIf intJ = 4 Then
LatestTrading = varRecord(intJ, intI)
ElseIf intJ = 5 Then
LatestTotalInterest = varRecord(intJ, intI)
ElseIf intJ = 6 Then
LatestPreTaxProfit = varRecord(intJ, intI)
ElseIf intJ = 7 Then
LatestTotEmpRem = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 1 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date1 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material1 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation1 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading1 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest1 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit1 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem1 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 2 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date2 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material2 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation2 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading2 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest2 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit2 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem2 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 3 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date3 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material3 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation3 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading3 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest3 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit3 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem3 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 4 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date4 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material4 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation4 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading4 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest4 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit4 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem4 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
ElseIf intI = 5 Then
If intJ = 0 Then
RegNum = varRecord(intJ, intI)
ElseIf intJ = 1 Then
Date5 = varRecord(intJ, intI)
ElseIf intJ = 2 Then
Material5 = varRecord(intJ, intI)
ElseIf intJ = 3 Then
Depreciation5 = varRecord(intJ, intI)
ElseIf intJ = 4 Then
Trading5 = varRecord(intJ, intI)
ElseIf intJ = 5 Then
TotalInterest5 = varRecord(intJ, intI)
ElseIf intJ = 6 Then
PreTaxProfit5 = varRecord(intJ, intI)
ElseIf intJ = 7 Then
TotEmpRem5 = varRecord(intJ, intI)
Else
MsgBox "Error in Loop"
Exit Sub
End If
Else
MsgBox "Error in Loop"
Exit Sub
End If
Next intJ
Next intI
SQL = "UPDATE SabiFigures1 SET SabiFigures1.[Closing Date Last avail yr] = '" & LatestDate & "', SabiFigures1.[Closing Date Year - 1] = '" & Date1 & "', " & _
"SabiFigures1.[Closing Date Year - 2] = '" & Date2 & "', SabiFigures1.[Closing Date Year - 3] = '" & Date3 & "', SabiFigures1.[Closing Date Year - 4] = '" & Date4 & "', " & _
"SabiFigures1.[Closing Date Year - 5] = '" & Date5 & "', SabiFigures1.[Material costs th EUR Last avail yr] = '" & LatestMaterial & "', SabiFigures1.[Material costs th EUR Year - 1] " & _
"= '" & Material1 & "', SabiFigures1.[Material costs th EUR Year - 2] = '" & Material2 & "', SabiFigures1.[Material costs th EUR Year - 3] = '" & Material3 & "', SabiFigures1.[Material costs th EUR Year - 4] " & _
"= '" & Material4 & "', SabiFigures1.[Material costs th EUR Year - 5] = '" & Material5 & "', SabiFigures1.[Depreciation th EUR Last avail yr] = '" & LatestDepreciation & "', SabiFigures1.[Depreciation th EUR Year - 1] = " & _
"'" & Depreciation1 & "', SabiFigures1.[Depreciation th EUR Year - 2] = '" & Depreciation2 & "', SabiFigures1.[Depreciation th EUR Year - 3] = '" & Depreciation3 & "', SabiFigures1.[Depreciation th EUR Year - 4] = '" & Depreciation4 & "', " & _
"SabiFigures1.[Depreciation th EUR Year - 5] = '" & Depreciation5 & "', SabiFigures1.[Financial revenue th EUR Last avail yr] = '" & LatestTrading & "', SabiFigures1.[Financial revenue th EUR Year - 1] " & _
"= '" & Trading1 & "', SabiFigures1.[Financial revenue th EUR Year - 2] = '" & Trading2 & "', SabiFigures1.[Financial revenue th EUR Year - 3] = '" & Trading3 & "', SabiFigures1.[Financial revenue th EUR Year " & _
"- 4] = '" & Trading4 & "', SabiFigures1.[Financial revenue th EUR Year - 5] = '" & Trading5 & "', SabiFigures1.[Financial expenses th EUR Last avail yr] = '" & LatestTotalInterest & "', SabiFigures1.[Financial expenses " & _
"th EUR Year - 1] = '" & TotalInterest1 & "', SabiFigures1.[Financial expenses th EUR Year - 2] = '" & TotalInterest2 & "', SabiFigures1.[Financial expenses th EUR Year - 3] = '" & TotalInterest3 & "', " & _
"SabiFigures1.[Financial expenses th EUR Year - 4] = '" & TotalInterest4 & "', SabiFigures1.[Financial expenses th EUR Year - 5] = '" & TotalInterest5 & "', SabiFigures1." & _
"[P/L before tax th EUR Last avail yr] = '" & LatestPreTaxProfit & "', SabiFigures1.[P/L before tax th EUR Year - 1] = '" & PreTaxProfit1 & "', SabiFigures1.[P/L before tax th EUR Year - 2] " & _
"= '" & PreTaxProfit2 & "', SabiFigures1.[P/L before tax th EUR Year - 3] = '" & PreTaxProfit3 & "', SabiFigures1.[P/L before tax th EUR Year - 4] = '" & PreTaxProfit4 & "', SabiFigures1.[P/L before tax th EUR Year - 5] = " & _
"'" & PreTaxProfit5 & "', SabiFigures1.[Cost of employees th EUR Last avail yr] = '" & LatestTotEmpRem & "', SabiFigures1.[Cost of employees th EUR Year - 1] = '" & TotEmpRem1 & "', SabiFigures1.[Cost of employees th EUR Year - 2] " & _
"= '" & TotEmpRem2 & "', SabiFigures1.[Cost of employees th EUR Year - 3] = '" & TotEmpRem3 & "', SabiFigures1.[Cost of employees th EUR Year - 4] = '" & TotEmpRem4 & "', SabiFigures1.[Cost of employees th EUR Year - 5] = '" & TotEmpRem5 & "' " & _
"WHERE (((SabiFigures1.[NIF Code])='" & RegNum & "'));"
db.Execute SQL
SQL = ""
RegNum = ""
LatestDate = ""
Date1 = ""
Date2 = ""
Date3 = ""
Date4 = ""
Date5 = ""
LatestMaterial = ""
Material1 = ""
Material2 = ""
Material3 = ""
Material4 = ""
Material5 = ""
LatestDepreciation = ""
Depreciation1 = ""
Depreciation2 = ""
Depreciation3 = ""
Depreciation4 = ""
Depreciation5 = ""
LatestTrading = ""
Trading1 = ""
Trading2 = ""
Trading3 = ""
Trading4 = ""
Trading5 = ""
LatestTotalInterest = ""
TotalInterest1 = ""
TotalInterest2 = ""
TotalInterest3 = ""
TotalInterest4 = ""
TotalInterest5 = ""
LatestPreTaxProfit = ""
PreTaxProfit1 = ""
PreTaxProfit2 = ""
PreTaxProfit3 = ""
PreTaxProfit4 = ""
PreTaxProfit5 = ""
LatestTotEmpRem = ""
TotEmpRem1 = ""
TotEmpRem2 = ""
TotEmpRem3 = ""
TotEmpRem4 = ""
TotEmpRem5 = ""
'For intI = 6 To UBound(varRecord, 2)
' For intJ = 0 To UBound(varRecord, 1)
' Debug.Print varRecord(intJ, intI)
' Next intJ
'Next intI
rstDef.Close
Set rstDef = Nothing
rsFormat.MoveNext
Loop
End If
Table: scores
Judge No Name Casual Barong Talent Swimsuit Formal
Judge1 Ginoo 1 John 85 85 85 85 85
Judge2 Ginoo 1 John 84 86 88 82 83
Judge3 Ginoo 1 John 90 86 84 87 87
Judge1 Ginoo 2 David 85 85 85 85 85
Judge2 Ginoo 2 David 89 81 83 84 85
Judge3 Ginoo 2 David 87 84 83 87 88
Table: textvote
No Sender
Ginoo 1 9307895654
Ginoo 1 9566551234
Ginoo 1 9232235643
Ginoo 2 9225557878
Query Result
Rank No Name Casual Barong Talent Swimsuit Formal Textvote Total
Champion Ginoo 1 John 86.33 85.67 85.67 84.67 85.00 93.75 86.73
1stRunup Ginoo 2 David 87.00 83.33 83.67 85.33 86.00 81.25 84.32
2ndRunup
This is my latest code:
"SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal, " & _
"(select count(*) / (select count(*) from (textvote) where no like '%Ginoo%') * (100 / count(*)) + (100 - (100 / count(*))) AS 'Text Vote', " & _
"(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) + " & _
"(select count(*) / (select count(*) from (textvote) where no like '%Ginoo%') * (100 / count(*)) + (100 - (100 / count(*))) * 0.15 AS Total " & _
"FROM scores s " & _
"INNER JOIN textvote t ON s.no = t.no " & _
"WHERE t.no LIKE '%Ginoo%' " & _
"GROUP BY t.no"
I got this error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near " at line 1
Until I know how your calculating textvote I have left textvotecalc blank (fill in yourself):
SELECT s.candidate, AVG(s.gown) AS "gown 30%",
AVG(s.talent) AS "talent 30%",
?textvotecalc? AS "textvote 40% ",
((AVG(s.gown)*100)/30) + ((AVG(s.talent)*100)/30) + ((?textvotecalc?*100)/40) AS TOTAL
FROM scores s
INNER JOIN textvote t ON s.candidate = t.candidate
GROUP BY s.candidate
EDIT:
You had an extra ) at the end of the TOTAL calculation.
SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal,
(COUNT(t.no) / " & TextVoteCountGinoo & ") * (" & TextVoteCountGinoo & " - COUNT(t.no)) + (" & TextVoteCountGinoo & " - (" & TextVoteCountGinoo & " - COUNT(t.no))) AS'Text Vote',
(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) + (COUNT(t.no) / " & TextVoteCountGinoo & ") * (100 / " & TextVoteCountGinoo & ") + (100 - (100 / " & TextVoteCountGinoo & ") ) * 0.15 AS Total
FROM scores s
INNER JOIN textvote t ON s.no = t.no
WHERE s.no LIKE '%Ginoo%'
GROUP BY s.no
In your VB format:
SELECT s.no, s.name, AVG(s.casual) AS Casual, AVG(s.barong) AS Barong, AVG(s.swimsuit) AS Swimsuit, AVG(s.formal) AS Formal, & _
"(COUNT(t.no) / " & TextVoteCountGinoo & ") * (" & TextVoteCountGinoo & " - COUNT(t.no)) + (" & TextVoteCountGinoo & " - (" & TextVoteCountGinoo & " - COUNT(t.no))) AS'Text Vote'," & _
"(AVG(s.casual) * 0.15) + (AVG(s.barong) * 0.25) + (AVG(s.swimsuit) * 0.15) + (AVG(s.formal) * 0.15) +" & _ "(COUNT(t.no) / " & TextVoteCountGinoo & ") * (100 / " & TextVoteCountGinoo & ") + (100 - (100 / " & TextVoteCountGinoo & ") ) * 0.15 AS Total" & _
"FROM scores s
INNER JOIN textvote t ON s.no = t.no
WHERE s.no LIKE '%Ginoo%'
GROUP BY s.no"
I currently have a statement along the lines of:
SELECT
SUM(claims.sanctioned_resultant) AS 'Resultant',
SUM(claims.sanctioned_flooring) AS 'Flooring',
SUM(claims.sanctioned_contents) AS 'Furniture',
etc, and I need to include a "WHERE" in this somehow. Along the lines of:
SUM(claims.sanctioned_resultant WHERE claims.a_column = 1)
Obviously this doesn't work, since SUM just.. sums. I currently have a whole other query with the WHERE claims_a_column = 1 clause, then later combine the results. The "WHERE" condition cannot be added to this query, as the majority of results won't be selected by it.
I've looked into CASE WHEN, and sub-selects, but can't figure out the actual syntax.
Full query, in case it helps:
SELECT
CONCAT(users.user_first_name,IF((users.user_surname<>''),CONCAT(' ',users.user_surname),'')) as 'Surveyor',
SUM(claims.sanctioned_roof + claims.sanctioned_building + claims.sanctioned_internal + claims.sanctioned_resultant + claims.sanctioned_flooring + claims.sanctioned_contents) AS 'Overall',
SUM(claims.sanctioned_roof) AS 'Roofing',
SUM(claims.sanctioned_building + claims.sanctioned_internal) AS 'Building',
SUM(claims.sanctioned_resultant) AS 'Resultant',
SUM(claims.sanctioned_flooring) AS 'Flooring',
SUM(claims.sanctioned_contents) AS 'Furniture',
COUNT(CASE WHEN claims.claimed_amount > 0 THEN 1 END) AS 'OverallCount',
COUNT(CASE WHEN (claims.claimed_amount > 0 AND claims.peril_id IN (" . implode(',', $WRPPerils) . ")) THEN 1 END) AS 'WRPCount',
COUNT(CASE WHEN claims.claimed_roof > 0 THEN 1 END) AS 'RoofingCount',
COUNT(CASE WHEN (claims.claimed_building > 0) OR (claims.sanctioned_internal > 0) THEN 1 END) AS 'BuildingCount',
COUNT(CASE WHEN claims.claimed_resultant > 0 THEN 1 END) AS 'ResultantCount',
COUNT(CASE WHEN claims.claimed_flooring > 0 THEN 1 END) AS 'FlooringCount',
COUNT(CASE WHEN claims.claimed_contents > 0 THEN 1 END) AS 'FurnitureCount',
COUNT(CASE WHEN claims.claimed_amount > 0 AND claims.letter_of_findings = 'Y' THEN 1 END) AS 'LOFCount'
FROM
claims claims
INNER JOIN users users ON claims.surveyor_id = users.user_id
INNER JOIN insurers insurers on claims.insurer_id = insurers.insurer_id
WHERE
(claims.claim_type <> " . $conn->qstr(TYPE_DESKTOP) . ") AND
(claims.claim_type <> " . $conn->qstr(TYPE_AUDIT) . ") AND
(claims.claim_cancelled_id <= 0) AND
(claims.date_completed BETWEEN '" . $start_date . " 00:00:00' AND '" . $end_date . " 23:59:59') AND
(claims.overturn_confirmed = 'N' OR claims.overturn_confirmed IS NULL)
GROUP BY surveyor_id
I think the easiest way to do what you want will be using a CASE statement within the SUM function itself, like this:
SUM(CASE WHEN claims.a_column = 1 THEN claims.sanctioned_resultant ELSE 0 END)