Help please with this SQL statement?

Status
Not open for further replies.

indigo

Well-known member
5,606
OK, this is ASP/T-SQL, so it may be unfamiliar to some...

What I want is really easy... I'm just not too bright I guess, and my guru of choice is in a meeting for the day... so...

The code:
<code>
' Begin Query Logic
If trim(Request.QueryString("SearchFor") & " ") <> "" and trim(Request.QueryString("SearchFor") & " ") <> "Enter Keywords Here" Then
Session("SearchFor") = trim(Request.QueryString("SearchFor") & " ")
Else
Session("SearchFor") = ""
End If

If trim(Request.QueryString("OfType") & " ") <> "" Then
Session("OfType") = trim(Request.QueryString("OfType") & " ")
Else
Session("FAK") = ""
End If

intTerm = 0

Select Case trim(Request.QueryString("OfType") & " ")
Case "Origin"
intTerm = 1
Case "Destination"
intTerm = 2
Case "Commodity"
intTerm = 3
Case "Volume"
intTerm = 4
End Select



If trim(Request.QueryString("Direction") & " ") <> "" and trim(Request.QueryString("Direction") & " ") <> "All" Then
Session("Direction") = trim(Request.QueryString("Direction") & " ")
Else
Session("Direction") = ""
End If

If trim(Request.QueryString("FAK") & " ") <> "" Then
Session("FAK") = trim(Request.QueryString("FAK") & " ")
Else
Session("FAK") = ""
End If

If trim(Request.QueryString("Sort") & " ") <> "" Then
Session("Sort") = trim(Request.QueryString("Sort") & " ")
Else
Session("Sort") = "Desc"
End If

If trim(Request.QueryString("OrderBy") & " ") <> "" Then
Session("OrderBy") = trim(Request.QueryString("OrderBy") & " ")
Else
Session("OrderBy") = "ContractNum"
End If

Dim sqlText
Dim rsServiceContracts
sqlText = "Select Distinct ct.RecordStatus"
sqlText = sqlText & ", ct.TableId"
sqlText = sqlText & ", ct.ContractNum"
sqlText = sqlText & ", ct.CurrentAmendmentNum"
sqlText = sqlText & ", ct.Description"
sqlText = sqlText & ", ct.DateEffective"
sqlText = sqlText & ", ct.DateExpiration"
sqlText = sqlText & ", ct.DateFiling"
sqlText = sqlText & ", ct.FMCFilingNum"
If Session("Direction") <> "" Then sqlText = sqlText & ", SubTermNum AS SubTerm1"

sqlText = sqlText & " from Contract ct"

If (Session("OfType") = "Shipper" or Session("OfType") = "All") and trim(Session("SearchFor") & " ") <> "" Then
sqlText = sqlText & " left join Shipper sh on ct.ShipperTableID = sh.TableID"
End If

If ((intTerm <> 0 or Session("OfType") = "All") and trim(Session("SearchFor") & " ") <> "" ) or _
Session("Direction") <> "" Then
sqlText = sqlText & " left join ContractLines cl on ct.TableId = cl.ContractTableId"
End If

sqlText = sqlText & " where ct.CarrierId = " & Session("CarrierId")

If (Session("OfType") = "Shipper" or Session("OfType") = "All") and trim(Session("SearchFor") & " ") <> "" Then
sqlText = sqlText & " and "
If Session("OfType") = "All" and trim(Session("SearchFor") & " ") <> "" Then sqlText = sqlText & "("
sqlText = sqlText & "sh.ShipperName like '%" & FmtOdbcStrOut(Session("SearchFor")) & "%'"
End If

