Good day!I have a running query that displays the summary of sales of a given date. However if salestype is a 'Bad Order' the query should look into supplier table instead of customer table. This is my query so far:ALTER PROCEDURE [dbo].[display_sales_detail_bydate] @fromdate AS Varchar(max), @todate AS Varchar(max)AsBegin SELECT transdate,[Last Name] + ', ' + [First Name] + ' ' + [Middle Name] AS FullName, salesid,salestype,casheirid,terminal,totalsales ,costofsales=(SELECT SUM(sd.supprice * sd.qty) FROM dbo.tbl_pos_sales_detail sd WHERE sd.salesid=sm.salesid AND sd.salestype=sm.salestype) FROM tbl_pos_sales_summary smLEFT JOIN tbl_costumers tcON tc.[Last Name]=(SELECT tc.[Last Name] WHERE tc.[ID No.] = sm.costumerid) WHERE (sm.salestype='cash' or sm.salestype='credit' or sm.salestype='pay' OR sm.salestype='Bad Order')--AND (sm.transdate >= @fromdate AND transdate < DATEADD(dd,1,@todate))AND (CONVERT(Date, sm.transdate)) BETWEEN @fromdate AND @todate ORDER BY sm.transdate,sm.salesid ascEnd
If its bad order, the left join must be replace with this query:INNER JOIN tbl_supplier spON sp.suppliername=(SELECT sp.suppliername WHERE sp.supplierID = sm.costumerid)
This is what i want to incorporate on the query above!Thank you for helping!-------------------------------------------------------------------------------------------------------Learning MS SQL Server 2008