Group By in Oracle is not working - sql

I am trying to use group by in with all fields in select too.
My query:
SELECT count(*) as totalRow,
trim(A.P_CODE) P_CODE,
INITCAP(trim(A.ALTERNATE_TEXT)) ALTERNATE_TEXT,
trim(A.LINKED_SKU_CODE) as LINKED_SKU_CODE,
UCWORD(B.BRAND_NAME) BRAND_NAME,
LOWER(TRIM(A.UNIT)) UNIT,
INITCAP(TRIM(A.PK_SIZE)) PK_SIZE,
trim(A.DF_SALE_RATE) DF_SALE_RATE,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) CAT_TYPE,
NVL(MAX_QTY,25) MAX_QTY,
A.BAL_QTY
FROM GET_PRODUCT A,
WEB_BRANDS B
WHERE A.P_CODE in (".$p_codes.")
AND A.BR_CODE = '".BR_CODE."'
AND A.BRAND_CODE = B.BRAND_CODE
GROUP BY A.P_CODE,
A.ALTERNATE_TEXT,
A.LINKED_SKU_CODE,
B.BRAND_NAME,
A.UNIT,
A.PK_SIZE,
A.DF_SALE_RATE,
A.MRP,
A.CAT_TYPE,
MAX_QTY,
A.BAL_QTY
ORDER BY CAT_TYPE,
P_NAME
Error:
ORA-00979: not a GROUP BY expression in C:
Please point me in right direction or could point me where I have done mistakes.

You should write in GROUP BY section at least all columns from SELECT section ( without aggregatable columns). I think you query should look like
SELECT count(*) as totalRow,
trim(A.P_CODE) P_CODE,
INITCAP(trim(A.ALTERNATE_TEXT)) ALTERNATE_TEXT,
trim(A.LINKED_SKU_CODE) as LINKED_SKU_CODE,
UCWORD(B.BRAND_NAME) BRAND_NAME,
LOWER(TRIM(A.UNIT)) UNIT,
INITCAP(TRIM(A.PK_SIZE)) PK_SIZE,
trim(A.DF_SALE_RATE) DF_SALE_RATE,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) CAT_TYPE,
NVL(MAX_QTY,25) MAX_QTY,
A.BAL_QTY
FROM GET_PRODUCT A,
WEB_BRANDS B
WHERE A.P_CODE in (".$p_codes.")
AND A.BR_CODE = '".BR_CODE."'
AND A.BRAND_CODE = B.BRAND_CODE
GROUP BY
trim(A.P_CODE) ,
INITCAP(trim(A.ALTERNATE_TEXT)),
trim(A.LINKED_SKU_CODE) ,
UCWORD(B.BRAND_NAME) ,
LOWER(TRIM(A.UNIT)),
INITCAP(TRIM(A.PK_SIZE)),
trim(A.DF_SALE_RATE) ,
trim(A.MRP) MRP,
INITCAP(trim(A.CAT_TYPE)) ,
NVL(MAX_QTY,25),
A.BAL_QTY

Related

Syntax error in FROM clause - MS ACCESS