If ((intTerm <> 0 or Session("OfType") = "All") and trim(Session("SearchFor") & " ") <> "") or Session("Direction") <> "" Then
If intTerm <> 0 and trim(Session("SearchFor") & " ") <> "" Then
sqlText = sqlText & " and (cl.TermNum = " & intTerm
sqlText = sqlText & " and cl.SubTermNum = 0)"
End If
If Session("Direction") <> "" Then
If intTerm <> 1 Then
sqlText = sqlText & " and (cl.TermNum = 1"
sqlText = sqlText & " and cl.SubTermNum = 0"
sqlText = sqlText & " and SubTerm1 = 1)"
Else
sqlText = sqlText & " and (cl.TermNum = 1"
sqlText = sqlText & " and SubTerm1 = 1)"
End If
End If
If Session("OfType") = "All" and trim(Session("SearchFor") & " ") <> "" Then
If intTerm <> 1 and Session("Direction") <> "" Then sqlText = sqlText & " and cl.TermNum = 1"
If intTerm <> 2 Then sqlText = sqlText & " and cl.TermNum = 2"
If intTerm <> 3 Then sqlText = sqlText & " and cl.TermNum = 3"
If intTerm <> 4 Then sqlText = sqlText & " and cl.TermNum = 4"
sqlText = sqlText & " or"
ElseIf Session("Direction") = "" and intTerm <> 0 Then
sqlText = sqlText & " and"
End If
If trim(Session("SearchFor") & " ") <> "" Then
sqlText = sqlText & " (cl.TermShortDescription like '%" & FmtOdbcStrOut(Session("SearchFor")) & "%'"
sqlText = sqlText & " or cl.TermDescription like '%" & FmtOdbcStrOut(Session("SearchFor")) & "%'"
sqlText = sqlText & " or cl.ScopeDescription like '%" & FmtOdbcStrOut(Session("SearchFor")) & "%')"
End If
If Session("Direction") <> "" Then
If Session("Direction") = "Eastbound" or Session("Direction") = "Combination" Then
sqlText = sqlText & " and cl.TermShortDescription like '%EASTBOUND%'"
End If
If Session("Direction") = "Westbound" or Session("Direction") = "Combination" Then
sqlText = sqlText & " and cl.TermShortDescription like '%WESTBOUND%'"
End If
End If
End If

If Session("OfType") = "All" and trim(Session("SearchFor") & " ") <> "" Then sqlText = sqlText & ")"

If Session("MM_UserAuthorization") < 3 or Session["MM_UserAuthorization") > 4 Then
sqlText = sqlText & " and ct.RecordStatus <> 'H'"
Else
sqlText = sqlText & " and ct.RecordStatus = 'C'"
End If
sqlText = sqlText & " Order by ct.RecordStatus desc"
sqlText = sqlText & ", ct." & Session("OrderBy") & " " & Session("Sort")
Response.Write(sqlText)
Set rsServiceContracts = Conn.Execute(sqlText)

If Session("MM_UserAuthorization") <> 4 Then
If Session("AccessDate") < Date and Session["MM_UserAuthorization") <> 4 Then
strShowHistory = "Y"
sqlText = "Select RecordStatus"
sqlText = sqlText & ", TableId"
sqlText = sqlText & ", ContractNum"
sqlText = sqlText & ", CurrentAmendmentNum"
sqlText = sqlText & ", Description"
sqlText = sqlText & ", DateEffective"
sqlText = sqlText & ", DateExpiration"
sqlText = sqlText & ", DateFiling"
sqlText = sqlText & ", FMCFilingNum"
sqlText = sqlText & " from Contract"
sqlText = sqlText & " where CarrierId = " & Session("CarrierId")
sqlText = sqlText & " and DateExpiration >= '" & Session("AccessDate") & "'"
sqlText = sqlText & " and RecordStatus = 'H'"
sqlText = sqlText & " Order By RecordStatus desc"
sqlText = sqlText & ", " & Session("OrderBy") & " " & Session("Sort")
Set rsSCHistory = Conn.Execute(sqlText)
End If
End If
</code>

