Count for multiple value in table without union all in sql - sql

I have validationErrors with various WIPReason associated with and I want to count based on different WIPReason, I am using Union All and it works but Query is very big with mutiple wip reason more than 10. Please find the query below
SELECT
COUNT(tlv.TransactionLineId) AS TotalErrors,
COUNT(tl.Id) AS TotalLines,
COUNT(tlv.Reason) AS NoWorkRecords,
0 AS ValidationErrors
FROM
dbo.TimesheetCellTransactionLine tctl
INNER JOIN
dbo.TransactionLine tl ON tctl.TransactionLineId = tl.Id
INNER JOIN
dbo.TransactionLineValidation tlv ON tl.Id = tlv.TransactionLineId
INNER JOIN
dbo.WIPReason w ON tlv.Reason = w.Id
WHERE
tl.CurrentStatus = 1
AND w.Id = 4 -- NoWorkRecords
GROUP BY
tlv.TransactionLineId, tl.Id
UNION ALL
SELECT
COUNT(tlv.TransactionLineId) AS TotalErrors,
COUNT(tl.Id) AS TotalLines,
0 AS NoWorkRecords,
COUNT(tlv.Reason) AS ValidationErrors
FROM
dbo.TimesheetCellTransactionLine tctl
INNER JOIN
dbo.TransactionLine tl ON tctl.TransactionLineId = tl.Id
INNER JOIN
dbo.TransactionLineValidation tlv ON tl.Id = tlv.TransactionLineId
INNER JOIN
dbo.WIPReason w ON tlv.Reason = w.Id
WHERE
tl.CurrentStatus = 1
AND w.Id = 1 -- validationErrors
GROUP BY
tlv.TransactionLineId, tl.Id
Is there any other elegant way of doing this for w.Id = 1 to 10
AND w.Id = 1 -- validationErrors
Update :
I want result as columns 10 count columns as I am using this in another big select.

You can use a case statement inside count like this:
SELECT
COUNT(tlv.TransactionLineId) AS TotalErrors,
COUNT(tl.Id) AS TotalLines,
COUNT(case when w.Id = 4 then tlv.Reason else null end) AS NoWorkRecords,
COUNT(case when w.Id = 1 then tlv.Reason else null end) AS ValidationErrors,
... Repeat for remaining w.Id's ...
FROM
dbo.TimesheetCellTransactionLine tctl
INNER JOIN
dbo.TransactionLine tl ON tctl.TransactionLineId = tl.Id
INNER JOIN
dbo.TransactionLineValidation tlv ON tl.Id = tlv.TransactionLineId
INNER JOIN
dbo.WIPReason w ON tlv.Reason = w.Id
WHERE
tl.CurrentStatus = 1
GROUP BY
tlv.TransactionLineId, tl.Id
Note
Since you are using an inner join on tl.Id = tlv.TransactionLineId
TotalErrors/TotalLines are always going to be the same
Grouping by tl.Id and tlv.TransactionLineId is unnecessary

SELECT COUNT(tlv.TransactionLineId) OVER (partition by (w.Id) order by w.Id) AS TotalErrors,
COUNT(tl.Id) OVER (partition by (w.Id) order by w.Id) AS TotalLines,
0 AS NoWorkRecords,
COUNT(tlv.Reason) OVER (partition by (w.Id) order by w.Id) AS ValidationErrors
FROM dbo.TimesheetCellTransactionLine tctl
INNER JOIN dbo.TransactionLine tl ON tctl.TransactionLineId = tl.Id
INNER JOIN dbo.TransactionLineValidation tlv ON tl.Id = tlv.TransactionLineId
INNER JOIN dbo.WIPReason w ON tlv.Reason = w.Id
WHERE tl.CurrentStatus = 1
AND w.Id between 1 and 10 -- validationErrors
GROUP BY tlv.TransactionLineId,
tl.Id,tlv.Reason

