Jump to content

How To Join Columns From Two Sqls Having Differnt Scenarios


s4surbhi2218

Recommended Posts

Hi,

i have two different scenarios for both of them i have framed different sqls now i want to show the columns from both of them together as :

if first sql outputs col A,col B, col C

and second sql outputs col D, col E

in the final output i want to show col A, col B,col C, col D, col E

(i do not need union here) following are the sqls

sql 1

SELECT COD.OrderID,COD.ItemCode AS Item,COD.MaxValue AS Line ,COD.CreatedOn AS DATE,COD.Quantity,

IF(COD.Price IS NULL OR COD.Price = '','0.00',COD.Price) AS Total,

SC.SourceCode AS SourceCode,

RR.Description AS CancellationReason

,CONCAT(CBI.FirstName,' ',CBI.LastName) AS CustomerName

FROM tblCancelOrderDetails COD LEFT JOIN tblOrderMaster OM ON COD.`OrderID` = OM.`OrderID`

LEFT JOIN tblSourceCode SC ON OM.`SourceCode` = SC.`SourceCodeID`

LEFT JOIN tblReturnReason RR ON COD.Reason = RR.`ReturnCode`

LEFT JOIN tblCustomerBasicInfo CBI ON OM.CustomerID = CBI.CustomerID ;

sql 2

SELECT COUNT(DISTINCT(OrderID)) AS NumberofAutodeliveryOrders ,CustomerID ,RecurrenceMasterID FROM tblOrderDetail

WHERE RecurrenceMasterID != '' AND RecurrenceMasterID IS NOT NULL AND RecurrenceMasterID != '-1'

GROUP BY CustomerID;

 

any suggestions would be helpful,thanks

Link to comment
Share on other sites

i tried this

SELECT COD.OrderID,COD.ItemCode AS Item,COD.MaxValue AS Line ,COD.CreatedOn AS DATE,COD.Quantity,

IF(COD.Price IS NULL OR COD.Price = '','0.00',COD.Price) AS Total,

SC.SourceCode AS SourceCode,

RR.Description AS CancellationReason

,CONCAT(CBI.FirstName,' ',CBI.LastName) AS CustomerName,

COUNT(DISTINCT(OrderID)) AS NumberofAutodeliveryOrders,RecurrenceMasterID

FROM tblCancelOrderDetails COD LEFT JOIN tblOrderMaster OM ON COD.`OrderID` = OM.`OrderID`

LEFT JOIN tblSourceCode SC ON OM.`SourceCode` = SC.`SourceCodeID`

LEFT JOIN tblReturnReason RR ON COD.Reason = RR.`ReturnCode`

LEFT JOIN tblCustomerBasicInfo CBI ON OM.CustomerID = CBI.CustomerID

 

INNER JOIN (SELECT COUNT(DISTINCT(OrderID)) AS NumberofAutodeliveryOrders ,CustomerID AS cust ,RecurrenceMasterID FROM tblOrderDetail

WHERE RecurrenceMasterID != '' AND RecurrenceMasterID IS NOT NULL AND RecurrenceMasterID != '-1'

GROUP BY CustomerID) AS OD ON OM.`CompanyID` = OD.CustomerID ;

 

but getting the following error

Error Code: 1052

Column 'OrderID' in field list is ambiguous

 

and i also want this orderid .

Link to comment
Share on other sites

that's probably comming from here :

COUNT(DISTINCT(OrderID)) AS NumberofAutodeliveryOrders,RecurrenceMasterID
FROM tblCancelOrderDetails COD LEFT JOIN tblOrderMaster OM ON COD.`OrderID` = OM.`OrderID`

 

You are joining two tables that both have the OrderID and your not making it clear which one you are using.

 

Also, please please please use code tags!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.