Merging two tables into one both the table having different where clause - sql

I am having Two Sql Queries as Follows:
1st Query
SELECT TC.TCName,
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount,
(0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName)
FROM [dbo].[TCDetails] )
GROUP BY TCName
order by [TCName] ASC
I am getting a result:
||TCName || BLDOS || CollectedAmount || TargetAmount||
| Aarti | 81234.2 | 1678.76 | 789065 |
| Dev | 181234.2| 678.76 | 2389065 |
2nd query is:
SELECT COUNT(*) as count
FROM [dbo].[TCDetails]
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
AND CallDate is NOT NULL
GROUP BY TCName
AND I am Getting:
||count||
231
123
I want to print it like this
||TCName || BLDOS || CollectedAmount || TargetAmount|| count ||
| Aarti | 81234.2 | 1678.76 | 789065 | 231 |
| Dev | 181234.2| 678.76 | 2389065 | 123 |
I am confused because both the queries are having different wheres clause

Try this
SELECT * FROM
(
SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C INNER JOIN
[dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID INNER JOIN
[dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
GROUP BY TCName order by [TCName] ASC
) S JOIN
(
SELECT TCName,COUNT(*) as count FROM [dbo].[TCDetails]
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
AND CallDate is NOT NULL GROUP BY TCName
) T ON S.TCName=T.TCName;
OP:
+--------------------------------------------------------------+
|TCName | BLDOS | CollectedAmount | TargetAmount| count |
+--------------------------------------------------------------+
| Aarti | 81234.2 | 1678.76 | 789065 | 231 |
| Dev | 181234.2| 678.76 | 2389065 | 123 |
+--------------------------------------------------------------+

In both queries you use TCDetails and group by TCName. You don't have to do this twice only to count non-null call dates. COUNT(CallDate) does that for you. I also removed the IN clause which doesn't add anthing to your query. So the query is simply:
SELECT
TC.TCName,
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount,
0.35 * sum(BS.BLDOS) as TargetAmount,
count(TC.CallDate) as cnt
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
GROUP BY TC.TCName
ORDER BY TC.TCName ASC;

SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount ,
(SELECT COUNT(*) as count FROM [dbo].[TCDetails] TC2
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
AND CallDate is NOT NULL AND TC2.TCName = TC.TCName GROUP BY TCName) AS Count
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
GROUP BY TCName order by [TCName] ASC

Try as:
SELECT TC.TCName,
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount,
(0.35*sum(BS.BLDOS)) as TargetAmount,
Count(CallDate) as count
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName)
FROM [dbo].[TCDetails] )
GROUP BY TCName
order by [TCName] ASC
Check Demo here.

