简体   繁体   中英

Combining these two SQL Queries

i'm having troubling combining these two SQL queries. I have this original query which selects data from two tables, the related columns being Tasks.CustomerID and Customers.CustomerID. It's basically Tasks.* + Customers.Name

ALTER PROCEDURE [dbo].[SP_SelectTasksandName]
AS
SELECT TASKS.CustomerID, TASKS.DateCreation, TASKS.DateFinish, TASKS.Description, TASKS.Fees, TASKS.Hours, TASKS.InvoiceNum, TASKS.PaymentMethod,
        TASKS.Status, TASKS.TaskID, CUSTOMERS.Name
FROM TASKS, CUSTOMERS
WHERE (TASKS.CustomerID LIKE CUSTOMERS.CustomerID)

This works, but I've now been asked to limit this query to a specified amount of rows. I found this code which worked well for my other, simpler queries, but I can't seem to use it correctly with this query. This Limit code is below. (ie all rows > @low && rows <= @high)

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY CustomerID) as row FROM CUSTOMERS
    ) a WHERE ((a.row > @low and a.row <= @high) 

This is the closest i've gotten, but i'm stuck here.

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY TASKS.CustomerID) as row FROM 
        (SELECT TASKS.CustomerID, TASKS.DateCreation, TASKS.DateFinish, TASKS.Description, TASKS.Fees, TASKS.Hours, TASKS.InvoiceNum, TASKS.PaymentMethod,
                TASKS.Status, TASKS.TaskID, CUSTOMERS.Name
        FROM TASKS, CUSTOMERS
        WHERE (TASKS.CustomerID LIKE CUSTOMERS.CustomerID))

) a WHERE (a.row > @low and a.row <= @high)

With an error "expecting as id or quoted id" on the first bracket on the last line.

Sorry for the noob question, appreciate any help you guys can give.

Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.

The query you are looking for can be written as:

SELECT tc.*
FROM (SELECT t.CustomerID, t.DateCreation, t.DateFinish, t.Description, 
             t.Fees, t.Hours, t.InvoiceNum, t.PaymentMethod,
             t.Status, t.TaskID, c.Name,
             ROW_NUMBER() OVER (ORDER BY t.CustomerID) as seqnum
        FROM TASKS t JOIN
             CUSTOMERS c
             ON t.CustomerID = c.CustomerID
     ) tc
WHERE tc.seqnum > @low ANDtc.seqnum <= @high

I would write the query as follows:

;With CTE
As
(
    SELECT
        TASKS.CustomerID, 
        TASKS.DateCreation, 
        TASKS.DateFinish, 
        TASKS.Description, 
        TASKS.Fees, 
        TASKS.Hours, 
        TASKS.InvoiceNum, 
        TASKS.PaymentMethod,
        TASKS.Status, 
        TASKS.TaskID, 
        CUSTOMERS.Name, 
        Row_Number() over (Partition By Tasks.CustomerID Order by Tasks.CustomerID) as RN
    FROM TASKS
        Inner Join CUSTOMERS
            on (TASKS.CustomerID = CUSTOMERS.CustomerID)
    )
select *   -- just being lazy here - in production code you ALWAYS use an explicit field list
from CTE
Where CTE.RN > @low and CTE.RN <= @high  -- or whatever logic you need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM