TSQL: how do i show 0.00 totals for this column - sql

I have a Department table with 15 different departments. I want my query to show some totals for a certain project. In this particular project there are 3 departments that are working on it and have totals for various things. My current query does show the totals for those three departments. However, i also want to show all the other departments but just have zeros in the totals columns for those departments. Those departments did not have anything to do with this project but still need to show them with zero totals. Here is my query that results in three rows of data. Disciplines is the table with all my departments. Project_Disciplines are those that are actually working on the project
SELECT
T.NAme
,ISNULL(CAST(t.EngHours AS DECIMAL(15,0)),0) AS EngHours
,ISNULL(SUM(t.EngBlendedRate),0) as EngRate
,ISNULL(CAST(t.EngHours * SUM(t.EngBlendedRate) AS DECIMAL(15,0)),0) AS EngDollars
,ISNULL(CAST(t.DesignHours AS DECIMAL(15,0)),0) AS DesignHours
,ISNULL(sum(t.DsgBlendedRate),0) AS DesignRate
,ISNULL(CAST(t.DesignHours * SUM(t.DsgBlendedRate) AS DECIMAL(15,0)),0) AS DesignDollars
,ISNULL(t.EngHours + t.DesignHours,0) AS TotalHours
,ISNULL(CAST(CAST((t.EngHours * SUM(t.EngBlendedRate)) + (t.DesignHours * SUM(t.DsgBlendedRate)) AS DECIMAL (18,2)) / CAST(NULLIF(t.EngHours + t.DesignHours,0) AS DECIMAL(18,2)) AS DECIMAL (18,2)),0) AS TotalRate
,CAST(ISNULL((t.EngHours * SUM(t.EngBlendedRate)),0) + ISNULL((t.DesignHours * SUM(t.DsgBlendedRate)),0) AS DECIMAL(15,0)) AS TotalDollars
,ISNULL(MAX(case when OA.Code = 'Travel' then OA.Cost end),0) Travel
,ISNULL(MAX(case when OA.Code = 'Equipment' then OA.Cost end),0) Equipment
,ISNULL(MAX(case when OA.Code = 'Subcontract' then OA.Cost end),0) Subcontract
,ISNULL(MAX(case when OA.Code = 'Other' then OA.Cost end),0) Other
,ISNULL(MAX(case when OA.Code = 'Markups' then OA.Cost end),0) Markups
,CAST(ISNULL((t.EngHours * SUM(t.EngBlendedRate)),0) + ISNULL((t.DesignHours * SUM(t.DsgBlendedRate)),0) AS DECIMAL(15,0))
+ ISNULL(MAX(case when OA.Code = 'Travel' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Equipment' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Subcontract' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Other' then OA.Cost end),0)
+ ISNULL(MAX(case when OA.Code = 'Markups' then OA.Cost end),0) AS EstimateTotal
FROM
(SELECT c.id, x.Name,
ISNULL(CASE WHEN f.team_type_id = 1 OR f.team_type_id = 3
THEN
CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2))
END,0) AS EngBlendedRate ,
ISNULL(CASE WHEN f.team_type_id = 2 OR f.team_type_id = 4
THEN
ISNULL(CAST(f.POH * (d.HourlyRate * (1-(r.Discount/100))/100) AS DECIMAL(8,2)),0) END,0) AS DsgBlendedRate
,ISNULL(CAST(SUM((b.qty * b.unit_rate)* b.Eng_RPQ /100) AS DECIMAL(8,1)),0) AS [EngHours]
,ISNULL(CAST(SUM((b.qty * b.unit_rate)* b.Design_RPQ /100) AS DECIMAL(8,1)),0) AS [DesignHours]
FROM Project p
INNER JOIN Project_Disciplines pd on p.id = pd.project_id
INNER JOIN Discipline x on x.id = pd.Disc_id
INNER JOIN Activity c on c.discipline_id = x.id
INNER JOIN Activity_Details b ON b.activity_id = c.id
INNER JOIN Team f on f.activity_id = c.id
INNER JOIN SOF_Details d on d.id = f.sof_detail_id
INNER JOIN Rate r on r.projectid = p.id
WHERE p.id = 19
GROUP BY c.id, x.Name,
f.team_type_id
, f.POH,d.HourlyRate,r.Discount
) AS T
LEFT OUTER JOIN (
SELECT a.activity_id, c.Code
,CAST(Sum(a.Rate * a.Qty)AS DECIMAL(18,2)) AS COST
FROM Other_Activity a
INNER JOIN Activity b on a.activity_id = b.id
INNER JOIN EXPENSE_Codes c on a.expense_code_id = c.id
WHERE b.project_id = 19
GROUP BY a.activity_id, c.Code) AS OA ON OA.activity_id = T.id
GROUP BY T.Name, t.EngHours, t.DesignHours
Results
NAme EngHours EngRate EngDollars DesignHours DesignRate DesignDollars TotalHours TotalRate TotalDollars Travel Equipment Subcontract Other Markups EstimateTotal
Architechtural 81 110.73 8947 237 78.85 18711 318.1 86.95 27658 0.00 0.00 0.00 0.00 0.00 27658.00
Concrete 3014 423.72 1277092 2136 357.96 764603 5150.0 396.45 2041695 60.00 45.00 0.00 325.00 0.00 2042125.00
Structural 903 104.98 94744 1351 0.00 0 2253.0 42.05 94744 0.00 0.00 0.00 0.00 0.00 94744.00
I would like to show the other departments also with zeros
NAme EngHours EngRate EngDollars DesignHours DesignRate DesignDollars TotalHours TotalRate TotalDollars Travel Equipment Subcontract Other Markups EstimateTotal
Architechtural 81 110.73 8947 237 78.85 18711 318.1 86.95 27658 0 0 0 0 0 27658
Concrete 3014 423.72 1277092 2136 357.96 764603 5150 396.45 2041695 60 45 0 325 0 2042125
Structural 903 104.98 94744 1351 0 0 2253 42.05 94744 0 0 0 0 0 94744
Mechanical 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Piping 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Process 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Civil 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Electrical 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Project Admin 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
C&A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Estimating 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Procurement 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

