Query Parameters not working - sql

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))

Related

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

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();
}

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)

How to query Oracle grouping?

I have such a problem and I don't know how to solve it, can you help me? t
The query returns a result that is shown on the photo and I want to get it to be shown in one line instead of many based on type of age.
https://imgur.com/a/OA6CBpa
with x as (
select ai.invoice_id, ai.invoice_num, ai.invoice_amount, ai.amount_paid,
trial.entity_id, trial.acctd_amount, trial.entered_amount, trial.gl_date,
aps.amount_remaining, aps.gross_amount, aps.due_date, aps.payment_status_flag,
trial.gl_date - aps.due_date dni_opoznienia
from ap_invoices_all ai,
xla.xla_transaction_entities xte,
(
select nvl (tr.applied_to_entity_id, tr.source_entity_id) entity_id,
tr.source_application_id application_id,
sum (nvl (tr.acctd_unrounded_cr, 0)) - sum (nvl (tr.acctd_unrounded_dr, 0)) acctd_amount,
sum (nvl (tr.entered_unrounded_cr, 0)) - sum (nvl (tr.entered_unrounded_dr, 0)) entered_amount,
max(tr.gl_date) gl_date
from xla.xla_trial_balances tr
where 1=1
and tr.definition_code = 'AP_200_1001'
and tr.source_application_id = 200
and tr.gl_date <= fnd_date.canonical_to_date('2019-12-13') -- Data KG
group by nvl (tr.applied_to_entity_id, tr.source_entity_id),
tr.source_application_id
) trial,
ap_payment_schedules_all aps
where 1=1
and ai.invoice_id = 3568325
and nvl(xte.source_id_int_1, -99) = ai.invoice_id
and xte.ledger_id = 1001
and xte.entity_code = 'AP_INVOICES'
and xte.entity_id = trial.entity_id
and xte.application_id = trial.application_id
and ai.invoice_id = aps.invoice_id
)
select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id
Based on your comment I guess you need the below
select * from (select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id)
where przedzial > 0;

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