Should be pretty self explanatory. Keep in mind, this is pre-cleanup, before I "Find&Replace" Sessions with strings pulled from an array (which will be split() from either a single Session or cookie... I haven't decided yet) and the like.

RecordStatus = Current, History, Pending, Future, etc...

Once the main logic is done, it'll be a simple cut&paste for History.

There's money in tweaks later, but this has to go into production today so getting it working is the first priority...

So... I've messed up somewhere on my AS statement looks like. Here's the painstakingly recreated SQL statement if Direction = Eastbound and everything else is defaults (All or Null).

<code>
Select Distinct ct.RecordStatus, ct.TableId, ct.ContractNum, ct.CurrentAmendmentNum, ct.Description, ct.DateEffective, ct.DateExpiration, ct.DateFiling, ct.FMCFilingNum from Contract ct left join ContractLines cl on ct.TableId = cl.ContractTableId where ct.CarrierId = 50 and (cl.TermNum = 1 and cl.SubTermNum = 0 and cl.SubTermNum = 1) and cl.TermShortDescription like '%EASTBOUND%' and ct.RecordStatus <> 'H' Order by ct.RecordStatus desc, ct.ContractNum Desc
</code>

Yes, I know it's the ugliest code you've ever seen... I promise to clean it up later when I can spend some time on the couch mulling over the best way to tie the search function together... then I'll have my reports template...
 

indigo

Well-known member
5,606
Didn't label it, it's the code section at the bottom.

<code>
Select Distinct ct.RecordStatus, ct.TableId, ct.ContractNum, ct.CurrentAmendmentNum, ct.Description, ct.DateEffective, ct.DateExpiration, ct.DateFiling, ct.FMCFilingNum cl.SubTermNum AS cl.SubTerm from Contract ct left join ContractLines cl on ct.TableId = cl.ContractTableId where ct.CarrierId = 50 and (cl.TermNum = 1 and cl.SubTermNum = 0 and cl.SubTerm = 1) and cl.TermShortDescription like '%EASTBOUND%' and ct.RecordStatus <> 'H' Order by ct.RecordStatus desc, ct.ContractNum Desc</code>

Actually, that was a previous piece before the AS, here's the current one...

[This message was edited by indigo on January 16, 2002 at 12:24.]
 
From books online:
column_alias can be used in an ORDER BY clause. However, it cannot be used in a WHERE, GROUP BY, or HAVING clause. If the query expression is part of a DECLARE CURSOR statement, column_alias cannot be used in the FOR UPDATE clause.

So you have aliased cl.SubTermNum as cl.SubTerm, and then used the alias in your where clause. in addition, you have used both the aliased and non aliased values in the where clause, so even if that did work, you would not get any results unless 0 == 1, which isn't going to happen.

As a 3rd thing (and I am not 100% on this) I don't think you can use a table modifier in an alias, so you would have to change your alias to

code:<HR>
Code:
cl.SubTermNum AS SubTerm

This would make the statement work but i am not sure if you would get the corect results; I haven't looked at your code to work out exactly what you where trying to do there I'm afraid.
 

indigo

Well-known member
5,606
There should indeed... ;)

Thanx for the help guys... you're going to kill me when I say this though...

Throw out the alias, I didn't need 'em... The prob was I was looking at the wrong field... for some reason I was *sure* termdescription would contain direction, but no... tariffdescription is the one... (probably, it works for now, but I've got to dig through some other code to make sure that that is indeed the field I want).

So the actual statement should be something like:

<code>
SELECT DISTINCT ct.RecordStatus, ct.TableId, ct.ContractNum,
ct.CurrentAmendmentNum, ct.Description, ct.DateEffective,
ct.DateExpiration, ct.DateFiling, ct.FMCFilingNum FROM Contract ct
LEFT JOIN ContractLines cl ON ct.TableId = cl.ContractTableId WHERE
ct.CarrierId = 50 AND ct.RecordStatus <> 'H' and ((cl.TermNum = 1 AND
cl.SubTermNum = 0 AND cl.TariffDescription LIKE '%Eastbound%') OR (cl.TermNum = 1 AND cl.SubTermNum = 1 AND cl.TariffDescription LIKE '%
Eastbound%')) Order By ct.RecordStatus DESC, ct.ContractNum DESC</code>
 
Status
Not open for further replies.