Select a record with highest amount by joining two tables

Posted by user2516394 on Stack Overflow See other posts from Stack Overflow or by user2516394
Published on 2013-06-29T09:30:13Z Indexed on 2013/06/29 10:21 UTC
Read the original article Hit count: 131

Filed under:
|
|

I've 2 tables Sales & Purchase, Sales table with fields SaleId, Rate, Quantity, Date, CompanyId, UserID. Purchase table with fields PurchaseId, Rate, Quantity, Date, CompanyId, UserID.

I want to select a record from either table that have highest Rate*Quantity.

SELECT SalesId Or PurchaseId FROM Sales,Purchase 
where Sales.UserId=Purchase.UserId and Sales.CompanyId=Purchase.CompanyId 
AND Sales.Date=Current date 
AND Purchase.Date=Current date AND Sales.UserId=1 
AND Purchase.UserId=1 AND Sales.CompanyId=1 AND Purchase.ComoanyId=1

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server