无法将where子句集成到现有的select语句中以进行报告

时间:2012-10-31 19:12:31

标签: sql tsql

所以,我有两个查询 - 一个目前正在创建一个报告,另一个我编写的查询将更多数据附加到报告中。

我想合并新查询,以便在ASP中调用时,数据显示在同一数据集中。

我在放置新的where子句时遇到了麻烦,因此它只给了我想要的东西。查询可以自行完成,但我似乎无法弄清楚如何将它们组合在一起,而不会给我一些我不想要的额外数据。

以下是原始查询:

SELECT 
transactions.date,
transactions.Pcode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist
WHERE
transactions.Pcode=productlist.Pcode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
ORDER BY
mTo,
transactions.date,
transactions.pCode

我要整合的查询是:

SELECT 
transactions.date,
transactions.pcode,
transactions.L,
transactions.iL,  
transactions.qty,
transactions.mTo,
FROM
transactions, 
productlist,
productarchive,
Shipping
WHERE 
transactions.pcode=productlist.pcode 
AND productlist.client=transactions.client 
AND transactions.mFrom='NotAccepted'
AND ProductArchive.TrID=Transactions.ID
AND ProductArchive.StID = Shipping.ID
AND Shipping.SID LIKE 'BAG%'
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 

最后的查询我想出了那些给我的记录超过我想要的数万条。

SELECT 
transactions.date,
transactions.pCode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist, 
shipping,
ProductArchive
WHERE 
transactions.pCode=productlist.pCode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage' OR transactions.mFrom='NotAccepted') 
AND transactions.client=232 
AND 
( tophysical = 'Place1' OR fromphysical = 'Place1' OR 
    ( ProductArchive.TransID=Transactions.ID
        AND ProductArchive.StID = Shipping.ID
        AND Shipping.SID LIKE 'BAG%') 
)
AND transactions.Date>= '2012-10-01' 
AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
ORDER BY
transactions.mTo,
transactions.date,
transactions.pCode

感谢您的时间,精力和帮助!深深感激:)

2 个答案:

答案 0 :(得分:0)

SELECT 
transactions.date,
transactions.Pcode,
transactions.L,
transactions.iL,  
transactions.q,
transactions.mTo
FROM
transactions, 
productlist
WHERE
transactions.Pcode=productlist.Pcode 
AND productlist.client=transactions.client 
AND (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
AND transactions.Date>= '2012-10-01' 
AND transactions.Date < '2012-11-01' 
ORDER BY
mTo,
transactions.date,
transactions.pCode

union all

SELECT 
transactions.date,
transactions.pcode,
transactions.L,
transactions.iL,  
transactions.qty,
transactions.mTo,
FROM
transactions, 
productlist,
productarchive,
Shipping
WHERE 
transactions.pcode=productlist.pcode 
AND productlist.client=transactions.client 
AND transactions.mFrom='NotAccepted'
AND ProductArchive.TrID=Transactions.ID
AND ProductArchive.StID = Shipping.ID
AND Shipping.SID LIKE 'BAG%'
AND transactions.Date>= '2012-10-01' 
AND transactions.Date < '2012-11-01' 

答案 1 :(得分:0)

根据我的评论,使用UNION ALL可能更容易,我也更新为使用ANSI JOIN语法:

SELECT transactions.date,
    transactions.Pcode,
    transactions.L,
    transactions.iL,  
    transactions.q,
    transactions.mTo,
    'query 1' source
FROM transactions
INNER JOIN productlist
    ON transactions.Pcode=productlist.Pcode 
    AND productlist.client=transactions.client 
WHERE (transactions.mTo='NotAccepted' OR transactions.mTo = 'TheStage') 
    AND transactions.client=232 AND ( tophysical = 'Place1' OR fromphysical = 'Place1' ) 
    AND transactions.Date>= '2012-10-01' 
    AND transactions.Date <= '2012-10-31 11:59:59 PM ' 
UNION ALL
SELECT transactions.date,
    transactions.pcode,
    transactions.L,
    transactions.iL,  
    transactions.qty,
    transactions.mTo,
    'query 2' source
FROM transactions
INNER JOIN productlist
    ON transactions.pcode=productlist.pcode 
    AND productlist.client=transactions.client 
INNER JOIN productarchive
    ON ProductArchive.TrID=Transactions.ID
INNER JOIN Shipping
    ON ProductArchive.StID = Shipping.ID
WHERE transactions.mFrom='NotAccepted'
    AND Shipping.SID LIKE 'BAG%'
    AND transactions.Date>= '2012-10-01' 
    AND transactions.Date <= '2012-10-31 11:59:59 PM '