ASKER
ASKER
I made a calculation through code vb and datatables
ASKER
SELECT Factor
FROM <your table>
WHERE <date field> >= <start date range> AND <date field> < <end date range>
AND <location field> = <location value>
AND <type field> = <type value>
AND <color field> >= <start color range> AND <color field <= <end color range>
AND <density field> >= <start density range> AND <density field> < <end density range>
ASKER
Public Shared Sub Getfactor(ByRef RowG As DataRow, ByVal TbFactor As DataTable)
Dim LTypes As New List(Of String)
Dim mtype As String = If(IsDBNull(RowG("idType")), "", RowG("idType"))
Dim VBonusQ As Decimal = 0
Dim CBonusQ As Decimal = 0
If TbFactor.Rows.Count > 0 Then
LTypes.Clear()
For v = 0 To TbFactor.Rows.Count - 1
Dim xV As String = If(IsDBNull(TbFactor.Rows.Item(v)("TypeName")), "", TbFactor.Rows.Item(v)("TypeName"))
If xV.Trim.Length > 0 Then
If Not LTypes.Contains(xV) Then
LTypes.Add(xV)
End If
End If
Next
Dim xQueryType As String = " and Type = '' "
If mtype.Length > 0 Then
If LTypes.Contains(mtype) Then
xQueryType = " and Type = '" & mtype & "' "
End If
End If
Dim xLoc As String = If(IsDBNull(RowG("Location")), "", RowG("Location"))
If xLoc.Length > 0 Then
Dim RowA() As DataRow = TbFactor.Select("CodContrat = '" & RowG("CodContrat") & "' and Location = '" &
RowG("Location") & "' " & xQueryType, "date,density")
If Not RowA Is Nothing Then
If RowA.Length > 0 Then
If Not IsDBNull(RowG("OrderDate")) Then
Dim OrderDate As Date = RowG("OrderDate")
Dim Dens As Decimal = FormatNumber(If(IsDBNull(RowG("Density")), 0, RowG("Density")), 3)
For i = RowA.Length - 1 To 0 Step -1
If RowA(i)("Date") <= OrderDate Then
Dim RowAlineas() As DataRow = TbFactor.Select("CodContrat = '" & RowG("CodContrat") & "' and Location = '" &
RowG("Location") & "' and date = '" & Format(RowA(i)("date"), "yyyy-MM-dd") &
"' " & xQueryType, "Date,density")
For a = RowAlineas.Length - 1 To 0 Step -1
If RowAlineas(a)("density") <= Dens Then
RowG("Factor") = RowAlineas(a)("Factor")
Exit For
End If
Next
Exit For
End If
Next
End If
End If
End If
End If
End If
End Sub
ASKER
ASKER
SELECT Factor
FROM factor
WHERE '02/05/2018' >= '01/05/2018' AND '02/05/2018' < '15/05/2018'
AND Location = 05
AND type = 'Type 1'
AND 0.13 >= 0 AND 0.13 <= 2.31
AND 4.25 >= 0 AND 4.25 < 5.5
select Date,Color,Density,Type,Location,Factor
from yourtablename
where date >= '01/05/2018' AND date < '15/05/2018'
AND Location = 05
AND type = 'Type 1'
AND color between 0 AND 2.31
AND density between 0 AND 5.5
ORDER BY Factor desc
And if you want to sort or rank, you can add in constructs like row_number() , top, or add to the bottom, an ORDER BY, or a range of other possibilities
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
What do we want to do with Factor ? Calculate it, or use it for something ? Or, emulate your VB code but in SQL ?
The table.xlsx seems to have a factor in there for everything, but missing some colors and density.
If you could please explain a bit more, might be able to help you.