sql query works in azure data studio but returns nothing via vs code - sql

I have the following SQL query that returns a few rows when I try in Azure Data Studio:
SELECT
A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivity,
ActivityName as ActivityType
FROM
WidgetActivity A
INNER JOIN
WidgetUsers ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = 4
AND WidgetUsers.Branch = '52'
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -300, GETDATE()) AND GETDATE()
AND A.ActivityName IN ('Action1', 'Action2')
GROUP BY
A.ActivityName, A.ReportRefreshDate
This returns a few rows like this:
ReportRefreshDate TotalActivity ActivityType
-----------------------------------------------------
2022-05-16 07:18:06.000 9 Action1
2022-05-16 07:18:06.000 3 Action2
But when I try this in VS Code, it returns an empty result set.
Here's the code snippet:
Console.WriteLine(activityFilter);
activityByActivityType = conn.Query<WidgetActivityDetailsByActivityTypes>(
#"
SELECT A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivityCount, ActivityName AS ActivityType
FROM WidgetActivity A
INNER JOIN WidgetUsers
ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = #TM
AND WidgetUsers.Branch = #Branch
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -#RefreshDate, GETDATE()) AND GETDATE()
AND A.ActivityName IN (#ActivityList)
GROUP BY A.ActivityName, A.ReportRefreshDate
",
new { Branch=branchId, TM = tmid, RefreshDate = dateRange, ActivityList = activityFilter}).ToList();
}
return activityByActivityType;
I think the problem is related to the #ActivityList variable because when I remove that AND clause, a bunch of data is returned.
The Console.Writeline on activityFilter returns this:
'Activity1', 'Activity2'
which looks correct to me.
I'm sure it's something simple I've missed but I can't see it.
Any help would be appreciated.

I had to change the sql to look like this:
activityByActivityType = conn.Query<WidgetActivityDetailsByActivityTypes>(
$#"
SELECT A.ReportRefreshDate, COUNT(ActivityName) AS TotalActivityCount, ActivityName AS ActivityType
FROM WidgetActivity A
INNER JOIN WidgetUsers
ON LOWER(WidgetUsers.UserPrincipalName) = LOWER(A.UserId)
WHERE
A.TMID = #TM
AND WidgetUsers.Branch = #Branch
AND A.ReportRefreshDate BETWEEN DATEADD(DD, -#RefreshDate, GETDATE()) AND GETDATE()
AND A.ActivityName IN ({activityFilter})
GROUP BY A.ActivityName, A.ReportRefreshDate
",
new { Branch=branchId, TM = tmid, RefreshDate = dateRange}).ToList();
}

Related

IBM Maximo Where Clause

I am trying to create a where clause to return all work orders between set dates where the there are no ACTUALS recorded (no labtrans). I have an existing WC which i am using as a template for this one but i am stuck with the "where" element.
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
istask = 0 and
worktype = 'PPM' and *This could be removed*
targcompdate >= { ts '2020-05-01 00:00:00.000' } and
targcompdate >= { ts '2020-05-05 00:00:00.000' } and
not exists?? there are no actuals are on the work order?
Many Thanks,
G
The easy way, trusting that the rest of Maximo has done its job like it normally does, would be something like the following. (I'm going from memory. You can double-check the column names on the WORKORDER object / table in Database Configuration or in your database browser tool, like SQL Developer.)
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
istask = 0 and
worktype = 'PPM' and *This could be removed*
targcompdate >= { ts '2020-05-01 00:00:00.000' } and
targcompdate >= { ts '2020-05-05 00:00:00.000' } and
actlabcost = 0 and
actmatcost = 0 and
actservcost = 0 and
acttoolcost = 0
This works for me:
(woclass = 'WORKORDER' or woclass = 'ACTIVITY') and
(targcompdate between '2020-05-01' and '2020-05-05' and historyflag = 0 and istask = 0)
and not exists(select 1 from labtrans where refwo=workorder.wonum and siteid=workorder.siteid)

Oracle query works in Toad Developer but not in .net application

This query works in Toad developer but not(wont return results) in an .net application in visual studio. Any clues why?
The query work if I omit the line -- having sum(total_cust_cnt) >= NVL (:customers_out, 100)
select d.region_nbr, pi.city, d.case_id, c.cause_desc cause, sum(total_cust_cnt) customers, d.total_duration_minutes
from t_om_cause_of_trouble c,
t_om_archive_hist_device d,
t_om_archive_hist_loc l,
t_om_pod_info pi
where c.cause = d.cause_of_trouble
and pi.city is not null
and pi.total_cust_cnt > 0
and pi.company = l.company
and pi.distribution_location = l.distribution_location_nbr
and l.company = d.company
and l.region_nbr = d.region_nbr
and l.outage_system = d.outage_system
and l.case_id = d.case_id
and d.first_call_date_time >= :start_date
and d.first_call_date_time <= :end_date
and d.service_request_type = 'LGTS'
and d.cause_of_trouble not in
(select distinct cs.cause from t_om_cause_by_summary cs, t_om_cause_of_trouble c
where summary_cause = 'ERROR' and cs.cause = c.cause)
and d.device_type <> 'ERR'
and d.total_duration_minutes >= 60 * NVL(:hours_out,4)
and d.total_duration_minutes > 5
and d.total_customers_affected >= NVL (:customers_out, 100)
and to_char(d.region_nbr) like :region_nbr
group by d.region_nbr, pi.city, d.case_id, c.cause_desc, d.total_duration_minutes
/* having sum(total_cust_cnt) >= NVL (:customers_out, 100) */
order by d.region_nbr, pi.city, d.case_id
)
group by region_nbr, city, cause
order by region_nbr, city, cause
) stats
where r.region = stats.region_nbr
)