Related

Joining two queries to one table and substracting values

i need to put those two output values (Add_sum and Minus_sum) to one table and subtract them (Add_sum - Minus_sum) and show this value.
I tried many other options, subqueries etc but could not get it to work.
Query 1:
SELECT I.ItemCode, COUNT(H.TransactionTypeID) AS ADD_Sum
FROM inMoveHd AS H INNER JOIN
inMoveLn AS L ON L.InvMoveID = H.InvMoveID INNER JOIN
inItem AS I ON I.ItemID = L.ItemID INNER JOIN
inTransactionType AS T ON H.TransactionTypeID = T.TransactionTypeID
WHERE (T.TransactionSign = 1)
GROUP BY I.ItemCode
Query 2:
SELECT I.ItemCode, COUNT(H.TransactionTypeID) AS Minus_Sum
FROM inMoveHd AS H INNER JOIN
inMoveLn AS L ON L.InvMoveID = H.InvMoveID INNER JOIN
inItem AS I ON I.ItemID = L.ItemID INNER JOIN
inTransactionType AS T ON H.TransactionTypeID = T.TransactionTypeID
WHERE (T.TransactionSign = -1)
GROUP BY I.ItemCode
Use case expressions to do conditional aggregation:
SELECT I.ItemCode,
COUNT(case when T.TransactionSign = 1 then H.TransactionTypeID end) AS ADD_Sum,
COUNT(case when T.TransactionSign = -1 then H.TransactionTypeID end) AS Minus_Sum
FROM inMoveHd AS H INNER JOIN
inMoveLn AS L ON L.InvMoveID = H.InvMoveID INNER JOIN
inItem AS I ON I.ItemID = L.ItemID INNER JOIN
inTransactionType AS T ON H.TransactionTypeID = T.TransactionTypeID
WHERE (T.TransactionSign = -1 or T.TransactionSign = 1)
GROUP BY I.ItemCode
I think that you are looking for conditional aggregation:
SELECT
I.ItemCode,
SUM(CASE WHEN T.TransactionSign = 1 THEN 1 ELSE 0 END) AS Add_Sum,
SUM(CASE WHEN T.TransactionSign = -1 THEN 1 ELSE 0 END) AS Minus_Sum,
SUM(T.TransactionSign) difference
FROM
inMoveHd AS H INNER JOIN
inMoveLn AS L ON L.InvMoveID = H.InvMoveID INNER JOIN
inItem AS I ON I.ItemID = L.ItemID INNER JOIN
inTransactionType AS T ON H.TransactionTypeID = T.TransactionTypeID
WHERE T.TransactionSign IN (-1, 1)
GROUP BY I.ItemCode

Sum from the different tables Sql server