Try below:
SELECT S.*,T.count FROM
(
SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C JOIN
[dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID JOIN
[dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
GROUP BY TCName order by [TCName] ASC
) S join
(
SELECT TCName,COUNT(*) as count FROM [dbo].[TCDetails]
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
AND CallDate is NOT NULL GROUP BY TCName
) T on S.TCName=T.TCName;

Related

How to use PIVOT in SQL for this sample

How do I pivot a results query?
Currently it looks like this
| Date | Count | BankName |
+---------+----------+----------+
| 970401 | 87 | Saderat |
| 970401 | 25 | Melli |
| 970401 | 11 | Sina |
into this
|Date | Saderat | Melli | Sina |
+---------+----------+----------+----------+
|970401 | 87 | 25 | 11 |
I tried the following but it's not working
SELECT
PayDate AS [Date],
COUNT(*) AS [Count], b.BankName
FROM
Payments p
INNER JOIN
dbo.Accounts a ON a.AccountId = p.CashAccountId
INNER JOIN
dbo.Banks b ON b.BankId = a.BankId
WHERE
PayTypeId = 21.101
AND PayDate BETWEEN '970401' AND '970412'
GROUP BY
PayDate, b.BankName
ORDER BY
paydate
or
SELECT
x.PayDate AS 'Date',
b.BankName
FROM
(SELECT
p.PayDate, p.PaymentId, p.CashAccountId
FROM
Payments p
WHERE
PayTypeId = 21.101
AND PayDate BETWEEN '970401' AND '970412') AS x
INNER JOIN
dbo.Accounts a ON a.AccountId = x.CashAccountId
INNER JOIN
dbo.Banks b ON b.BankId = a.BankId
PIVOT
(COUNT(PaymentId) FOR PayDate IN (bankid)) AS Pivotable
You can try following SQL for required results:
SELECT PayDate, Saderat, Melli, Sina
FROM
(SELECT PayDate , COUNT(*) AS [Count] , b.BankName
FROM Payments p INNER JOIN dbo.Accounts a ON a.AccountId = p.CashAccountId
INNER JOIN dbo.Banks b ON b.BankId = a.BankId
WHERE PayTypeId = 21.101 AND PayDate BETWEEN '970401' AND '970412'
GROUP BY PayDate , b.BankName
ORDER BY paydate) AS SourceTable
PIVOT
(
SUM([Count])
FOR BankName IN (Saderat, Melli, Sina)
) AS PivotTable;
You can do aggregation :
SELECT PayDate AS [Date],
SUM(CASE WHEN b.BankName = 'Saderat' THEN 1 ELSE 0 END) AS Saderat,
. . .
FROM Payments p INNER JOIN
dbo.Accounts a
ON a.AccountId = p.CashAccountId INNER JOIN
dbo.Banks b
ON b.BankId = a.BankId
WHERE PayTypeId = 21.101 AND PayDate BETWEEN '970401' AND '970412'
GROUP BY PayDate
ORDER BY paydate;
You can use PIVOT function in SQL Server, try following query:
SELECT date, [Saderat],[Melli],[Sina]
FROM YourTableName
PIVOT( MAX(count)
FOR BankName IN ([Saderat],[Melli],[Sina])) AS p
You could PIVOT
SELECT *
FROM
(
SELECT
p.PayDate AS [Date],
b.BankName
FROM dbo.Payments p
JOIN dbo.Accounts a ON a.AccountId = p.CashAccountId
JOIN dbo.Banks b ON b.BankId = a.BankId
WHERE p.PayTypeId = 21.101
AND p.PayDate BETWEEN CAST('1997-04-01' AS DATE) AND CAST('1997-04-12' AS DATE)
) src
PIVOT
(
COUNT(*)
FOR BankName IN (...) -- put quoted list of bank names here
) pvt
ORDER BY [Date]

four queries in one result - oracle

I have four queries.
1.result: Count() | Nazev
result: Count() | Nazev
result: Ode_dne_včetně | Do_dne_včetně | Nazev_organizace | Pocet
result: Nazev | Create_uzivatel | create_cas
I want to have only one result after one click. In this way:
Count() | Nazev | Count() | Nazev | Ode_dne_včetně | Do_dne_včetně | Nazev_organizace | Pocet | Nazev | Create_uzivatel | create_cas
Is it possible?
--first
select count(*),subjekt.nazev from osoba,subjekt where
osoba.ID_PATRI_DO=subjekt.ID group by subjekt.nazev order by
subjekt.nazev;
--second
select count(*),subjekt.nazev from ZADAVACI_POSTUP,subjekt where
ZADAVACI_POSTUP.id_zadavatel=subjekt.ID group by subjekt.nazev order by
subjekt.nazev;
--third
select max(trunc(sysdate)-6) ode_dne_včetně, max(trunc(sysdate))
do_dne_včetně,nazev_organizace,count(*) pocet
from(
select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,
u.username,u.nazev, a.datumzapisauditu
,to_char(a.datumzapisauditu,'DD.MM.YYYY') datum , a.id
from d$caudit a
join cuzivatel u on u.id= a.id_uzivatel
join osoba os on u.id_osoba_bridge = os.id
join t$subjekt subj on subj.id = os.id_patri_do
left join d$caudittyp t on t.id=a.id_audittyp
where datumzapisauditu between trunc(sysdate)-7 AND trunc(sysdate)
order by a.datumzapisauditu desc
)
group by nazev_organizace order by nazev_organizace ;
--fourth
select sb.nazev, lg.create_uzivatel, lg.create_cas from Aplikacni_log lg
join zadavaci_postup zp on zp.id = lg.id_zp
join subjekt sb on sb.id = zp.id_zadavatel
where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY')
order by sb.nazev asc
You can use subquery refactoring to achieve this.
Edit1:- subjekt.nazev is the relationship between the four queries then
you can add WHERE first_qry.nazev=second_qry.nazev and similar relationship with remaining queries.
with first_qry as (select count(*),subjekt.nazev from osoba,subjekt where
osoba.ID_PATRI_DO=subjekt.ID group by subjekt.nazev order by
subjekt.nazev),
second_qry as (select count(*),subjekt.nazev from ZADAVACI_POSTUP,subjekt where
ZADAVACI_POSTUP.id_zadavatel=subjekt.ID group by subjekt.nazev order by
subjekt.nazev),
third_qry as ( select max(trunc(sysdate)-6)
ode_dne_včetně, max(trunc(sysdate))
do_dne_včetně,nazev_organizace,count(*) pocet
from(
select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,
u.username,u.nazev, a.datumzapisauditu
,to_char(a.datumzapisauditu,'DD.MM.YYYY') datum , a.id
from d$caudit a
join cuzivatel u on u.id= a.id_uzivatel
join osoba os on u.id_osoba_bridge = os.id
join t$subjekt subj on subj.id = os.id_patri_do
left join d$caudittyp t on t.id=a.id_audittyp
where datumzapisauditu between trunc(sysdate)-7
AND trunc(sysdate)
order by a.datumzapisauditu desc
)
group by nazev_organizace order by nazev_organizace),
fourth_qry as (select sb.nazev, lg.create_uzivatel,
lg.create_cas from Aplikacni_log lg
join zadavaci_postup zp on zp.id = lg.id_zp
join subjekt sb on sb.id = zp.id_zadavatel
where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY')
order by sb.nazev asc)
select distinct a.*,b.*,c.*,d.*
from first_qry a ,second_qry b,third_qry c,fourth_qry d
Yesterday you asked a similar question and I answered it with this answer.
You can use the same method of using Union or Union all and just select null for each column where you do not have a result
select count(*) AS subjekt_count,subjekt.nazev ,null,null,null,null,null,null
--null columns represent the results from the other queries
from osoba,subjekt
where osoba.ID_PATRI_DO=subjekt.ID
group by subjekt.nazev
UNION ALL
select null, null,count(*) AS subjekt_nazev_count,subjekt.nazev,null,null,null,null
from ZADAVACI_POSTUP,subjekt where
ZADAVACI_POSTUP.id_zadavatel=subjekt.ID
group by subjekt.nazev
---and so on
There are other methods using a WITH statement but you need a common key between the statements and I am not clear on whether your queries are four exclusive queries to the same tables or four overlapping queries. Do you expect duplicates in the results?
All your queries contain column nazev which is grouping and order key so I assumed that this is the field joining results.
If this is so, then you can use follwoing SQL. If this is not what you wanted then please edit question, attach some data, table definitions, preferably as SQL Fiddle and precisely explain your request.
SQLFiddle
with q1 as (select count(*) cnt, subjekt.nazev
from osoba,subjekt where osoba.ID_PATRI_DO=subjekt.ID
group by subjekt.nazev ),
q2 as (select count(*) cnt, subjekt.nazev
from ZADAVACI_POSTUP, subjekt
where ZADAVACI_POSTUP.id_zadavatel=subjekt.ID
group by subjekt.nazev ),
q3 as (select max(trunc(sysdate)-6) ode_dne_vcetne,
max(trunc(sysdate)) do_dne_vcetne, nazev_organizace nazev, count(*) pocet
from (
select to_char(t.popis) popis_typu,subj.nazev nazev_organizace,
u.username, u.nazev, a.datumzapisauditu,
to_char(a.datumzapisauditu,'DD.MM.YYYY') datum, a.id
from d$caudit a
join cuzivatel u on u.id= a.id_uzivatel
join osoba os on u.id_osoba_bridge = os.id
join t$subjekt subj on subj.id = os.id_patri_do
left join d$caudittyp t on t.id=a.id_audittyp
where datumzapisauditu between trunc(sysdate)-7 and trunc(sysdate) )
group by nazev_organizace),
q4 as (select sb.nazev, lg.create_uzivatel, lg.create_cas
from aplikacni_log lg join zadavaci_postup zp on zp.id = lg.id_zp
join subjekt sb on sb.id = zp.id_zadavatel
where lg.create_cas > to_date('08.11.2014', 'DD.MM.YYYY') )
select nazev, q1.cnt cnt1, q2.cnt cnt2, q3.ode_dne_vcetne, q3.do_dne_vcetne,
q3.pocet, q4.create_uzivatel, q4.create_cas
from q1 left join q2 using (nazev) left join q3 using (nazev) left join q4 using (nazev)
order by nazev, create_cas
Output for sample data:
NAZEV CNT1 CNT2 ODE_DNE_VCETNE DO_DNE_VCETNE POCET CREATE_UZIVATEL CREATE_CAS
---------- ---- ---- -------------- ------------- ----- --------------- ----------
SUBJEKT1 1 1 1 2015-03-20
SUBJEKT2 2 1 1 2015-03-20

Count Customers based on item master

I need you to help me on writing two queries in SQL Server 2008 that shows the following information based on item master:
Brand wise count on customer master plus customer who purchased the brand
Item Wise count of customer master plus customer who purchased the item
Here the link that shows the table information and the query which I tried.
Click here to view the table in SQL Fiddle
SELECT
brandname,
division,
route,
DivisionTotalCustomersCount = MAX(DivisionTotalCustomersCount),
RouteTotalCustomersCount = MAX(RouteTotalCustomersCount),
PurchasedCustomersCount = SUM(PurchasedCustomersCount)
FROM
(SELECT
i.brandname,
c.division,
c.route,
DivisionTotalCustomersCount =
(SELECT COUNT(distinct x.CustomerID)
FROM CustomerMaster x
WHERE x.division = c.division),
RouteTotalCustomersCount =
(SELECT COUNT(distinct x.CustomerID)
FROM CustomerMaster x
WHERE x.Route = c.route),
PurchasedCustomersCount = count(distinct C.CustomerID)
FROM CustomerMaster c
LEFT OUTER JOIN SalesData s on c.CustomerID = s.CustomerID
right outer join ItemMaster i on s.item = i.itemcode
GROUP BY i.brandname, c.division, c.route) A
GROUP BY
brandname, division, route
ORDER BY 1
Result Should as below
Excelsheet
I think you need to go reconsider the report and maybe splitting it out into multiple reports.
It does not make sense to have a route count as well as a divisional count if they are counting things at different levels of aggregation. So have a route count and division count report.
Either way, division and route is going to be null for 100PLUS because there are no customers for that brand which means there is no route or division info available.
--Division Count
SELECT BrandName, Division, COUNT(CustomerMaster.CustomerID) [Customer Count]
FROM ItemMaster LEFT OUTER JOIN
SalesData ON ItemMaster.BrandName = SalesData.Brand LEFT OUTER JOIN
CustomerMaster ON SalesData.CustomerID = CustomerMaster.CustomerID
GROUP BY BrandName, Division
--Route Count
SELECT BrandName, Route, Division, COUNT(CustomerMaster.CustomerID) [Customer Count]
FROM ItemMaster LEFT OUTER JOIN
SalesData ON ItemMaster.BrandName = SalesData.Brand LEFT OUTER JOIN
CustomerMaster ON SalesData.CustomerID = CustomerMaster.CustomerID
GROUP BY BrandName, Route, Division
Using your sqlfiddle data there are 25 sales records & 18 distinct brand/ division/ route/ customer records and there are no sales invloving 100PLUS
select
B.BrandName
, V.Division
, coalesce(Brand_count,0) as Brand_count
, coalesce(Division_count,0) as Division_count
from (select distinct BrandName from ItemMaster) as B
cross join (select distinct Division from CustomerMaster) as V
left join (
select
Brand
, Division
, sum(cust_count) over (partition by Brand) as Brand_count
, sum(cust_count) over (partition by Division) as Division_count
from (
select
S.Brand
, C.Division
, count(distinct S.CustomerID) cust_count
from salesdata as S
inner join CustomerMaster as C
on S.CustomerID = C.CustomerID
inner join ItemMaster as I
on S.item = I.ItemCode
group by
S.Brand
, C.Division
) as S
) as D
on B.BrandName = D.Brand
and V.Division = D.Division
order by
B.BrandName
, V.Division
;
| BRANDNAME | DIVISION | BRAND_COUNT | DIVISION_COUNT |
|-----------|----------|-------------|----------------|
| 100PLUS | Dubai | 0 | 0 |
| 100PLUS | RAK | 0 | 0 |
| KITCO | Dubai | 9 | 11 |
| KITCO | RAK | 9 | 7 |
| Red Bull | Dubai | 9 | 11 |
| Red Bull | RAK | 9 | 7 |
http://sqlfiddle.com/#!3/fecb0/27
All Credit to #kevriley
select
A.BrandName,
B.Division,
B.Route,
B.DivisionTotalCustomers,
B.RouteTotalCustomers,
isnull(C.PurchasedCustomersCount,0) as PurchasedCustomersCount
from
(
select distinct
BrandName, Route, Division
from dbo.ItemMaster
cross join dbo.CustomerMaster
) A
join
(
select distinct
Division,
Route,
DENSE_RANK() over (partition by Division order by c.CustomerID asc) + DENSE_RANK() over (partition by Division order by c.CustomerID desc) - 1 as DivisionTotalCustomers ,
DENSE_RANK() over (partition by ROUTE order by c.CustomerID asc) + DENSE_RANK() over (partition by ROUTE order by c.CustomerID desc) - 1 as RouteTotalCustomers
from CustomerMaster c
left join SalesData s on c.CustomerID = s.CustomerID
) B on B.Division = A.Division and B.Route = A.Route
left join
(
select
s.brand,
c.division,
c.route,
PurchasedCustomersCount = count(distinct C.CustomerID)
FROM CustomerMaster c
JOIN SalesData s on c.CustomerID = s.CustomerID
--join ItemMaster i on s.item = i.itemcode
GROUP by s.brand, c.division, c.route
) C on A.Brandname = C.Brand and C.Division = A.Division and C.Route = A.Route
See the same on SQL Fiddle
Select B.Brandname,B.Division,C AS DivisionTotalCustomerCount,
ISNULL(T.Count,0) AS PURCHASEDCUSTOMERSCOUNT
from
(
Select CM.Division,M.BrandName,COUNT(distinct CM.CustomerID) AS C
from dbo.CustomerMaster CM
CROSS JOIN ItemMaster M
GROUP BY CM.Division,M.BrandName
)B
LEFT JOIN
(Select Division,Brand,COUNT(Distinct C.CustomerID) As Count from CustomerMaster C
JOIN salesdata D
On C.CustomerID=D.CustomerID
where D.Brand='Red Bull'
GROUP BY Division,Brand
)T
ON B.Brandname=T.Brand
and B.Division=T.Division
Order by 1,2

I want to write a query to print sum of data for all TC into single table

I have this query to select distinct TCName values from a table:
SELECT DISTINCT(TCName)
FROM [dbo].[TCDetails]
This is another query to sum a data into table:
SELECT
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
I want to write a query so that I am able to print sum of data for all TC into single table
I try this but It's not working
SELECT
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C
INNER JOIN [dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID
INNER JOIN [dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
WHERE TCName in
(
Select distinct
(TCName)
FROM [dbo].[TCDetails]
)
I wanted to print it like
TCName | sum(BS.BLDOS) | sum(BS.CollectedAmount)
xyz | 23456 | 6755
tyu | 34556 | 567898
bnv | 21467 | 345
You need a group by if you want multiple rows in the output
SELECT TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount
FROM [Customer] C INNER JOIN
[dbo].[BillingStatus] BS
ON BS.CustomerID = C.CustomerID INNER JOIN
[dbo].[TCDetails] TC
ON TC.CustomerID = BS.CustomerID
WHERE TCName in (Select distinct(TCName) FROM [dbo].[TCDetails])
GROUP BY TCName;

Get Total Branch wise in SQL Server

SELECT b.BranchName ,
pm.AgreementValue
FROM dbo.Member AS m
INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )
this is the result of above query
BranchName AgreementValue
------------------------------
abc 60000.00
abc 36000.00
abc 36000.00
xyz 20000.00
xyz 10000.00
now i want to get to total of AgreementValue BranchName wise..thanks for help
GROUP BY b.BranchName with SUM like so:
SELECT b.BranchName ,
SUM(pm.AgreementValue) TotalValue
FROM dbo.Member AS m
INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )
GROUP BY b.BranchName;
If you are trying to get the total on each row, then use the window function for sum():
SELECT b.BranchName ,
pm.AgreementValue,
sum(pm.AgreementValue) over (partition by b.BranchName) as BranchTotal
FROM dbo.Member AS m
INNER JOIN dbo.PlanMaster AS pm ON ( m.PlanId = pm.PlanId )
INNER JOIN dbo.Branch AS b ON ( b.BranchId = m.BranchId )