The multi-part identifier could not be bound

SELECT
TOP (100) PERCENT
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
CASE WHEN udJobType IN ('Scheduled Maintenance', 'Unscheduled Emergency',
'Unscheduled Call Out') THEN 'Maintenance'
WHEN udJobType IN ('Scheduled Special Projects', 'UPS Internal Capital Exp')
THEN 'Capital'
WHEN udJobType LIKE '%Turnaround%' THEN 'T/A'
END AS JobType,
CASE WHEN Factor = 1.0 THEN 'ST'
WHEN Factor = 1.5 THEN 'OT'
WHEN Factor = 2.0 THEN 'OT'
END AS STOT,
SUM(dbo.bJBID.Hours) AS Hours,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) as SortMonth
FROM
dbo.bJBID
INNER JOIN
dbo.bJBIN ON dbo.bJBID.JBCo = dbo.bJBIN.JBCo AND dbo.bJBID.BillMonth = dbo.bJBIN.BillMonth AND dbo.bJBID.BillNumber = dbo.bJBIN.BillNumber
INNER JOIN
dbo.bARCM
INNER JOIN
dbo.bJCCM ON dbo.bARCM.CustGroup = dbo.bJCCM.CustGroup AND dbo.bARCM.Customer = dbo.bJCCM.Customer
INNER JOIN
dbo.JCJMPM ON dbo.bJCCM.JCCo = dbo.JCJMPM.JCCo AND dbo.bJCCM.Contract = dbo.JCJMPM.Contract ON dbo.bJBIN.JBCo = dbo.JCJMPM.JCCo AND
dbo.bJBIN.Contract = dbo.JCJMPM.Contract
INNER JOIN
dbo.bJCCT ON dbo.bJBID.CostType = dbo.bJCCT.CostType AND dbo.bJBID.PhaseGroup = dbo.bJCCT.PhaseGroup
INNER JOIN
dbo.budAcctMonths ON dbo.budAcctMonths.Month = dbo.bJBIN.BillMonth
WHERE
(dbo.bJCCM.JCCo = 1)
AND (dbo.bJBID.CostType IN (1, 41, 42, 43, 44, 45, 46))
AND (dbo.bJBID.CostTypeCategory = 'L')
AND (dbo.JCJMPM.udPlantLocation LIKE 'Deer%')
AND (dbo.bARCM.Name LIKE 'Dow%' OR dbo.bARCM.Name LIKE 'Rohm%')
GROUP BY
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
dbo.JCJMPM.udJobType, dbo.bJBID.Factor, dbo.SortMonth
HAVING
(dbo.bARCM.CustGroup = 1) AND (SUM(dbo.bJBID.Hours) <> 0)
When I excute this query, I get
The multi-part identifier "dbo.SortMonth" could not be bound
error message. I am new to SQL, need some help.
Your SELECT is assigning a alias of SortMonth to the following DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) but you cannot use an alias in GROUP BY unless it was named in a subquery.
You will need to change the code to:
GROUP BY dbo.bARCM.CustGroup,
dbo.bARCM.Customer,
dbo.JCJMPM.udJobType,
dbo.bJBID.Factor,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) -- use the DATEADD code here not the alias

Linq - SQL query conversion