I assume the Activity and Activity_Details tables do not have any data for those departments. You should make them and any other table without data for all departments be connected as left joins.
You can run the subquery labeled T on a standalone basis and make sure that all your departments are on the list. Without knowing the table structure, you might have to reorder the tables in the subquery T to make "Discipline" come first, then left join everything else that is not guaranteed to have info on all departments

Related

SQL Select with joins and calculate percentage

I have three tables and I'm trying to make a select statement to give me a result like the one below
Teams:
ID Name
1 A
2 B
3 C
Players:
ID Name TeamID
1 P1 1
2 P2 1
3 P3 2
Goals: (goaltype: H for home, A for away, T for training)
ID PID goaltype
1 1 A
2 1 A
3 1 H
4 2 A
5 2 H
6 3 A
7 3 T
Result will be Like:
Team totalGoals home away trainig percentage[(home/total)*100]
A 5 2 3 0 40%
B 2 0 1 1 0
C 0 0 0 0 0
This is my current query:
select t.name,
count(g.id) as totalGoals,
sum(case when g.GTYPE = 'H' then 1 else 0 end) as home,
sum(case when g.GTYPE = 'A' then 1 else 0 end) as away,
sum(case when g.GTYPE = 'T' then 1 else 0 end) as training,
--(home/totalGoals) as percentage
from teams t
left join players p on p.TeamID = t.id
left join goals g on g.pid = p.id
group by t.name
You can use conditional aggregation to get the results you want:
SELECT t.Name AS Team,
COUNT(g.goaltype) AS totalGoals,
SUM(CASE WHEN g.goaltype = 'H' THEN 1 ELSE 0 END) AS home,
SUM(CASE WHEN g.goaltype = 'A' THEN 1 ELSE 0 END) AS away,
SUM(CASE WHEN g.goaltype = 'T' THEN 1 ELSE 0 END) AS training,
CASE WHEN COUNT(g.goaltype) = 0 THEN 0
ELSE 100.0 * SUM(CASE WHEN g.goaltype = 'H' THEN 1 ELSE 0 END) /
COUNT(g.goaltype)
END AS percentage
FROM Teams t
LEFT JOIN Players p ON p.TeamID = t.ID
LEFT JOIN Goals g ON g.PID = p.ID
GROUP BY t.Name
ORDER BY t.Name
Output:
team totalgoals home away training percentage
A 5 2 3 0 40
B 2 0 1 1 0
C 0 0 0 0 0
Demo on SQLFiddle

