Conversion of Oracle query to teradata query - oracle

How to convert this below query to equivalent teradate query. I tried but results varies a lot.
select il.domainN as listname, il.SourceID, ns.sourcename, cbo.customerid, cu.username, hv.domainN as HTname
, nvl((select 1
from mydb.customerPP cpp
where cbo.customerid = cpp.customerid
and NOT EXISTS (select 1 from mydb.customerPI cpt where cpp.customerid = cpt.customerid)
and trunc(cpp.startdate) <= sysdate
group by cpp.customerid),0) as BBID
from mydb.customerBO cbo
join mydb.customers cu on cbo.customerid = cu.customerid
join mydb.inv il on cbo.domainN = il.domainN
join mydb.Sources ns on il.SourceID = ns.SourceID
left join mydb2.HT hv on (il.domainN = hv.domainN
and hv.sDate+1 >= il.dDate
and il.dDate+1 >= hv.sDate)
where cbo.customerBOID = 1
and cu.statusid = 1
and il.sourceTID = 2
and il.joinbydate >= cbo.cDate
and trunc(il.dDate) = trunc(sysdate)
Thanks.

Use coalesce instead nvl and compare explain statements to see whether you have left outer joins converted to inner joins for nvl on Teradata.

Related

Pagination in Oracle 11g

I am trying to achieve pagination in Oracle 11g with following query:
SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY
general_compliance_Details.ID) R,
general_compliance.,
get_entity.,
general_compliance_Details.,
GSTN_STATE_JURIS_DETAILS.,
etm_gl_district.*
FROM general_compliance_details
LEFT JOIN get_Entity
ON get_entity.gstin = general_compliance_details.gstin
LEFT JOIN general_compliance
ON general_compliance.id =
general_compliance_details.general_compliance_id
LEFT JOIN get_entity_status
ON get_entity_status.gstin =
general_compliance_details.gstin
LEFT JOIN GSTN_STATE_JURIS_DETAILS
ON get_Entity.PPBZDTLS_STJD = GSTN_STATE_JURIS_DETAILS.code
LEFT JOIN etm_gl_district
ON GSTN_STATE_JURIS_DETAILS.dist_cd =
etm_gl_district.district_code
WHERE general_compliance_details.general_compliance_id = '2'
AND general_compliance_details.status = '0'
AND general_compliance.form_type = '1') WHERE R >= 100 AND R <= 200
But it is giving error Exception111ORA-00918: column ambiguously defined but my all columns are defined using table.column_name.
Please help.

Linq how to join tables with a where clause on each table and a count

The following LINQ query isn't allowed when I use multiple where elements - it stops liking the 'into':
var query =
from ph in _db.PlayHits
join ua in _db.UserAgents on ph.UserAgentId equals ua.UserAgentId
where (ph.VideoId == 1 && ua.AgentString.Contains("test"))
into hits
select new
{
ResultCount = hits.Count()
};
Any idea why or how I amend this?
The equivilent sql I want is:
select count(*) as ResultCount
from Playhits ph
join UserAgents ua on ph.UserAgentId = ua.UserAgentId
where ph.VideoId = 1
and ua.AgentString like '%test%'
To my understanding, counting of the results can be done using the extension methond Count as follows.
var query =
from ph in _db.PlayHits
join ua in _db.UserAgents on ph.UserAgentId equals ua.UserAgentId
where (ph.VideoId == 1 && ua.AgentString.Contains("test"));
int Result = query.Count();

Not a single group function

This query is not showing anything at all. I want results only those ORDERNO which match in the sub-query.
SELECT SUM(STY.NQTY * STY.NUNITPRICE
/ 1000) AS ORDERAMOUNT,
(SELECT SUM(TBLORDERSTYLE.NQTY * TBLORDERSTYLE.NUNITPRICE/1000) FROM TBLORDER
INNER JOIN TBLORDERSTYLE
ON TBLORDER.CORDERNO = TBLORDERSTYLE.CORDERNO
WHERE TBLORDER.CLCNO LIKE '%MR#%' AND
TBLORDER.CORDERNO = ORD.CORDERNO
GROUP BY ORD.CORDERNO) AS K
FROM TBLORDER ORD INNER JOIN
TBLORDERSTYLE STY ON
ORD.CORDERNO = STY.CORDERNO
where (ORD.NPOSTFLAG = '1') AND
(ORD.NCANCEL = '0') AND
ORD.NPAYMODE = '1' AND
(ORD.DPICONFIRMDATE BETWEEN
TO_DATE('01-JUL-2012')AND TO_DATE('31-JUL-2012'))
The error message is quite clear: you don't have a GROUP BY clause. Whenever we mix aggregating functions like SUM() with non-aggregated columns we need to include the static columns in a GROUP BY clause.
In you code the static column is K. Yes it is derived from an aggregation but that is in a sub-query, and so it counts as a non-aggregating column.
The way to solve thi sis to move the sub-query into a common table expression (what Oracle calls subquery factoring), which makes it easy to reference the column more than once. The use of the WITH cluase is covered in the Oracle SQL Reference. Find out more.
with cte as (SELECT ORD.CORDERNO
, SUM(TBLORDERSTYLE.NQTY * TBLORDERSTYLE.NUNITPRICE/1000) as k
FROM TBLORDER
INNER JOIN TBLORDERSTYLE
ON TBLORDER.CORDERNO = TBLORDERSTYLE.CORDERNO
WHERE TBLORDER.CLCNO LIKE '%MR#%' AND
TBLORDER.CORDERNO = ORD.CORDERNO
GROUP BY ORD.CORDERNO)
SELECT SUM(STY.NQTY * STY.NUNITPRICE
/ 1000) AS ORDERAMOUNT,
cte.K
FROM cte inner join
TBLORDER ORD on ord.orderno = k.orderno
INNER JOIN TBLORDERSTYLE STY ON STY.CORDERNO = k.orderno
where (ORD.NPOSTFLAG = '1') AND
(ORD.NCANCEL = '0') AND
ORD.NPAYMODE = '1' AND
(ORD.DPICONFIRMDATE BETWEEN TO_DATE('01-JUL-2012')AND TO_DATE('31-JUL-2012'))
group by cte.k
It's highly likely that this is not the actual logic you need. You did not include any explanation of the business rules you're trying to implement so I have just made a guess at how to join the sub-query with the main query.

