Not a single group function - oracle

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.

Related

Error when running a sub query in Oracle SQL

I am trying to join three tables using a sub query.
The result of the first left outer join is to be used with another table to get a composite view with all attributes.
I am getting an error where the compile says, Unknown Command for the table in the second join clause.
When I create two independent views and then join then it works fine.
(select
l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
from tgt_workday.learning l
left outer join ods_hrmaster.employee e
on l.EMPLOYEE_ID = e.employee#) t1
left outer join ( select
per_ids_id,
per_id,
id_pureid from
ods_pure.person_ids
) t2 on t1.wk_worker_id = t2.value where t2.type = 'Employee ID';
You can write it in a simple way. There is no need to make sub-queries as:
SELECT L.ENROLLED_CONTENT,
L.LEARNING_ENROLLMENT_LEARNER,
L.EMPLOYEE_ID,
L.JOB_FAMILY_GROUP,
L.EMPLOYEE_TYPE,
L.JOB_FAMILY,
L.LEARNING_ENROLLMENT,
L.COMPLETION_STATUS,
L.COMPLETION_DATE,
L.EXPIRATION_DATE,
L.CF_LRV_LEARNING_CONTENT_NUMBER,
L.LEARNING_CONTENT_DETAIL,
L.LEARNING_CONTENT_TYPE,
L.LESSON_TYPE,
E.ID# "WK_WORKER_ID"
FROM TGT_WORKDAY.LEARNING L
LEFT OUTER JOIN ODS_HRMASTER.EMPLOYEE E
ON L.EMPLOYEE_ID = E.EMPLOYEE#
LEFT OUTER JOIN ODS_PURE.PERSON_IDS T2
ON E.ID# = T2.VALUE
AND T2.TYPE = 'Employee ID';
Once you use the outer joined table's column in WHERE clause, It will result in the same result as inner join(there is another ways to use it in WHERE clause though). So it is better to avoid using outer joined table's column in the WHERE clause.
Try as
SELECT *
FROM ( (SELECT l.ENROLLED_CONTENT,
l.LEARNING_ENROLLMENT_LEARNER,
l.EMPLOYEE_ID,
l.JOB_FAMILY_GROUP,
l.EMPLOYEE_TYPE,
l.JOB_FAMILY,
l.LEARNING_ENROLLMENT,
l.COMPLETION_STATUS,
l.COMPLETION_DATE,
l.EXPIRATION_DATE,
l.CF_LRV_LEARNING_CONTENT_NUMBER,
l.LEARNING_CONTENT_DETAIL,
l.LEARNING_CONTENT_TYPE,
l.LESSON_TYPE,
e.id# "WK_WORKER_ID"
FROM tgt_workday.learning l
LEFT OUTER JOIN ods_hrmaster.employee e
ON l.EMPLOYEE_ID = e.employee) t1
LEFT OUTER JOIN
(SELECT per_ids_id, per_id, id_pureid FROM ods_pure.person_ids) t2
ON t1.wk_worker_id = t2.VAL AND t2.TYPE = 'Employee ID')

How to group and uniqe each row with ORACLE?

Anyone please help me, how to uniqe each row
SELECT T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN AS SORT, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') AS DATE_FINISHED
FROM TB_WORK T1
JOIN TB_INSPECTION T2 ON T1.ID_INSPECTION=T2.ID_INSPECTION
JOIN TB_JOBSHEET T3 ON T2.ID_JS = T3.ID_JS
JOIN TB_METHODE_REPAIR T4 ON T1.ID_METHODE_REPAIR=T4.ID_METHODE_REPAIR
JOIN TB_DIVISI T5 ON T1.ID_DIVISI=T5.ID_DIVISI
where t3.id_js=142414
GROUP BY T3.ID_JS,T5.DIVISI_AREA,T4.NAME_METHODE_REPAIR, T1.URUTAN,TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY')
ORDER BY T3.ID_JS, TO_DATE(T1.TIME_FINISHED_WORK,'DD/MM/YYYY') desc
[Result] (http://prntscr.com/sfpuuq)
Those rows are unique, so I'll guess: there are two rows with sort = 3 and you'd want to have only one. If that's so, regarding the fact that date value is the only thing that makes difference, a simple way is to use aggregate function (such as min or max). I used max, but you can change it if you want:
SELECT t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan AS sort,
MAX (TO_DATE (t1.time_finished_work, 'DD/MM/YYYY')) AS date_finished
FROM tb_work t1
JOIN tb_inspection t2 ON t1.id_inspection = t2.id_inspection
JOIN tb_jobsheet t3 ON t2.id_js = t3.id_js
JOIN tb_methode_repair t4
ON t1.id_methode_repair = t4.id_methode_repair
JOIN tb_divisi t5 ON t1.id_divisi = t5.id_divisi
WHERE t3.id_js = 142414
GROUP BY t3.id_js,
t5.divisi_area,
t4.name_methode_repair,
t1.urutan
ORDER BY t3.id_js, date_finished DESC
You'd remove date column from GROUP BY; besides, if you wanted to select distinct rows, why didn't you apply select distinct instead of using group by? Result is the same, but - group by is generally used with aggregates, not to return distinct result.

speed up a query with multiple inner joins in ms access

as tittle says i need to improve this query that i have made in ms access, the tables are from a linked DB. i can't index them. i need help to understand where it is taking so long... is there any function like
EXPLAIN to access? do i need to put more columns in some sort of group by? what i need to do to improve the speed of this (the group by of first select has 4M rows but after grouped only has 321k and it takes 20min to run when laptop doesn't crashes)
SELECT a.SEQ_NO,
b.SKU,
b.maxdate,
(a.BASE_COST/a.EXCHANGE) AS BASE_COST,
(a.NET_COST/a.EXCHANGE) AS NET_COST,
(a.NET_NET_COST/a.EXCHANGE) AS EXCHAGED_NET_NET_COST,
a.NET_NET_COST,
(a.DEAD_NET_NET_COST/a.EXCHANGE) AS DEAD_NET_NET_COST,
(a.LANDED_COST/a.EXCHANGE) AS LANDED_COST,
(a.POSEIMA/a.EXCHANGE) AS POSEIMA,
(a.TOTAL_BONUS/a.EXCHANGE) AS TOTAL_BONUS,
(a.IEC/a.EXCHANGE) AS IEC,
(a.IEC_BONUS/a.EXCHANGE) AS IEC_BONUS,
(a.ECO_INVOICE_FORN/a.EXCHANGE) AS ECO_INVOICE_FORN_SYSTEM,
(a.ECO_INVOICE/a.EXCHANGE) AS ECO_INVOICE_SYSTEM,
(a.ECO_MERCHANDISE/a.EXCHANGE) AS ECO_MERCHANDISE_SYSTEM,
c.SUPPLIER,
c.SUP_NAME,
d.UPC,
d.PRIMARY_UPC_IND,
f.BRAND,
g.DEPT,
g.DESC_UP,
g.CLASS,
g.SUBCLASS,
h.AV_COST,
h.UNIT_RETAIL AS Last_of_unit_retail,
h.STATUS, i.[UNIT VALUE],
i.[INITIAL DATE],
i.[END DATE] INTO PRICELIST
FROM (((((((RMS_MC_NB_PRICELIST_COST AS a INNER JOIN (SELECT MAX(SEQ_NO) AS
ID, SKU, MAX(ACTIVE_DATE) AS maxdate FROM RMS_MC_NB_PRICELIST_COST GROUP BY
SKU) AS b ON a.SEQ_NO = b.ID)
INNER JOIN RMS_MC_SUPS AS c ON a.SUPPLIER = c.SUPPLIER)
INNER JOIN RMS_MC_UPC_EAN AS d ON b.SKU = d.SKU)
INNER JOIN RMS_MC_WIN_ATTRIBUTES AS e ON b.SKU = e.SKU)
INNER JOIN RMS_MC_NB_BRAND AS f ON e.NB_BRAND_NO = f.BRAND_NO)
INNER JOIN RMS_MC_DESC_LOOK AS g ON b.SKU = g.SKU)
INNER JOIN RMS_MC_WIN_STORE AS h ON b.SKU = h.SKU)
LEFT JOIN MAPA_APOIOS_SISO AS i ON b.SKU = i.[# ARTICLE];

Conversion of Oracle query to teradata query

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.

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.

Resources