Solution to insert sub-query with additional group by

I'm trying to merge two working SQL query's in Oracle SQL Developer but can't seem to get the sub's Group By's to play nicely. I want/expect to see separate totals for each row but I'm getting an overall total for all rows.
I tried adding the second query as sub-query.
Query 1:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5"
FROM TABLE.req
GROUP BY SOURCE
ORDER BY SOURCE;
Query 2 to be added to the above:
SELECT SOURCE, COUNT(REQ.SOURCE) AS "Done in 7 Days"
FROM TABLE.req REQ
JOIN TABLE.audit AUD ON REQ.ROW_ID = AUD.RECORD_ID
WHERE (AUD.LAST_UPD - REQ.CREATED) <= 7
AND REQ.STATUS = 'Complete'
GROUP BY SOURCE;
Tried Sub-Query:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5"
(SELECT SOURCE, COUNT(REQ.SOURCE)
FROM TABLE.req REQ
JOIN TABLE.audit AUD ON REQ.ROW_ID = AUD.RECORD_ID
WHERE (AUD.LAST_UPD - REQ.CREATED) <= 7
AND REQ.STATUS = 'Complete'
GROUP BY SOURCE) AS "Done in 7"
FROM TABLE.req
GROUP BY SOURCE
ORDER BY SOURCE;
Query 1 returns:
A 0 0 0 0 0
B 0 0 3026 26 2461
C 0 0 0 0 0
D 3 39 2 1 19
E 0 0 61156 0 79430
Query 2 returns:
A 2906
B 10
C 28
D 7
E 0
ACTUAL:
Sub-Query returns the additional Column BUT it's being totaled
A 0 0 0 0 0 2951
B 0 0 3026 26 2461 2951
C 0 0 0 0 0 2951
D 3 39 2 1 19 2951
E 0 0 61156 0 79430 2951
EXPECTED:
Sub-Query returns the additional Column BUT it's being totaled
A 0 0 0 0 0 2906
B 0 0 3026 26 2461 10
C 0 0 0 0 0 28
D 3 39 2 1 19 7
E 0 0 61156 0 79430 0
You seem to want a correlated subquery:
SELECT SOURCE,
sum(case when status = 'C1' then 1 else 0 end) as "C1",
sum(case when status = 'C2' then 1 else 0 end) as "C2",
sum(case when status = 'C3' then 1 else 0 end) as "C3",
sum(case when status = 'C4' then 1 else 0 end) as "C4",
sum(case when status = 'C5' then 1 else 0 end) as "C5",
(SELECT COUNT(*)
FROM TABLE.req REQ r2 JOIN
TABLE.audit a
ON r2.ROW_ID = a.RECORD_ID
WHERE r2.SOURCE = r.SOURCE AND
(a.LAST_UPD - r2.CREATED) <= 7 AND
r2.STATUS = 'Complete'
)
FROM TABLE.req r
GROUP BY SOURCE
ORDER BY SOURCE;

