Error in SSRS when trying to update my dataset - sql

I am trying to update one of my datasets in SSRS, and I am receiving the following error
"Could not update a list of fields for the query. Verify that you can connect to the data source and that your query syntax is correct"
My query is:
`SELECT eg.Name AS PropertyGroupName
, CASE ec.RAG WHEN 0 THEN 'Green' WHEN 1 THEN 'Amber' ELSE 'Red' END AS RAGColor
, CASE ec.RAG WHEN 0 THEN 'Green' WHEN 1 THEN 'Gold' ELSE 'Red' END AS RAGDisplayColor
, en.PropertyReference
, en.Name
, en.Address1
, en.Address2
, en.Address3
, en.Address4
, en.PostCode
, ec.ComplianceType AS [Compliance Type]
, ec.NextEventDate
, ec.RAG
, ec.Notes AS ComplianceDetail
FROM tEntityCompliance AS ec
INNER JOIN tEntity AS en ON en.ID = ec.EntityID
INNER JOIN tEntityGroupEntity AS ege ON ege.EntityID = en.ID
INNER JOIN tEntityGroup AS eg ON eg.ID = ege.EntityGroupID
WHERE (ec.CompanyID = #CompanyID)
AND (en.CompanyID = #CompanyID)
AND (eg.CompanyID = #CompanyID)
AND (en.Deleted = 0)
AND (en.Status = 1)
AND (eg.Name <> 'All properties')
AND (ec.ComplianceType IN (#ComplianceTypeFilter))
AND (eg.ID IN (#PropertyGroupNames))
AND (ec.RAG <> 0)'

Related

BigQuery | How to select * from previously defined CTE in the same query?

I have the below SQL query which I want to run on BigQuery.
I am constantly getting error : Unrecognized name: nojoin on the start of the select statement of CTE named onejoin.
Please help. It works for uom_conversion.* in nojoin CTE but nojoin.* doesnt work in onejoin CTE.
WITH uom_conversion AS
(
select a.*,
case when a.sales_unit in ('EA','ZIN') and b.unit = 'CSE' then ifnull(a.order_quan*b.bic_zconvfac,0.0) else a.order_quan end as cse_order_quan,
from `dev-amea-analyt-datal-svc-db.dev_amea_h_sapbw_ptc_otc.v_sales_order_items_amea` as a
left join `dev-amea-analyt-datal-svc-db.dev_amea_h_sapbw_mst.v_mat_unit_otc_amea` as b
on a.material = b.material and a.sales_unit = b.bic_zaltunit
),
nojoin AS
(
SELECT uom_conversion.*,
case when unit_of_wt = 'KG' and doc_type = 'ZOR' then net_wgt_dl end as ship_ord_kg_mysg_osa
from uom_conversion
),
onejoin AS
(
SELECT nojoin.*,
case when orders.doc_type = 'ZOR' and orders.reason_rej = '0' and delivs.biczdelv_itm = '0' and orders.unit_of_wt = 'KG' then orders.net_wgt_dl end as ship_ooqty_kg_mysg_osa
from nojoin as orders
left join `dev-amea-analyt-datal-svc-db.dev_amea_r_sapbw_ap_ptc_otc.v_sales_delivery_items_ap` delivs
on orders.doc_number = delivs.doc_number
),
twojoins AS
(
SELECT onejoin.*
case when orders.doc_type = 'ZOR' and orders.reason_rej = '0' and delivs.biczdelv_itm != '0' and bills.bill_num = '' and orders.unit_of_wt = 'KG' then orders.net_wgt_dl end as ship_nsord_kg_mysg_osa,
from onejoin as orders
left join `dev-amea-analyt-datal-svc-db.dev_amea_r_sapbw_ap_ptc_otc.v_sales_delivery_items_ap` as delivs
on orders.doc_number = delivs.doc_number
left join `dev-amea-analyt-datal-svc-db.dev_amea_r_sapbw_ap_ptc_otc.v_sales_billing_items_ap` as bills
on orders.doc_number = bills.doc_number
)
SELECT *
from twojoins

Two multi-value parameters in SSRS

I have two multi-value parameters in my SQL: where (PayToTIN IN (#PayToTIN) or PCP_ProvId IN (#PCP_ProvId). I am trying to drop in multiple values in either or parameters however when I drop in a value in either parameter, SSRS treats the other parameter as ''. I am also unable to null out the either param because they are multi-value. Is there a workaround to run multiple values in either one or the other parameter?
Declare #PayToTIN as varchar(max) = '562660402' --'562660402'
Declare #PCP_ProvId as varchar(max) --= 'LASRL12550' --'LASRL12550'
declare #detailfields as varchar(max) = NULL
declare #PayToTIN_switch varchar(max) =
( select max(PayToTIN)
from LA_Temp.dbo.vMemberPCP
where cast(PayToTIN as varchar) in (#PayToTIN)
)
declare #PCPAssignmentID_switch varchar(max) =
( select max(PCP_ProvId)
from LA_Temp.dbo.vMemberPCP
where cast(PCP_ProvId as varchar) in (#PCP_ProvId)
)
declare #include_PayToTIN bit = case when 'Pay To Tin' in (#detailfields) then 1 else 0 end
declare #include_PCP_ProvId bit = case when 'PCP ProvID' in (#detailfields) then 1 else 0 end
select distinct
x.CarrierMemID
-- , MemberName
, MbrLastName =rtrim( e.LastName)
, MbrFirstName = rtrim(e.FirstName)
, MbrMI = rtrim(e.MiddleName)
, x.DOB
, x.Age
, x.Sex
, Address = e.addr1
, City = e.city
, State = e.state
, Zip = e.zip
, Parish = e.county
, MemPhone = e.phone
, PCPName
, PCP_NPI
, PCPProvID
, PCPEffDate
, Specialty
, ServiceLocation
, PayToProvider
, PayToTIN
, PayToProvID
, PCP_ProvId
, PCPAssignment
from LA_Temp.dbo.vMemberPCP x
inner join PlanReport_QNXT_LA.dbo.enrollkeys ek
on x.enrollid = ek.enrollid
inner join PlanReport_QNXT_LA.dbo.orgpolicy o
on o.orgpolicyid = ek.orgpolicyid
and right(rtrim(o.policynum),2) <> 'BH'
inner join PlanReport_QNXT_LA.dbo.member m
on ek.memid = m.memid
inner join PlanReport_QNXT_LA.dbo.entity e
on e.entid = m.entityid
--where PayToTIN = #PayToTIN --AMB 05/07/18
where (PayToTIN IN (#PayToTIN)
or '' = #PayToTIN_switch
)
or ( PCP_ProvId IN (#PCP_ProvId)
or '' = #PCPAssignmentID_switch
)
where (case when PayToTIN = '' then null else PayToTIN end in (#PayToTIN))
OR (case when PCP_ProvId = '' then null else PCP_ProvId end in (#PCP_ProvId))

Sql select statement that can count rows in a table not in the main query

I have an old query that I want to now count the rows of wbs.WBSNodeGuid in a table not included in the main query. I've tried doing this with COUNT() in a separate select statement within the main SELECT, but of course it doesn't understand wbs within the SELECT Count() statement. How can I count the rows from the other table to match the main query's current row's wbs.WBSNodeGuid value?
Note that this example is much simpler than the original, but should get acress the general idea of what I tried and what I'm looking for.
SELECT wbs.WBSNodeGuid
, (SELECT COUNT(*) FROM Marker mkr WHERE mkr.WBSNodeGuid = wbs.WBSNodeGuid AND Deleted = 0)
FROM WBSNode wbs
Above is a simplified version. Below is the actual that I am trying to add to.
SELECT wbs.WBSNodeText
, wbs.WBSNodeOverallIndex
, wbs.WBSNodePath
--***THIS IS THE COLUMN I'M TRYING TO ADD**** (SELECT COUNT(*) FROM Marker mkr WHERE mkr.WBSNodeGuid = wbs.WBSNodeGuid AND Deleted = 0)
, insp.WBSNodeInspectionGuid
, insp.WBSInspInspector
, insp.WBSInspDate
, insp.WBSInspNote
, image0.WBSInspImageBlobPartialPath Image0
, image1.WBSInspImageBlobPartialPath Image1
, opt.POptionDefaultValue NetworkImagePath
, opt2.POptionDefaultValue WBSPathStartDepth
FROM ProjectOption opt
, ProjectOption opt2
, WBSNode wbs
, WBSNodeInspection insp
LEFT OUTER JOIN WBSNodeInspectionImage image0
ON insp.WBSNodeInspectionGuid = image0.WBSNodeInspectionGuid
AND image0.WBSInspImageIndex = 0
AND image0.Deleted = 0
LEFT OUTER JOIN WBSNodeInspectionImage image1
ON insp.WBSNodeInspectionGuid = image1.WBSNodeInspectionGuid
AND image1.WBSInspImageIndex = 1
AND image1.Deleted = 0
INNER JOIN Marker mkr ON mkr.WBSNodeGuid = wbs.WBSNodeGuid AND mkr.Deleted = 0
WHERE wbs.WBSNodeGuid = insp.WBSNodeGuid
AND opt.ProjectOption = 'NetworkPathToProjectImages'
AND opt2.ProjectOption = 'LevelToStartLocationPortionOfWBSNodePath'
AND opt.Deleted = 0
AND opt2.Deleted = 0
AND wbs.Deleted = 0
AND insp.Deleted = 0
ORDER BY wbs.WBSNodeText, insp.WBSInspDate
Does this work?
SELECT count(distinct wbs.WBSNodeGuid) as [Count]
FROM WBSNode wbs
INNER JOIN Marker mkr ON mkr.WNSNodeGuid = wbs.WBSNodeGuid AND Deleted = 0
If that's not what you're looking for, how about this?
SELECT wbs.WBSNodeGuid
,count(*) as [Count]
FROM WBSNode wbs
INNER JOIN Marker mkr ON mkr.WNSNodeGuid = wbs.WBSNodeGuid AND Deleted = 0
GROUP BY wbs.WBSNodeGuid
Based on your new updates, here this query should work
SELECT wbs.WBSNodeText
, wbs.WBSNodeOverallIndex
, wbs.WBSNodePath
,(select count (*) from Marker mkr WHERE mkr.WBSNodeGuid = wbs.WBSNodeGuid AND mkr.Deleted = 0 ) as [Deleted Count]
, insp.WBSNodeInspectionGuid
, insp.WBSInspInspector
, insp.WBSInspDate
, insp.WBSInspNote
, image0.WBSInspImageBlobPartialPath Image0
, image1.WBSInspImageBlobPartialPath Image1
, opt.POptionDefaultValue NetworkImagePath
, opt2.POptionDefaultValue WBSPathStartDepth
FROM ProjectOption opt
, ProjectOption opt2
, WBSNode wbs
, WBSNodeInspection insp
LEFT OUTER JOIN WBSNodeInspectionImage image0
ON insp.WBSNodeInspectionGuid = image0.WBSNodeInspectionGuid
AND image0.WBSInspImageIndex = 0
AND image0.Deleted = 0
LEFT OUTER JOIN WBSNodeInspectionImage image1
ON insp.WBSNodeInspectionGuid = image1.WBSNodeInspectionGuid
AND image1.WBSInspImageIndex = 1
AND image1.Deleted = 0
WHERE wbs.WBSNodeGuid = insp.WBSNodeGuid
AND opt.ProjectOption = 'NetworkPathToProjectImages'
AND opt2.ProjectOption = 'LevelToStartLocationPortionOfWBSNodePath'
AND opt.Deleted = 0
AND opt2.Deleted = 0
AND wbs.Deleted = 0
AND insp.Deleted = 0
ORDER BY wbs.WBSNodeText, insp.WBSInspDate

SQL query cannot be converted to LINQ

When I'm trying to convert this piece of SQL to LINQ, I received this error:
SQL cannot be converted to LINQ: Table [GL] not found in the current
Data Context.
But in SQL it works fine.
This is my SQL query:
SELECT itm.Id ,
ISNULL(itm.Debit, 0) AS Debit ,
ISNULL(itm.Credit, 0) AS Credit ,
itm.State ,
itm.DocCreateDate ,
ISNULL(itm.Num, 0) AS Num ,
ISNULL(itm.DocTypeRef, 0) AS DocTypeRef ,
itm.Year ,
itm.Month ,
ISNULL(itm.DebitCount, 0) AS DebitCount ,
ISNULL(itm.CreditCount, 0) AS CreditCount ,
itm.DL ,
itm.DL2 ,
itm.DL3 ,
itm.DL4 ,
itm.DL5 ,
itm.DL6 ,
itm.DL7 ,
ISNULL(itm.FCRef, 0) AS FCRef ,
itm.FollowUpNum ,
ISNULL(itm.BranchRef, 1) AS BranchRef ,
itm.DocHeaderRef ,
ISNULL(itm.RowNum, 0) AS RowNum ,
ISNULL(itm.DailyNum, 0) AS DailyNum ,
ISNULL(itm.TempNum, 0) AS TempNum ,
ISNULL(itm.RefNum, 0) AS RefNum ,
itm.Descript ,
itm.Count ,
itm.FollowUpDate ,
itm.FCVal ,
itm.FCRateVal ,
itm.FactorNum ,
ISNULL(itm.DebitFCVal, 0) AS DebitFCVal ,
ISNULL(itm.CreditFCVal, 0) AS CreditFCVal ,
sl.Id AS SLRef ,
sl.SLCode ,
sl.Title AS SLTitle ,
CASE WHEN ISNULL(sl.DLSRef, 0) > 0 THEN 1
ELSE 0
END AS HasDL ,
CASE WHEN ISNULL(sl.DLSRef2, 0) > 0 THEN 1
ELSE 0
END AS HasDL2 ,
CASE WHEN ISNULL(sl.DLSRef3, 0) > 0 THEN 1
ELSE 0
END AS HasDL3 ,
CASE WHEN ISNULL(sl.DLSRef4, 0) > 0 THEN 1
ELSE 0
END AS HasDL4 ,
CASE WHEN ISNULL(sl.DLSRef5, 0) > 0 THEN 1
ELSE 0
END AS HasDL5 ,
CASE WHEN ISNULL(sl.DLSRef6, 0) > 0 THEN 1
ELSE 0
END AS HasDL6 ,
CASE WHEN ISNULL(sl.DLSRef7, 0) > 0 THEN 1
ELSE 0
END AS HasDL7 ,
CASE WHEN ISNULL(sl.HasFC, 0) > 0 THEN 1
ELSE 0
END AS HasFC ,
1 AS HasFollow ,
tl.Id AS TLRef ,
tl.Title AS TLTitle ,
tl.TLCode ,
gl.Id AS GLRef ,
gl.Title AS GLTitle ,
gl.GLCode ,
gl.Balance AS GLBalance
FROM Acc.DocItem AS itm
LEFT OUTER JOIN Acc.SL AS sl ON itm.SLRef = sl.Id
LEFT OUTER JOIN Acc.TL AS tl ON sl.TLRef = tl.Id
LEFT OUTER JOIN Acc.GL AS gl ON tl.GLRef = gl.Id
WHERE ( itm.SLRef > 0 )
If there is no way to pass this error so can you tell me its LINQ equal?
There is a way to do that in linq:
var q =
from c in categories
join p in products on c.Category equals p.Category into ps
from p in ps.DefaultIfEmpty()
select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName };
This is the big idea for left outer join, you as the syntax for 'into' like 'as' in SQL query.

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.