Can some one help me to convert this query to linq? I dont know how to use union and then sum in linq.
SELECT Patientname, SUM(A)AS Trec
FROM (SELECT Pm.Patientname, COUNT(*)AS A
FROM Facilitycheckinholdorder Fcho
INNER JOIN Medordertype Mot ON Fcho.Orderseq = Mot.Orderseq
JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
AND Fcho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
AND Mot.Facilityid = 139
GROUP BY Pm.Patientname
UNION ALL
SELECT Pm.Patientname, COUNT(*)AS A
FROM Rxqdeliveryholdorder Rdho
INNER JOIN Medordertype Mot ON Rdho.Orderseq = Mot.Orderseq
JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
AND Rdho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
AND Mot.Facilityid = 139
GROUP BY Pm.Patientname
) AS Trec
GROUP BY Patientname;
In fact this is not a real question, but I've tried my best...
var lst1 =
from fhco in facilitycheckinholdorder
join mot in meordertype on mot.orderseq equals fhco.orderseq
join pm in patientmaster on mot.patientseq equals pm.patientseq
where
fcho.FilledDate >= new DateTime(2011, 9, 1)
fcho.FilledDate <= new DateTime(2012, 10, 16)
select pm;
var lst2 =
from rdho in rxqdeliveryholdorder
join mot in meordertype on rdho.orderseq equals mot.orderseq
join pm in patientmaster on moit.patientseq equals pm.patientseq
where
fcho.FilledDate >= new DateTime(2011, 9, 1)
fcho.FilledDate <= new DateTime(2012, 10, 16)
select pm;
var res =
from item in lst1.Union(lst2)
select new { Name = item.patientname, Count = lst1.Count() + lst2.Count() };
Key points:
Your outermost query seems redundant.
Your names are not common C# style, and it makes them difficult to read.
UNION ALL corresponds to Concat, not Union.
Getting the Concat out of the way early lets you streamline the query. You could do this in the SQL.
.
var result =
from order in Queryable.Concat(
FacilityCheckInHoldOrder.Select(o => new { o.OrderSeq, o.FilledDate }),
RxqDeliveryHoldOrder .Select(o => new { o.OrderSeq, o.FilledDate }))
where order.FilledDate >= new DateTime(2011, 9, 1) &&
order.FilledDate < new DateTime(2012, 10, 16)
join type in MedOrderType.Where(t => t.FacilityID == 139)
on order.OrderSeq equals type.OrderSeq
join patient in PatientMaster
on type.PatientSeq equals patient.PatientSeq
group by patient.PatientName into grp
select new
{
PatientName = grp.Key,
Trec = grp.Count()
};

Query Parameters not working

Working on a query here in SSRS and when I use the #Date parameters it doesnt seem to work, I dont get an error but it doesnt return values. When I take the =#Date value out of it, seems to run fine. Any Ideas on how I can correct this? I want to be able to input a name and date.
Thanks
SELECT TOP (100) PERCENT A.__$start_lsn, A.__$seqval, A.__$operation, A.__$update_mask, A.t_Refcntd, A.t_Refcntu, A.t_cbrn, A.t_ccon, A.t_ccor, A.t_ccrs, A.t_ccty, A.t_ccur,
A.t_cdec, A.t_cfrw, A.t_cfsg, A.t_clan, A.t_comp, A.t_corg, A.t_cotp, A.t_cpay, A.t_cplp, A.t_creg, A.t_ctrj, A.t_cvyn, A.t_cwar, A.t_ddat, A.t_ddtc, A.t_egen, A.t_odat,
A.t_odis, A.t_orno, A.t_prno, A.t_pspr, A.t_pstx, A.t_ragr, A.t_ratf, A.t_ratp, A.t_refa, A.t_refb, A.t_suno, A.t_txta, A.t_txtb, A.tran_begin_time, A.tran_end_time,
B.event_time, B.event_time_local, B.sequence_number, B.action_id, B.succeeded, B.permission_bitmask, B.is_column_permission, B.session_id,
B.server_principal_id, B.database_principal_id, B.target_server_principal_id, B.target_database_principal_id, B.object_id, B.class_type,
B.session_server_principal_name, B.server_principal_name, B.server_principal_sid, B.database_principal_name, B.target_server_principal_name,
B.target_server_principal_sid, B.target_database_principal_name, B.server_instance_name, B.database_name, B.schema_name, B.object_name, B.statement,
B.additional_information, B.file_name, B.audit_file_offset, CONVERT(VARCHAR(10), A.tran_end_time, 110) AS GenericDate
FROM dbo.ttdpur040101_CT AS A INNER JOIN
dbo.ttdpur040101_Audit AS B ON NOT (A.tran_begin_time > B.event_time_local OR
A.tran_end_time < B.event_time_local) AND (A.__$operation = 2 AND B.action_id = 'IN' OR
(A.__$operation = 3 OR
A.__$operation = 4) AND B.action_id = 'UP' OR
A.__$operation = 1 AND B.action_id = 'DL') AND B.class_type = 'U'
WHERE (B.server_principal_name = #Name) and (CONVERT(VARCHAR(10), A.tran_end_time, 110) = #Date)
ORDER BY B.event_time_local
If you are going to compare them, the formats need to match. Change your WHERE to:
WHERE (B.server_principal_name = #Name)
and (CONVERT(VARCHAR(10), A.tran_end_time, 110) = (CONVERT(VARCHAR(10),#Date, 110))