Entity to Linq Left Join + Grouping + Sum

SQL Query (Execution plan cost = 0.0127553)
SELECT
SUM(DATEDIFF(second, DTActivate, DTDeActivate)) AS Seconds,
AA.ID AS AAID,
AA.WorkStation
FROM
DbLogItems I
INNER JOIN DbApplicationArguments AA ON
AA.Id = I.ApplicationArgument_ID
GROUP BY
AA.ID,
AA.WorkStation
C#
var q = from items in db.LogItem
join aa in db.ApplicationArguments on
items.ApplicationArgument.ID equals aa.ID
into aaGroup
from aaJoin in aaGroup.DefaultIfEmpty()
group items by new
{
aaJoin.ID,
aaJoin.WorkStation
} into grouping
select new
{
Seconds = grouping.Sum(x => SqlFunctions.DateDiff("second", x.DTActivate, x.DTDeActivate)),
grouping.Key.ID,
grouping.Key.WorkStation
};
Result SQL very big (Execution plan cost = 0.0199849)
SELECT
1 AS [C1],
[GroupBy1].[A1] AS [C2],
[GroupBy1].[K1] AS [ID],
[GroupBy1].[K2] AS [WorkStation]
FROM
(
SELECT
[Join1].[K1] AS [K1],
[Join1].[K2] AS [K2],
SUM([Join1].[A1]) AS [A1]
FROM
(
SELECT
[Extent2].[ID] AS [K1],
[Extent2].[WorkStation] AS [K2],
DATEDIFF(second, [Extent1].[DTActivate], [Extent1].[DTDeActivate]) AS [A1]
FROM
[dbo].[DbLogItems] AS [Extent1]
LEFT OUTER JOIN [dbo].[DbApplicationArguments] AS [Extent2]
ON [Extent1].[ApplicationArgument_ID] = [Extent2].[ID]
) AS [Join1]
GROUP BY
[K1],
[K2]
) AS [GroupBy1]
help please write correct linq code.
My SQL Execution plan cost = 0.0127553.
Linq SQL Execution plan cost = 0.0199849.
DIFF = 0,0072296 on 21+10 records
LINQ to SQL queries are what they are. Sometimes you can't emit better SQL. However, you can write plain old T-SQL and call it in a number ways: a table-valued UDF perhaps that your customized DataContext exposes as an IQueryable.

LINQ count query returns a 1 instead of a 0

I have the following view:-
CREATE VIEW tbl_adjudicator_result_view
AS
SELECT a.adjudicator_id, sar.section_adjudicator_role_id, s.section_id, sdr.section_dance_role_id, d.dance_id, c.contact_id,
ro.round_id, r.result_id, c.title, c.first_name, c.last_name, d.name, r.value, ro.type
FROM tbl_adjudicator a
INNER JOIN tbl_section_adjudicator_role sar on sar.section_adjudicator_role2adjudicator = a.adjudicator_id
INNER JOIN tbl_section s on sar.section_adjudicator_role2section = s.section_id
INNER JOIN tbl_section_dance_role sdr on sdr.section_dance_role2section = s.section_id
INNER JOIN tbl_dance d on sdr.section_dance_role2dance = d.dance_id
INNER JOIN tbl_contact c on a.adjudicator2contact = c.contact_id
INNER JOIN tbl_round ro on ro.round2section = s.section_id
LEFT OUTER JOIN tbl_result r on r.result2adjudicator = a.adjudicator_id AND r.result2dance = d.dance_id
When I run the following query directly against the db I get 0 in the count column where there is no result
select adjudicator_id, first_name, COUNT(result_id)
from tbl_adjudicator_result_view arv
where arv.round_id = 16
group by adjudicator_id, first_name
However when I use LINQ query I always get 1 in the Count Column
var query = from arv in db.AdjudicatorResultViews
where arv.round_id == id
group arv by new { arv.adjudicator_id, arv.first_name} into grp
select new AdjudicatorResultViewGroupedByDance
{
AdjudicatorId = grp.Key.adjudicator_id,
FirstName = grp.Key.first_name,
Count = grp.Select(p => p.result_id).Distinct().Count()
};
What do I need to change in the View / Linq query.
You're not doing the same thing in the LINQ query as in the SQL. COUNT(result_id) does not count distinct values of result_id - it counts non-null values.
Try this instead:
Count = grp.Select(p => p.result_id).Where(x => x != null).Count()
The point is: you're grouping your data in the LINQ query - and you'll always get at least one group.
That group's Count may be 0 - but the count of groups will be 1.

Resources