How to nest a join into a complicated Select sum(case, group by statement

I am trying to generate a report, and so far have one completed that gives me how many orders, for each day, are in status 1-9.
TableA structure looks like this:
Sales Order | Order Status | Order Date
123456789 | 1 | 2017-02-22 00:00:00.000
123456790 | 0 | 2017-02-21 00:00:00.000
TableB structure looks like this:
Sales Order | Price
123456789 | 123.00
123456789 | 42.00
123456790 | 56.00
123456790 | 28.00
This code:
SELECT
MAX(year([OrderDate])) as Yr,
MAX(MONTH([OrderDate])) as M,
Day([OrderDate]) as Day,
sum(case when [OrderStatus]='0' THEN 1 ELSE 0 END) AS 'STATUS"0"',
sum(case when [OrderStatus]='1' THEN 1 ELSE 0 END) AS 'STATUS"1"',
sum(case when [OrderStatus]='2' THEN 1 ELSE 0 END) AS 'STATUS"2"',
sum(case when [OrderStatus]='4' THEN 1 ELSE 0 END) AS 'STATUS"4"',
sum(case when [OrderStatus]='8' THEN 1 ELSE 0 END) AS 'STATUS"8"',
sum(case when [OrderStatus]='9' THEN 1 ELSE 0 END) AS 'STATUS"9"',
sum(case when [OrderStatus]='S' THEN 1 ELSE 0 END) AS 'STATUS"S"',
sum(case when [OrderStatus]='*' THEN 1 ELSE 0 END) AS 'STATUS"*"',
sum(case when [OrderStatus]='/' THEN 1 ELSE 0 END) AS 'STATUS"/"'
FROM
SorMaster
WHERE
YEAR([OrderDate]) = YEAR(GETDATE())
GROUP BY
DATENAME(month, DateAdd(month, Month([OrderDate]) - 1, Cast('2008-01-01' AS Datetime))), Day([OrderDate])
ORDER BY
Yr DESC, M DESC, Day DESC
Returns this:
Yr | M | Day | STATUS"0" | STATUS"1" | STATUS"2" | STATUS"4" | STATUS"8" | STATUS"9" | STATUS"S" | STATUS"*" | STATUS"/"
2017 2 22 0 2 0 1 0 0 5 0 0
2017 2 21 0 0 0 7 0 0 0 0 0
This is PERFECT for my first report.
Now, comes the trouble. My Problem is that I need to nest-query Table B, so that instead of returning a count(orders), I need the sum(orders) those totals for each order are in Table B.
Using the above example, the query would need to return something like this:
Yr | M | Day | STATUS"0" | STATUS"1" | STATUS"2" | STATUS"4" | STATUS"8" | STATUS"9" | STATUS"S" | STATUS"*" | STATUS"/"
2017 2 22 0 165 0 0 0 0 0 0 0
2017 2 21 84 0 0 0 0 0 0 0 0
Any pointers?
Just join to TableB:
SELECT MAX(year([t1.OrderDate])) AS Yr,
MAX(MONTH([t2.OrderDate])) AS M,
DAY([t1.OrderDate]) AS Day,
SUM(CASE WHEN [OrderStatus] = '0' THEN t2.Price ELSE 0 END) AS 'STATUS"0"',
SUM(CASE WHEN [OrderStatus] = '1' THEN t2.Price ELSE 0 END) AS 'STATUS"1"',
SUM(CASE WHEN [OrderStatus] = '2' THEN t2.Price ELSE 0 END) AS 'STATUS"2"',
SUM(CASE WHEN [OrderStatus] = '4' THEN t2.Price ELSE 0 END) AS 'STATUS"4"',
SUM(CASE WHEN [OrderStatus] = '8' THEN t2.Price ELSE 0 END) AS 'STATUS"8"',
SUM(CASE WHEN [OrderStatus] = '9' THEN t2.Price ELSE 0 END) AS 'STATUS"9"',
SUM(CASE WHEN [OrderStatus] = 'S' THEN t2.Price ELSE 0 END) AS 'STATUS"S"',
SUM(CASE WHEN [OrderStatus] = '*' THEN t2.Price ELSE 0 END) AS 'STATUS"*"',
SUM(CASE WHEN [OrderStatus] = '/' THEN t2.Price ELSE 0 END) AS 'STATUS"/"'
FROM SorMaster t1
LEFT JOIN TableB t2
ON t1.[Sales Order] = t2.[Sales Order]
WHERE YEAR([OrderDate]) = YEAR(GETDATE())
GROUP BY DATENAME(month,DateAdd(month,Month([OrderDate])-1,Cast('2008-01-01' AS Datetime))),
DAY([OrderDate])
ORDER BY Yr DESC, M DEACLLSC, Day DESC
That's not too difficult, just a matter of LEFT JOINing in table B and then summing the prices in that. There's a couple of small tricks here. You want to LEFT JOIN to ensure that rows in table A always show up, even if there are no corresponding rows in table B. Secondly, in your SUM() statement, you'll need to add a COALESCE(...,0.00) to ensure you're summing decimals and no NULL values creep in from the LEFT JOIN. Oddly in databases, NULL + {anything} = NULL.
For the below query, you'll need to change the name of TableB to whatever the table name is, and the JOIN predicate will need to have the column names named accurately, and delimited correctly if they contain spaces. For example, in MSSQL the delimiters are [ and ], e.g. MyTable.[My Column With Spaces]
SELECT
MAX(YEAR([OrderDate])) as Yr,
MAX(MONTH([OrderDate])) as M,
DAY([OrderDate]) as Day,
sum(case when [OrderStatus]='0' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"0"',
sum(case when [OrderStatus]='1' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"1"',
sum(case when [OrderStatus]='2' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"2"',
sum(case when [OrderStatus]='4' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"4"',
sum(case when [OrderStatus]='8' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"8"',
sum(case when [OrderStatus]='9' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"9"',
sum(case when [OrderStatus]='S' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"S"',
sum(case when [OrderStatus]='*' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"*"',
sum(case when [OrderStatus]='/' THEN COALESCE(TableB.Price, 0.00) ELSE 0.00 END) AS 'STATUS"/"'
FROM SorMaster
LEFT OUTER JOIN TableB
ON TableB.SalesOrder = SorMaster.SalesOrder
WHERE YEAR([OrderDate]) = YEAR(GETDATE())
GROUP BY
DATENAME(month,DateAdd(month,Month([OrderDate])-1,Cast('2008-01-01' AS Datetime))),
DAY([OrderDate])
ORDER BY
Yr DESC,
M DESC,
Day DESC
By (left) joining tableB and replacing your 'count' ones by tableB.price you should get the sum of all positions of orders with the according status.

Fetch the total from the result of a query

The query i am using is
select convert(varchar(10), sales.saledate, 103) [SaleDate], SUM(sales.Domestic) [Domestic], SUM(sales.Export) [Export], SUM(sales.Import) [Import],
SUM(sales.Value) [Value], Sum(sales.Cancelled) [Cancelled], sum(sales.cancelledValue) [CancelledValue], SUM(sales.totalValue) [TotalValue]
from
(
select max(j.SaleDate) SaleDate,
case when max(oc.Code) = 'AU' and max(dc.Code) = 'AU' then 1 else 0 end [Domestic],
case when max(oc.Code) = 'AU' and max(dc.Code) <> 'AU' then 1 else 0 end [Export],
case when max(oc.Code) <> 'AU' and max(dc.Code) = 'AU' then 1 else 0 end [Import],
1 [Total],
max(ic.Total) [Value],
case when max(c.CancelDate) is not null then 1 else 0 end [Cancelled],
case when max(c.CancelDate) is not null then max(ic.Total) else 0 end [CancelledValue],
case when max(c.CancelDate) is null then max(ic.Total) else 0 end [TotalValue]
from invoices i
left join Jobs j on i.JobKey = j.JobKey
inner join tasks t on j.jobkey = t.jobkey
inner join Consignments c on t.TaskKey = c.consignmentkey
inner join places op on c.originplacekey = op.placekey
inner join places dp on c.destinationplacekey = dp.placekey
inner join places oC on dbo.ParentPlaceKey(c.originPlaceKey) = oc.placekey
inner join places dC on dbo.ParentPlaceKey(c.destinationplacekey) = dc.placekey
left join (select consignmentKey, sum(Value) [Value] from ConsignmentItems ci group by consignmentkey ) ci on ci.ConsignmentKey = c.ConsignmentKey
left join (select invoicekey, sum(case when ci.ChargeItemKey = 'FRT_SLL' then oc.Value else 0 end) [Freight],
sum(case when ci.ChargeItemKey = 'WTY_SLL' then oc.Value else 0 end) [Warranty],
sum(case when ci.ChargeType = 4 then oc.Value else 0 end) [Total] from InvoiceCharges ic
left join OptionCharges oc on ic.OptionChargeKey = oc.OptionChargeKey
left join ChargeItems ci on oc.ChargeItemKey = ci.ChargeItemKey
group by invoicekey
) ic on ic.InvoiceKey = i.InvoiceKey
where
j.SaleDate >= '01-Apr-2013' and j.SaleDate <= '10-May-2013'
and
j.operationalstorekey = dbo.StoreCode('AU-WEB')
and j.saledate is not null and SelectedOptionKey is not null
group by j.jobkey
) sales
group by convert(varchar(10), sales.saledate, 103)
order by max(sales.saledate)
The result of a sql query is
SaleDate Domestic Export Import Value Cancelled CancelledValue Totalvalue
11/04/2013 1 0 0 47.200 0 0.0000 47.2000
16/04/2013 6 0 0 249.750 0 0.0000 249.7500
22/04/2013 0 1 0 223.480 0 0.0000 223.4800
23/04/2013 0 3 0 670.440 0 0.0000 670.4400
I want result like (want to add the TOTALS at the end)
SaleDate Domestic Export Import Value Cancelled CancelledValue Totalvalue
11/04/2013 1 0 0 47.200 0 0.0000 47.2000
16/04/2013 6 0 0 249.750 0 0.0000 249.7500
22/04/2013 0 1 0 223.480 0 0.0000 223.4800
23/04/2013 0 3 0 670.440 0 0.0000 670.4400
TOTALS 7 4 0 1190.432 0 0 1190.432
Can anyone please tell me how to achieve this in above query, i am trying with temp tables which i dont want.
Thanks.
You can achieve this by adding an UNION at the end of your result. Something like:
... order by max(sales.saledate)
UNION
(SELECT "TOTALS", SUM(sales.Domestic) AS Domestic, SUM(sales.Export) AS Export,
SUM(sales.Import) AS Import, SUM(sales.Value) [Value], Sum(sales.Cancelled) AS Cancelled, sum(sales.cancelledValue) AS CancelledValue, SUM(sales.totalValue) AS TotalValue
FROM "your_big_query"...
WHERE ...
)
And this time remove the group by convert(varchar(10), sales.saledate, 103)
EDIT
Or you can use the GROUP BY Modifiers try to use this in the GROUP BY statement:
group by convert(varchar(10), sales.saledate, 103) WITH ROLLUP
You can read the official documentation here with some examples too.
Hope this works!!

Not getting data from Second table

''hi all
''query is working but i am not getting data from BD table ( B_Detail )
''What am i doing wrong..??
SELECT CM.CM_Date AS Cdate, CM.C_MemoNo AS CmNo,0 as BookDate, 0 as LR_No,
CM.CM_Total as CMAmt, 0 as Amt_Rcvd, 0 as ChqDDNo, 0 as ChqDDdate, 0 as Acc_Typ,
0 as RcptNo,0 as MemoNo, 0 as M_Date, CM.CNee as conName
from (CMemo as CM
INNER JOIN ClientLedger ON (ClientLedger.CName = CM.CNee))
UNION ALL
Select 0 as Rcpt_Date, 0 as CmNo, BD.Bdate as BookDate, BD.BNo as LR_No,
0 as CM_Total, 0 as Amt_Rcvd, 0 as ChqDDNo, 0 as ChqDDdate, 0 as Acc_Typ,
0 as RcptNo,BD.CST as MNo, BD.GRDate as M_Date, BD.Consignee
from (B_Detail BD
INNER JOIN Receipt_CLNT CM ON (CM.CNee = BD.Consignee))
UNION ALL
SELECT Receipt_CLNT.Rcpt_Date, 0 as CmNo, 0 as BookDate, 0 as LR_No,
0 as CM_Total, Receipt_CLNT.Amt_Rcvd as RcptAmt,Receipt_CLNT.ChqDDNo as RefNo,
Receipt_CLNT.ChqDDdate as RefDate, Receipt_CLNT.Amt_Mode as Mode,
Receipt_CLNT.RcptNo as RcptNo, 0 as MemoNo, 0 as MDate, Receipt_CLNT.G_Name
from Receipt_CLNT
ORDER BY Cdate;
If you simplify the question to just the problem part does it still not return data?
Select 0 as Rcpt_Date, 0 as CmNo, BD.Bdate as BookDate, BD.BNo as LR_No,
0 as CM_Total, 0 as Amt_Rcvd, 0 as ChqDDNo, 0 as ChqDDdate, 0 as Acc_Typ,
0 as RcptNo,BD.CST as MNo, BD.GRDate as M_Date, BD.Consignee
from (B_Detail BD
INNER JOIN Receipt_CLNT CM ON (CM.CNee = BD.Consignee))
If so then the join (being an inner join) is not matching any keys.
CM.CNee = BD.Consignee