I have couple of tables which stores amount and I want to group by and get sum - reason for the mutiple tables are nhibernate descriminators.
I am using Union all and works but query is very big.
I am using following query
SELECT CustomerAccountNumber,
vc.CustomerName,
SUM(PermAmount) AS PermAmount,
SUM(FreetextAmount) AS FreetextAmount,
(SUM(PermAmount) + SUM(FreetextAmount)) AS TotalAmountByCustomer
FROM
(
SELECT pp.CustomerAccountNumber,
pl.Amount AS PermAmount,
0 AS FreetextAmount
FROM dbo.PermanentPlacementTransactionLine pl
INNER JOIN dbo.TransactionLine tl ON pl.TransactionLineId = tl.Id
INNER JOIN dbo.PermanentPlacement pp ON pl.PermanentPlacementId = pp.Id
WHERE tl.CurrentStatus = 1
GROUP BY pp.CustomerAccountNumber,
pl.Amount,
tl.Id
UNION ALL
SELECT ft.CustomerAccountNumber,
0 AS PermAmount,
ft.Amount AS FreetextAmount
FROM dbo.FreeTextTransactionLine fttl
INNER JOIN dbo.TransactionLine tl ON fttl.TransactionLineId = tl.Id
INNER JOIN dbo.[FreeText] ft ON fttl.FreeTextId = ft.Id
WHERE tl.CurrentStatus = 1
GROUP BY ft.CustomerAccountNumber,
ft.Amount,
tl.Id
) WIPSummary
INNER JOIN dbo.vw_Customer vc ON WIPSummary.CustomerAccountNumber = vc.CustomerAccount
GROUP BY CustomerAccountNumber,
vc.CustomerName;
is there any elegant way of displaying amount in separate columns ?
I can use partition by if it was same table and want to display row by row.
Try these query, is easy to understand and probably faster than yours.
I assume that the values are unique in your view
WITH cte_a
AS (SELECT pp.customeraccountnumber
,Sum(pl.amount) AS PermAmount
,0 AS FreetextAmount
FROM dbo.permanentplacementtransactionline pl
INNER JOIN dbo.transactionline tl
ON pl.transactionlineid = tl.id
INNER JOIN dbo.permanentplacement pp
ON pl.permanentplacementid = pp.id
WHERE tl.currentstatus = 1
GROUP BY pp.customeraccountnumber),
cte_b
AS (SELECT ft.customeraccountnumber
,0 AS PermAmount
,Sum(ft.amount) AS FreetextAmount
FROM dbo.freetexttransactionline fttl
INNER JOIN dbo.transactionline tl
ON fttl.transactionlineid = tl.id
INNER JOIN dbo.[freetext] ft
ON fttl.freetextid = ft.id
WHERE tl.currentstatus = 1
GROUP BY ft.customeraccountnumber)
SELECT vc.customeraccountnumber
,vc.customername
,Isnull(A.permamount, 0) AS PermAmount
,Isnull(B.freetextamount, 0) AS FreetextAmount
,Isnull(A.permamount, 0)
+ Isnull(B.freetextamount, 0) AS TotalAmountByCustomer
FROM dbo.vw_customer vc
LEFT JOIN cte_a a
ON vc.customeraccount = A.customeraccountnumber
LEFT JOIN cte_b b
ON vc.customeraccount = A.customeraccountnumber
if no table structures and sample data, that is the best I can do to help you.

Remove Almost Duplicate Rows in SQL from a select query

