all groups > sql server (alternate) > march 2004 >
You're in the

sql server (alternate)

group:

Converting MS Access Queries to MS SQL Server


Converting MS Access Queries to MS SQL Server =-peep2ee-=|
3/21/2004 5:45:13 PM
sql server (alternate):
For any of those know about SQL and MS SQL Server I need your help... below
is some code out of an ASP script I have:

<%
If Request("cmd") = "rec_history" then
strSQL = "SELECT orders.ordID, orders.ordName, payprovider.payProvName,
orders.ordAuthNumber, orders.ordDate, orders.ordEmail, Last(status.Status)
AS Status, Last(orderStatus.statPublic) AS statPublic FROM orderStatus INNER
JOIN ((orders INNER JOIN status ON orders.ordID = status.ordID) INNER JOIN
payprovider ON orders.ordPayProvider = payprovider.payProvID) ON
orderStatus.statID = status.Status GROUP BY orders.ordID, orders.ordName,
payprovider.payProvName, orders.ordAuthNumber, orders.ordDate,
orders.ordEmail HAVING (((orders.ordEmail)='" & Session("Email") & "'))
ORDER BY orders.ordID DESC;"
%>

<%
Else
strSQL = "SELECT orders.ordID, orders.ordName, payprovider.payProvName,
orders.ordAuthNumber, orders.ordDate, orders.ordEmail, Last(status.Status)
AS Status, Last(orderStatus.statPublic) AS statPublic FROM orderStatus INNER
JOIN ((orders INNER JOIN status ON orders.ordID = status.ordID) INNER JOIN
payprovider ON orders.ordPayProvider = payprovider.payProvID) ON
orderStatus.statID = status.Status GROUP BY orders.ordID, orders.ordName,
payprovider.payProvName, orders.ordAuthNumber, orders.ordDate,
orders.ordEmail HAVING ((ordDate>Date()-7) AND ((ordEmail)='" &
Session("Email") & "')) ORDER BY orders.ordID DESC"
%>

Last, Date and other functions dont seem 2 work :-( Any help on this would
be great!

Re: Converting MS Access Queries to MS SQL Server Trevor Best
3/21/2004 6:20:40 PM
[quoted text, click to view]
Date() can be replaced by GetDate().
Last() hmmmm, could try Max() or a subquery, e.g.
(select top 1 status from status order by orderid desc)



--
Re: Converting MS Access Queries to MS SQL Server David Portas
3/21/2004 11:01:11 PM
Looks like the only Access feature you've used that would give you
difficulty in SQL is the LAST() "aggregate" function. There is no direct
equivalent of the Access FIRST and LAST functions in SQL Server. Because a
table in SQL is not logically ordered there is no first or last row - you
have to specify the column/expression that determines which rows you want.

Here's an example taken from the Pubs database giving the first and last
title based on the MIN and MAX values of Title_id.

SELECT
(SELECT title
FROM Titles
WHERE title_id
= (SELECT MIN(title_id)
FROM Titles)) AS First_Title,
(SELECT title
FROM Titles
WHERE title_id
= (SELECT MAX(title_id)
FROM Titles)) AS Last_Title

A common requirement is to extract a single row of a group based on some
criteria. The query below retrieves values from the "Last" (maximum
title_id) row for each Type in the Titles table.

SELECT T1.type, T1.title_id, T1.title, T1.price
FROM titles AS T1
JOIN
(SELECT MAX(title_id) AS title_id
FROM titles
GROUP BY type) AS T2
ON T1.title_id = T2.title_id

--
David Portas
SQL Server MVP
--

AddThis Social Bookmark Button