I am working with a tool that would extract some data from an Access Database. So basically, i am working on a query to get this data.
Below is the code i am currently working on.
I am getting an error: Syntax error in FROM clause
I can't seem to find where the query is going wrong. I would appreciate any help! Thank youu.
EDIT: putting my actual query
SELECT table_freq.*, IIF(table_freq.txn_ctr > (table_ave_freq.ave_freq * 3), "T", "F") as suspicious_flag
FROM
(
SELECT tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date, Count(tbl_TransactionHistory.client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY tbl_TransactionHistory.client_num, tbl_TransactionHistory.client_name,
tbl_TransactionHistory.transaction_date
) AS table_freq
INNER JOIN
(
SELECT table_total_freq.client_num, total_txn_ctr as TotalTransactionFrequency, total_no_days as TotalTransactionDays,
(table_total_freq.total_txn_ctr)/(table_no_of_days.total_no_days) AS ave_freq
FROM
(
(
SELECT client_num, SUM(txn_ctr) AS total_txn_ctr
FROM
(
SELECT client_num, client_name, transaction_date, COUNT(client_num) AS txn_ctr
FROM tbl_TransactionHistory
GROUP BY client_num, client_name, transaction_date
) AS tabFreq
GROUP BY client_num
) AS table_total_freq
INNER JOIN
(
SELECT client_num, COUNT(txn_date) as total_no_days
FROM
(
SELECT DISTINCT(transaction_date) as txn_date, client_num
FROM tbl_TransactionHistory
ORDER BY client_num
) AS table1
GROUP BY client_num
) AS table_no_of_days
ON table_total_freq.client_num = table_no_of_days.client_num
)
) AS table_ave_freq
ON table_freq.client_num = table_ave_freq.client_num

joining multiple subqueries Oracle

I have 2 working sets of subqueries. I'm trying to make them into one query with these fields:
select tos.source_system_order_id , tos.TOS_Date, tos.TOS_Final_Charge_Amt_Sum , oes.OES_Final_Charge_Amt_Sum
first query:
SELECT tos1.source_system_order_id,
tos1.tos_date,
SUM(tos1.tos_final_charge_amt_sum)
FROM (SELECT source_system_order_id,
source_system_cd,
To_char(billing_month_dt, 'YYYYMM') AS TOS_Date,
tos_final_charge_amt_sum
FROM tl_ov_stage
ORDER BY source_system_order_id) TOS1
GROUP BY tos1.source_system_order_id,
tos1.tos_date
2ndquery
SELECT OES1.source_system_order_id,
oes1.oes_date,
SUM(oes1.oes_final_charge_amt_sum) AS OES_Final_Charge_Amt_Sum
FROM (SELECT To_char("date", 'YYYYMM') AS OES_Date,
To_char("service order id") AS SOURCE_SYSTEM_ORDER_ID,
oes_final_charge_amt_sum
FROM v_ord_valuation_detail#prodr_link) OES1
GROUP BY OES1.source_system_order_id,
oes1.oes_date,
oes1.order_status
Try using CTE to combine the two select queries. I find CTE is more readable in such cases
with tos
as
(
SELECT tos1.source_system_order_id,
tos1.tos_date,
SUM(tos1.tos_final_charge_amt_sum)
FROM (SELECT source_system_order_id,
source_system_cd,
To_char(billing_month_dt, 'YYYYMM') AS TOS_Date,
tos_final_charge_amt_sum
FROM tl_ov_stage
ORDER BY source_system_order_id) TOS1
GROUP BY tos1.source_system_order_id,
tos1.tos_date
),
OES as
(
SELECT OES1.source_system_order_id,
oes1.oes_date,
SUM(oes1.oes_final_charge_amt_sum) AS OES_Final_Charge_Amt_Sum
FROM (SELECT To_char("date", 'YYYYMM') AS OES_Date,
To_char("service order id") AS SOURCE_SYSTEM_ORDER_ID,
oes_final_charge_amt_sum
FROM v_ord_valuation_detail#prodr_link) OES1
GROUP BY OES1.source_system_order_id,
oes1.oes_date,
oes1.order_status
)
select tos.source_system_order_id,
tos.TOS_Date,
tos.TOS_Final_Charge_Amt_Sum,
oes.OES_Final_Charge_Amt_Sum
from tos
inner join oes
on tos.source_system_order_id = oes.source_system_order_id
AND tos.tos_date = oes.oes_date -- Remove if this is not needed

Unsupported column aliasing oracle

I have problem, because I get error unsupported column aliasing and I dont't know where is the cause. Does anyone know the answer and can help? Thanks in advance :) Code:
with
ContractByRole (intermediary_nr, beneficiary_role, contract_nr) as (
select intermediary_nr, beneficiary_role, max(contract_nr) contract_nr
from boscs.atcs_commission_beneficiary
where beneficiary_role in( 'LEAD', 'SUP_FOR_LEAD', 'COAGENT' )
and intermediary_nr is not null
group by intermediary_nr, beneficiary_role
)
AllRoles(contract_nr) as (
select contract_nr
from ContractByRole
group by contract_nr
having count(*) = 3
)
select cbr.*
from ContractByRole cbr
join AllRoles ar
on ar.contract_nr = cbr.contract_nr;
Error show in this place:
ContractByRole(intermediary_nr, beneficiary_role, contract_nr) as (
You have missed a comma to seperate the two subqueries in your WITH clause -
Edit Try this : (Not sure if you really need the recursive subquery)
WITH contractbyrole
AS
(
SELECT intermediary_nr,
beneficiary_role,
Max(contract_nr) contract_nr
FROM boscs.atcs_commission_beneficiary
WHERE beneficiary_role IN( 'LEAD',
'SUP_FOR_LEAD',
'COAGENT' )
AND intermediary_nr IS NOT NULL
GROUP BY intermediary_nr,
beneficiary_role
)
, --> here is the missing comma
allroles
AS
(
SELECT contract_nr
FROM contractbyrole
GROUP BY contract_nr
HAVING count(*) = 3
)
SELECT cbr.*
FROM contractbyrole cbr
JOIN allroles ar
ON ar.contract_nr = cbr.contract_nr;

SQL group by issue when i try to get some info

I can not figure it out:
I have a table called ImportantaRecords with fields like market, zip5, MHI, MHV, TheTable and I want to group all the records by zip5 that have TheTable = 'mg'… I tried this :
select a.Market,a.zip5,count(a.zip5),a.MHI,a.MHV,a.TheTable from
(select * from ImportantaRecords where TheTable = 'mg') a
group by a.Zip5
but it gives me the classic error with not an aggrefate function
and then I tried this:
select Market,zip5,count(zip5),MHI,MHV,TheTable from ImportantaRecords where TheTable = 'mg'
group by Zip5
and the same thing…
any help ?
You did not state what database you are using but if you are getting an error about columns not being in an aggregate function, then you might need to add the columns not in an aggregate function to the GROUP BY:
select Market,
zip5,
count(zip5),
MHI,
MHV,
TheTable
from ImportantaRecords
where TheTable = 'mg'
group by Market, Zip5, MHI, MHV, TheTable;
If grouping by the additional columns alters the result that you are expecting, then you could use a subquery to get the result:
select i1.Market,
i1.zip5,
i2.Total,
i1.MHI,
i1.MHV,
i1.TheTable
from ImportantaRecords i1
inner join
(
select zip5, count(*) Total
from ImportantaRecords
where TheTable = 'mg'
group by zip5
) i2
on i1.zip5 = i2.zip5
where i1.TheTable = 'mg'

SQL Server GROUP BY troubles!

I'm getting a frustrating error in one of my SQL Server 2008 queries. It parses fine, but crashes when I try to execute. The error I get is the following:
Msg 8120, Level 16, State 1, Line 4
Column
'customertraffic_return.company' is
invalid in the select list because it
is not contained in either an
aggregate function or the GROUP BY
clause.
SELECT *
FROM (SELECT ctr.sp_id AS spid,
Substring(ctr.company, 1, 20) AS company,
cci.email_address AS tech_email,
CASE
WHEN rating IS NULL THEN 'unknown'
ELSE rating
END AS rating
FROM customer_contactinfo cci
INNER JOIN customertraffic_return ctr
ON ctr.sp_id = cci.sp_id
WHERE cci.email_address <> ''
AND cci.email_address NOT LIKE '%hotmail%'
AND cci.email_address IS NOT NULL
AND ( region LIKE 'Europe%'
OR region LIKE 'Asia%' )
AND SERVICE IN ( '1', '2' )
AND ( rating IN ( 'Premiere', 'Standard', 'unknown' )
OR rating IS NULL )
AND msgcount >= 5000
GROUP BY ctr.sp_id,
cci.email_address) AS a
WHERE spid NOT IN (SELECT spid
FROM customer_exclude)
GROUP BY spid,
tech_email
Well, the error is pretty clear, no??
You're selecting those columns in your inner SELECT:
spid
company
tech_email
rating
and your grouping only by two of those (GROUP BY ctr.sp_id, cci.email_address).
Either you need group by all four of them (GROUP BY ctr.sp_id, cci.email_address, company, rating), or you need to apply an aggregate function (SUM, AVG, MIN, MAX) to the other two columns (company and rating).
Or maybe using a GROUP BY here is totally the wrong way to do - what is it you're really trying to do here??
The inner query:
SELECT ctr.sp_id AS spid,
Substring(ctr.company, 1, 20) AS company,
cci.email_address AS tech_email,
CASE
WHEN rating IS NULL THEN 'unknown'
ELSE rating
END AS rating
FROM customer_contactinfo cci
INNER JOIN customertraffic_return ctr
ON ctr.sp_id = cci.sp_id
WHERE cci.email_address <> ''
AND cci.email_address NOT LIKE '%hotmail%'
AND cci.email_address IS NOT NULL
AND ( region LIKE 'Europe%'
OR region LIKE 'Asia%' )
AND SERVICE IN ( '1', '2' )
AND ( rating IN ( 'Premiere', 'Standard', 'unknown' )
OR rating IS NULL )
AND msgcount >= 5000
GROUP BY ctr.sp_id,
cci.email_address
has 4 non-aggregate things in the select (sp_id, company, email_address, rating) and you only group on two of them, so it is throwing an error on the first one it sees
So you either need to not group by any of them or group by all of them
i suggest replacing the * with a fully specified column list.
you can either group by all selected columns or use the other columns (not in group by clause) in a aggregate function (like sum)
you cannot: select a,b,c from bla group by a,b
but you can: select a,b,sum(c) from bla groupy by a,b