I'm trying to select some data but I'm getting 'almost duplicated rows' -
all of the columns are the same except the 'gtin' column, the result I'm expecting is to get only one line of each of those 'almost duplicated rows'
The result I'm trying to achieve is to only get row 1 & 3.
this is the query I'm using:
SELECT DISTINCT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
pid.PACKAGE_IDENTIFIER AS gtin,
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
Please help, thanks.
You can't do it with distinct, since distinct works on full rows only.
Your best option is to use group by:
SELECT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
MIN(pid.PACKAGE_IDENTIFIER) AS gtin, -- Or max, if you want to...
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
GROUP BY ii.ITEM_ID,
ii.PARENT_ITEM_ID,
ii.ITEM_IDENTIFIER,
id.DESCRIPTION_1,
id.DESCRIPTION_4,
pac.PRE_PRICE_AMOUNT
Another way is to use Row_Number to avoid this group by:
Select * from (
SELECT DISTINCT 'Y' AS isEnterprise,
ii.ITEM_ID AS itemId,
ii.PARENT_ITEM_ID as PID,
ii.ITEM_IDENTIFIER AS productNo,
id.DESCRIPTION_1 AS description,
id.DESCRIPTION_4 AS packValue,
pid.PACKAGE_IDENTIFIER AS gtin,
RowN = Row_Number() over (Partition by ii.Item_Id, ii.Parent_Item_id order by pid.PACKAGE_IDENTIFIER) --Added new row_number if you want max you can use desc
pac.PRE_PRICE_AMOUNT AS eachPrice
FROM PWRNXGDTA.ITEM_INFORMATION AS ii
JOIN PWRNXGDTA.ITEM_ASSORTMENT AS ia ON ii.ITEM_ID = ia.ITEM_ID
JOIN PWRNXGDTA.ITEM_DESCRIPTION AS id ON id.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.BUSINESS_UNIT AS bu ON ii.LOGISTICS_BU_ID = bu.BU_ID
LEFT JOIN PWRNXGDTA.ITEM_EXTN_NONMDM AS ien ON ien.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_CLASS AS lcc ON lcc.CLASSIFICATION_CLASS_ID = ii.CLASSIFICATION_CLASS_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBH AS lcp ON lcp.CLASSIFICATION_PBH_ID = ii.CLASSIFICATION_PBH_ID
LEFT JOIN PWRNXGDTA.LKP_CLASSIFICATION_PBHF AS lcpf ON lcpf.CLASSIFICATION_PBHF_ID = ii.CLASSIFICATION_PBHF_ID
LEFT JOIN PWRNXGDTA.LIST_PROPRIETARY_ITEM AS lpi ON lpi.ITEM_IDENTIFIER = ii.ITEM_IDENTIFIER
JOIN PWRNXGDTA.PA_ASSORTMENT AS pa ON pa.PACKAGE_ASSORTMENT_ID = ia.PACKAGE_ASSORTMENT_ID
JOIN PWRNXGDTA.ITEM_PRODUCT AS ip ON ip.ITEM_ID = ii.ITEM_ID
JOIN PWRNXGDTA.PRODUCT_INFO AS pin ON pin.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.PRODUCT_NUTRITIONAL AS pn ON pn.PRODUCT_ID = ip.PRODUCT_ID
LEFT JOIN PWRNXGDTA.LKP_BRAND_INFORMATION AS lbi ON lbi.BRAND_ID = pin.BRAND_ID
AND lbi.RECORD_STATUS = 'A'
JOIN PWRNXGDTA.PACKAGE_IDENTIFIER AS pid ON pid.PACKAGE_ID = pa.PACKAGE_ID
JOIN PWRNXGDTA.PA_CONSUMABLE AS pac ON pac.PACKAGE_ASSORTMENT_ID = pa.PACKAGE_ASSORTMENT_ID
where id.DESCRIPTION_1 like '%HAAGEN DAZ VAN MLK CHO BR%'
) a
Where a.RowN = 1

Subquery in from clause, Invalid Identifier in Where clause

select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from
iiasa_inventory.inv_device_2_persons_cc) dp
on dp.device_id = d.id
where dp.active = 1
I am trying to select my data but the where-clause says that "dp.active" is an INVALID Identifier. This is probably because the table dp is in the subquery. I have tried to give it an alias name and some other things I found while browsing stackoverflow, but I cant seem to find a solution. Any idea?
This is Oracle PL/SQL.
Put the check for active = 1 in the subquery as shown below.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id from iiasa_inventory.inv_device_2_persons_cc where active = 1) dp on dp.device_id = d.id
That is because you are not selecting active column in dp.
select * from iiasa_inventory.inv_device d
join iiasa_inventory.inv_type ty on d.type_id = ty.id
join iiasa_inventory.inv_category c on ty.category_id = c.id
join iiasa_inventory.inv_device_2_barcode b on b.device_id = d.id
join iiasa_inventory.inv_barcodes bc on b.barcode_id = bc.id
join iiasa_inventory.inv_status s on d.status = s.id
join iiasa_inventory.inv_brand br on ty.brand_id = br.id
left join iiasa_inventory.inv_supplier su on su.id = d.supplier_id
left join iiasa_inventory.inv_supplier sup on sup.id = d.maintenance_with
left join (select distinct device_id,active from iiasa_inventory.inv_device_2_persons_cc) dp on dp.device_id = d.id
where dp.active = 1
OR you can just filter from the subquery itself. Like:
left join (select distinct device_id
from iiasa_inventory.inv_device_2_persons_cc
where active=1) dp on dp.device_id = d.id

