我创建了一个看起来像这样且工作的SQL UNION字符串({dData}
和类似的占位符由VB.Net代码填充):
SELECT LEFT({dData}.PLATE,7) AS Plates,
{dData}.SIGHTING_DT AS LPRReadDate,
{dLov}.LOCATION AS LPRLocation
FROM {dData}
INNER JOIN {dLov} ON ({dData}.ROAD_NAME = {dLov}.ROAD_NAME) AND ({dData}.READER = {dLov}.READER)
WHERE ({dData}.SIGHTING_DT >= @sdate AND {dData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({dData}.PLATE,'')) = @slen1 OR LEN(COALESCE({dData}.PLATE,'')) = @slen2)
AND {dData}.PLATE NOT LIKE @rx1
AND {dData}.PLATE = @pf
UNION
SELECT LEFT({vData}.PLATE,7) AS Plates,
{vData}.SIGHTING_DT AS LPRReadDate,
{vLov}.LOCATION AS LPRLocation
FROM {vData}
INNER JOIN {vLov} ON {vData}.CAMERA = {vLov}.CAMERA
WHERE ({vData}.SIGHTING_DT >= @sdate AND {vData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({vData}.PLATE,'')) = @slen1 OR LEN(COALESCE({vData}.PLATE,'')) = @slen2)
AND {vData}.PLATE NOT LIKE @rx1
AND {vData}.PLATE = @pf
ORDER BY LPRReadDate
问题是我需要按降序排列LPRRead日期获得前1名和订购第二个Select语句。我试着这样做。
SELECT LEFT({dData}.PLATE,7) AS Plates,
{dData}.SIGHTING_DT AS LPRReadDate,
{dLov}.LOCATION AS LPRLocation
FROM {dData}
INNER JOIN {dLov} ON ({dData}.ROAD_NAME = {dLov}.ROAD_NAME) AND ({dData}.READER = {dLov}.READER)
WHERE ({dData}.SIGHTING_DT >= @sdate AND {dData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({dData}.PLATE,'')) = @slen1 OR LEN(COALESCE({dData}.PLATE,'')) = @slen2)
AND {dData}.PLATE NOT LIKE @rx1
AND {dData}.PLATE = @pf
UNION
(SELECT TOP 1 LEFT({vData}.PLATE,7) AS Plates,
{vData}.SIGHTING_DT AS LPRReadDate,
{vLov}.LOCATION AS LPRLocation
FROM {vData}
INNER JOIN {vLov} ON {vData}.CAMERA = {vLov}.CAMERA
WHERE ({vData}.SIGHTING_DT >= @sdate AND {vData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({vData}.PLATE,'')) = @slen1 OR LEN(COALESCE({vData}.PLATE,'')) = @slen2)
AND {vData}.PLATE NOT LIKE @rx1
AND {vData}.PLATE = @pf
ORDER BY {vLov}.CREATED_DT DESC)
ORDER BY LPRReadDate
但是,我收到了这个错误:
&#39; ORDER&#39;附近的语法错误。
我确信我需要做一些子查询。我试图通过在线查看示例来解决这个问题,但我无法弄明白。
更新 @UnhandledExcepSean我尝试调整你所说的代码。经过几次语法错误后,我得到了下面的代码。但每当我尝试运行它时,它只会超时服务器。你能否告诉我它看起来是否正确或是否有任何可能需要做出的改变......
SELECT
Plates, LPRReadDate, LPRLocation
FROM(
SELECT TOP 1
LEFT({vData}.PLATE,7) AS Plates, {vData}.SIGHTING_DT AS LPRReadDate, {vLov}.LOCATION AS LPRLocation
FROM {vData}
INNER JOIN {vLov} ON {vData}.CAMERA = {vLov}.CAMERA
WHERE ({vData}.SIGHTING_DT >= @sdate AND {vData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({vData}.PLATE,'')) = @slen1 OR LEN(COALESCE({vData}.PLATE,'')) = @slen2)
AND {vData}.PLATE NOT LIKE @rx1
AND {vData}.PLATE = @pf
ORDER BY {vLov}.CREATED_DT DESC
) test
UNION
SELECT LEFT({EOCdata}.PLATE,7) AS Plates,
{EOCdata}.SIGHTING_DT AS LPRReadDate,
{EOClov}.LOCATION AS LPRLocation
FROM {EOCdata}
INNER JOIN {EOClov} ON ({EOCdata}.CAMERA = {EOClov}.CAMERA) AND ({EOCdata}.READER = {EOClov}.READER)
WHERE ({EOCdata}.SIGHTING_DT >= @sdate AND {EOCdata}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({EOCdata}.PLATE,'')) = @slen1 OR LEN(COALESCE({EOCdata}.PLATE,'')) = @slen2)
AND {EOCdata}.PLATE NOT LIKE @rx1
AND {EOCdata}.PLATE = @pf
ORDER BY LPRReadDate
答案 0 :(得分:0)
你可以解决这个问题:
DECLARE @t TABLE ( Field1 INT,Field2 INT)
INSERT INTO @t (Field1,Field2) SELECT 1,1
INSERT INTO @t (Field1,Field2) SELECT 2,2
INSERT INTO @t (Field1,Field2) SELECT 3,3
SELECT
Field1,Field2
FROM(
SELECT TOP 1
Field1,Field2
FROM @t
ORDER BY Field1 ASC
) test
UNION
SELECT
Field1,Field2
FROM @t
ORDER BY Field2 DESC
答案 1 :(得分:0)
感谢大家的帮助。我终于能够弄清楚如何做到这一点。如果有人发现它有用,我会在下面发布我想出的内容。 derivedTable部分是我试图找出的部分。
WITH derivedTable
AS
(
SELECT {vLov}.LPR_VIGILANT_LOV_ID,
{vLov}.CAMERA,
{vLov}.LOCATION,
{vLov}.CREATED_DT,
ROW_NUMBER() OVER (Partition By {vLov}.CAMERA
ORDER BY {vLov}.CREATED_DT DESC) rn
FROM {vLov}
)
SELECT LEFT({vData}.PLATE,7) AS Plates,
{vData}.SIGHTING_DT AS LPRReadDate,
derivedTable.LOCATION
FROM {vData}
INNER JOIN derivedTable ON {vData}.CAMERA = derivedTable.CAMERA
WHERE ({vData}.SIGHTING_DT >= @sdate AND {vData}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({vData}.PLATE,'')) = @slen1 OR LEN(COALESCE({vData}.PLATE,'')) = @slen2)
AND {vData}.PLATE NOT LIKE @rx1
AND {vData}.LOCATION = @lf
AND derivedTable.rn = 1
UNION
SELECT LEFT({EOCdata}.PLATE,7) AS Plates,
{EOCdata}.SIGHTING_DT AS LPRReadDate,
{EOClov}.LOCATION AS LPRLocation
FROM {EOCdata}
INNER JOIN {EOClov} ON ({EOCdata}.CAMERA = {EOClov}.CAMERA) AND ({EOCdata}.READER = {EOClov}.READER)
WHERE ({EOCdata}.SIGHTING_DT >= @sdate AND {EOCdata}.SIGHTING_DT <= @edate)
AND (LEN(COALESCE({EOCdata}.PLATE,'')) = @slen1 OR LEN(COALESCE({EOCdata}.PLATE,'')) = @slen2)
AND {EOCdata}.PLATE NOT LIKE @rx1
AND {EOCdata}.LOCATION = @lf