怎么样合并这两个SQL,在线等。。。
XinJW 2014-05-06 10:13:59
SELECT a.*,b.*
FROM Process a left join custom b on a.CustomNO=b.CustomNO
WHERE (a.Process_OrderDate IS NOT NULL) AND (CONVERT(Varchar(100),
a.Process_OrderDate, 23) = '2014-05-05') OR
(a.Process_Date IS NOT NULL) AND (CONVERT(Varchar(100), a.Process_Date, 23)
= '2014-05-05')
SELECT nCustom.ID, nCustom.CustomNO, nCustom.M_Name, nCustom.F_Name,
nCustom.SeriesType, nCustom.M_Mobile, nCustom.F_Mobile, nCustom.M_Address,
nCustom.F_Address, nCustom.Series, nCustom.Receivable, nCustom.CardNO,
nCustom.InReturnMoney, nCustom.Sales, nCustom.Remark, nCustom.Prink,
nCustom.Photo, nCustom.PhotoPlace, nCustom.ProcessState,
nCustom.PhotoOrderDate, nCustom.PhotoDate,
nCustom.Receivable + ISNULL(nBuy.Price, 0) - ISNULL(nMoney.Money, 0)
AS n_Arrearage, nCustom.Receivable + ISNULL(nBuy.Price, 0) AS n_TotalSpending,
nCustom.Receivable AS n_Receivable, ISNULL(nBuy.Price, 0) AS n_SecondSpending,
nBuy.Price AS n_SecondSpending, nMoney.Money AS n_TotalPaying
FROM (SELECT ID, CustomNO, SUM(Receivable) 'Receivable', M_Name, F_Name,
SeriesType, M_Mobile, F_Mobile, M_Address, F_Address, Series, CardNO,
InReturnMoney, Sales, Remark, Prink, Photo, PhotoOrderDate, PhotoDate,
PhotoPlace, ProcessState
FROM [Custom]
WHERE Custom.PhotoOrderDate IS NOT NULL AND CONVERT(Varchar(100),
Custom.PhotoOrderDate, 23) = '2014-05-05' OR
Custom.PhotoDate IS NOT NULL AND CONVERT(Varchar(100),
Custom.PhotoDate, 23) = '2014-05-05'
GROUP BY ID, CustomNO, M_Name, F_name, SeriesType, M_Mobile, F_Mobile,
M_Address, F_Address, Series, CardNO, InReturnMoney, Sales, Remark,
Prink, Photo, PhotoOrderDate, PhotoDate, PhotoPlace, ProcessState)
nCustom LEFT OUTER JOIN
(SELECT CustomSN, SUM(Price) 'Price'
FROM [CustomDetails]
WHERE CustomDetails.Class = 'Retail'
GROUP BY CustomSN) nBuy ON
nCustom.CustomNO = nBuy.CustomSN LEFT OUTER JOIN
(SELECT CustomNO, SUM(Money) 'Money'
FROM [Money]
GROUP BY CustomNO) nMoney ON
nCustom.CustomNO = nMoney.CustomNO
WHERE (nCustom.ID > 0)
ORDER BY nCustom.ProcessState DESC, nCustom.PhotoDate DESC,
nCustom.PhotoOrderDate DESC, nCustom.ID DESC