How to link sql sub queries together?

is it possible to link the following sub query (see below.) The first three sub queries work fine however I'm struggling to see how to do the rest any guidance would be great cheers.
Ps. apologies for the long code
SELECT c.[Status],
c.CompanyId,
c.Name,
(SELECT count(DISTINCT usr.UserID)
FROM [ondemand.10cms.com].Security.[user] usr
INNER JOIN [ondemand.10cms.com].Company.Company
ON usr.CompanyID = Company.CompanyID
WHERE usr.CompanyID = c.CompanyID) AS TotalUsers,
(SELECT sum (CASE WHEN usr.Status = 2 THEN 1 ELSE 0 END)
FROM [ondemand.10cms.com].Security.[user] usr
INNER JOIN [ondemand.10cms.com].Company.Company
ON usr.CompanyID = Company.CompanyID
WHERE usr.CompanyID = c.CompanyID) AS ActiveUsers,
(SELECT sum (CASE WHEN usr.Status = 3 THEN 1 ELSE 0 END)
FROM [ondemand.10cms.com].Security.[User] usr
INNER JOIN [ondemand.10cms.com].Company.Company
ON usr.CompanyID = Company.CompanyID WHERE usr.CompanyID = c.CompanyID) AS SuspendedUsers,
(Select COUNT (distinct usrs.id)
From [ondemand.10cms.com].Security.UserSession usrs
inner join [ondemand.10cms.com].Security.[user] usr on usrs.UserID=usr.UserID
) as TotalLogin,
(Select
COUNT( MerchandisingModule.Name)
From [ondemand.10cms.com].Project.Template
inner join [ondemand.10cms.com].Project.MerchandisingModule on Template.TemplateID= MerchandisingModule.TemplateId
)as CurrentModules,
(Select
count(MerchandisingModule.CreatedDate)
from [ondemand.10cms.com].Project.MerchandisingModule
inner join [ondemand.10cms.com].Project.Template on Template.TemplateID= MerchandisingModule.TemplateId
)as ModulesCreated,
(Select
count(mm.UpdatedDate)
from [ondemand.10cms.com].Project.MerchandisingModule mm
inner join [ondemand.10cms.com].Project.Template on Template.TemplateID= mm.TemplateId
)as ModulesUpdated,
(Select
COUNT(MA.MerchandisingAreaID)
from [ondemand.10cms.com].Project.MerchandisingArea MA
inner join [ondemand.10cms.com].Project.Project on Project.ProjectID= MA.ProjectID
) as Currentareas,
(Select
COUNT (MA.name)
from [ondemand.10cms.com].Project.MerchandisingArea MA
inner join [ondemand.10cms.com].Project.Project on Project.ProjectID= MA.ProjectID
) as AreasCreated,
(select
COUNT (MerchandisingArea.UpdatedDate)
from [ondemand.10cms.com].Project.MerchandisingArea
inner join [ondemand.10cms.com].Project.Project on Project.ProjectID= MerchandisingArea.ProjectID
) as AreasUpdated,
(Select
SUM ( case when MA.PublishStatus = 1 then 1 else 0 end)
from [ondemand.10cms.com].Project.MerchandisingArea MA
inner join [ondemand.10cms.com].Project.PublishingStatus on PublishingStatus.PublishStatusId = MA.PublishStatus
) as SuccessPublished,
(Select
SUM ( case when MA.PublishStatus = 3 then 1 else 0 end)
from [ondemand.10cms.com].Project.MerchandisingArea MA
inner join [ondemand.10cms.com].Project.PublishingStatus on PublishingStatus.PublishStatusId= MA.PublishStatus
) as FailedPublished
from [ondemand.10cms.com].Company.Company c
This was fixed by adding another inner join to each sub query and also another where clause