I am generating records with sum aggregate function and subquery, but the alias is not work there in inner query.
my query is
select UPP.item_total,
(select sum(INN.item_value_afs) total_item_value_afs from
(select distinct INN.reg_no,INN.tpt_cuo_nam,INN.item_total,INN.item_value_afs
from sigtasad.customs_import_data INN where INN.reg_no=UPP.reg_no and INN.tpt_cuo_nam=UPP.tpt_cuo_nam)) total_item_value,
sum(UPP.code_tax_amount), UPP.cmp_nam from SIGTASAD.CUSTOMS_IMPORT_DATA UPP where
UPP.reg_no='38699' and UPP.company_tin='9003247336' group by
UPP.reg_no,UPP.tpt_cuo_nam,UPP.cmp_nam,UPP.item_total ;
this query generate this error :
ORA-00904: "UPP"."TPT_CUO_NAM": invalid identifier
I want like this result!!!
Your query has numerous errors and bad habits. For instance:
You qualify a column name with an undefined table alias.
You are aggregating by columns not in the select.
You are using sum() on a subquery that has sum().
Based on the picture that you show, you probably want something like this:
select upp.item_total,
sum(item_value_afs) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Or perhaps:
select upp.item_total,
sum(sum(item_value_afs)) over (partition by upp.cmp_nam, upp.item_total) as total_item_value,
sum(upp.code_tax_amount),
upp.cmp_nam
from SIGTASAD.CUSTOMS_IMPORT_DATA upp
where upp.reg_no = '38699' and upp.company_tin = '9003247336'
group by upp.cmp_nam, upp.item_total ;
Your innermost subquery
(select distinct nn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)
references a table that is not joined (upp). It also does not have an alias but that problem would come later. Please note, that there also seems to be a type nn.reg_no instead of inn.reg_no
The structure of the tables is not displayed here but fixing the problem would mean something along the lines of:
(select distinct inn.reg_no,inn.tpt_cuo_nam, inn.item_total, inn.item_value_afs
from sigtasad.customs_import_data inn, SIGTASAD.CUSTOMS_IMPORT_DATA upp
where inn.reg_no = upp.reg_no and inn.tpt_cuo_nam = upp.tpt_cuo_nam
)
Related
select EDR_Process_Time, MSISDN1, Total_payment/Total_data_MB from
(
select trunc(M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time) as EDR_Process_Time, M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN as MSISDN1, trunc(M_MOBILEMV.Q_RA_CDR_DETAILS_V.CDR_start_Date), M_MOBILEMV.Q_RA_CDR_DETAILS_V.served_msisdn, sum(M_MOBILEMV.Q_RA_CDR_DETAILS_V.charge_fee_1) as Charge_fee_CDR,
sum(M_MOBILEMV.Q_RA_EDR_DETAILS_V.charge_fee/100) as Total_Payment,
sum(M_MOBILEMV.Q_RA_EDR_DETAILS_V.amount/1048576) as Total_data_MB,
from (M_MOBILEMV.Q_RA_EDR_DETAILS_V
left join M_MOBILEMV.Q_RA_CDR_DETAILS_V on concat(trunc(M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time), M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN) = concat(trunc(M_MOBILEMV.Q_RA_CDR_DETAILS_V.CDR_start_date), M_MOBILEMV.Q_RA_CDR_DETAILS_V.served_MSISDN))
group by trunc(M_MOBILEMV.Q_RA_EDR_DETAILS_V.Process_time), M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN
group by trunc(M_MOBILEMV.Q_RA_CDR_DETAILS_V.CDR_start_Date), M_MOBILEMV.Q_RA_CDR_DETAILS_V.Served_MSISDN
)
Syntax of functions is wrong. Use trunc function as trunc(M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time). Usege of sum is wrong too. You also should change them to sum(M_MOBILEMV.Q_RA_CDR_DETAILS_V.charge_fee_1.
First, wrong syntax at M_MOBILEMV.Q_RA_EDR_DETAILS_V.sum((charge_fee)/100) and other similar expressions.
If you want sum of charge_fee/100 column of the M_MOBILEMV.Q_RA_EDR_DETAILS_V table (view) it must
be
sum(M_MOBILEMV.Q_RA_EDR_DETAILS_V.charge_fee/100)
Next, you can not use an alias in the same SELECT list where it was introduced. Total_Payment and Total_data_MB should only apprear in expressions at outer SELECT.
Assign aliases to the every expression of the inner select, an alias to the inner select itself and use list of qualified names and expresions using those names instead of * in the outer select.
Also, it looks like GROUP BY is missing.
select t.process_time, t.MSISDN, t.sum1,
t.Total_Payment,
t.Total_data_MB,
t.Total_Payment/t.Total_data_MB as cost_per_MB
from (
select
M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time,
M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN,
sum(M_MOBILEMV.Q_RA_CDR_DETAILS_V.charge_fee_1) as sum1,
(coalesce(sum(M_MOBILEMV.Q_RA_CDR_DETAILS_V.charge_fee_1),0) + coalesce(sum(M_MOBILEMV.Q_RA_EDR_DETAILS_V.charge_fee/100),0)) as Total_Payment,
sum(M_MOBILEMV.Q_RA_EDR_DETAILS_V.amount)/1048576 as Total_data_MB
-- Don't you mean
--GROUP BY M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time, M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN
from (M_MOBILEMV.Q_RA_EDR_DETAILS_V
left join M_MOBILEMV.Q_RA_CDR_DETAILS_V on concat(trunc(M_MOBILEMV.Q_RA_EDR_DETAILS_V.process_time), M_MOBILEMV.Q_RA_EDR_DETAILS_V.MSISDN) = concat(trunc(M_MOBILEMV.Q_RA_CDR_DETAILS_V.CDR_start_date), M_MOBILEMV.Q_RA_CDR_DETAILS_V.served_MSISDN)
) t
)
I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).
I have a requirement to create a Sales report and I have a sql query:
SELECT --top 1
t.branch_no as TBranchNo,
t.workstation_no as TWorkstation,
t.tender_ref_no as TSaleRefNo,
t.tender_line_no as TLineNo,
t.tender_code as TCode,
T.contribution as TContribution,
l.sale_line_no as SaleLineNo
FROM TENDER_LINES t
LEFT JOIN SALES_TX_LINES l
on t.branch_no = l.branch_no and t.workstation_no = l.workstation_no and t.tender_ref_no = l.sale_tx_no
where l.sale_tx_no = 2000293 OR l.sale_tx_no = 1005246 --OR sale_tx_no = 1005261
order by t.tender_ref_no asc,
l.sale_line_no desc
The results of the query look like the following:
The results I am trying to achieve is:
With only 1 line for transaction 2 either SaleLineNo 1 or 2, while still have=ing both lines for transaction 1 because the TCode is different.
Thanks
I am using SSQL2012.
Not exactly sure on what data you have, but you might want to try
GROUP BY TlineNo, TCode ...
But you have to keep a look on not to group by something that would result in duplicate contribution values.
You can use the ROW_NUMBER function that allows to partition the rows in groups, and number the lines inside each group starting by one. If you choose the right columns to define the partition, and keep only the rows with "row_number = 1`, you have solved the first part of your problem, i.e. discarding the lines that don't have to appear in the report. (See the sample sin the linked documentation, they're quite clear).
Once you have solved this problem, you simply have to repeat what you're doing, but on the result of this data, instead of the original data. You can use a view, a CTE, or a subselect to achieve your result, i.e.
With view:
CREATE VIEW FilteredData AS -- here the rank function query, then selct from the view
SELECT --here your current query --
FROM FilteredData
With CTE
WITH -- here the rank function query
SELECT -- your current querym, from the CTE
With subselect
SELECT -- your current query
FROM (SELECT FROM -- here the rank function query -- )
Appreciate your assistance with my query. After playing around, I have found a solution that works just as I want. It is as below: I did a Group by as hinted by #Yogesh86 on a few fields.
SELECT
MAX(t.branch_no) as TBranchNo,
Max(t.workstation_no) as TWorkstation,
t.tender_ref_no as TSaleRefNo,
Max(t.tender_line_no) as TLineNo,
t.tender_code as TCode,
MAx(T.contribution) as TContribution,
MAX(l.sale_line_no) as SaleLineNo
FROM TENDER_LINES t
LEFT JOIN SALES_TX_LINES l
on t.branch_no = l.branch_no and t.workstation_no = l.workstation_no and t.tender_ref_no = l.sale_tx_no
where l.sale_tx_no = 2000293 OR l.sale_tx_no = 1005246 --OR sale_tx_no = 1005261
GROUP BY
t.tender_ref_no,
t.tender_line_no,
t.tender_code
My SQL query is the following:
SELECT
oh.ORDER_NUMBER, oh.CREATED_TS, stat.STATE, stat.TIMESTAMP
FROM
IMPL_OH_ORDER_HEADER oh,
SNCR_ORDER_DISPOSITION stat,
SNCR_ORDER_DISP_HEAD stathead,
IMPL_CUST_ACCOUNT cust
WHERE
stat.DISP_TRANSACTION_ID=stathead.DISP_TRANSACTION_ID
and stathead.TRANSACTION_ID=oh.TRANSACTION_ID
and oh.ACCOUNT_ID=cust.ACCOUNT_ID
and stat.CATEGORY=100
and oh.USER_ID='a'
and cust.ACCOUNT_NUM='123'
GROUP BY oh.ORDER_NUMBER
ORDER BY stat.TRX_SEQ DESC;
It is giving the following error: ORA-00979: not a GROUP BY expression
If I remove the GROUP BY expression, I don't get the error.
What is correct GROUP BY syntax?
Research: A google search says that, if AGGREGATE functions are used, I should have all fields not used by aggregate function in the GROUP BY clause. So I tried to modify the GROUP BY expression to GROUP BY oh.ORDER_NUMBER, oh.CREATED_TS, stat.TIMESTAMP, stat.STATE.
Yet I am getting the same error.
Please help.
You get the error because you have STAT.TRX_SEQ in the order by list.
If you use the group by clause together with the order by clause, you must either order by a column on which you grouped or on an aggregated value.
This rule also applies to the selected values: they're either part of the group by or aggregate functions (or pseudo columns).
Can you try this one and see??
SELECT OH.ORDER_NUMBER,
OH.CREATED_TS,
STAT.STATE,
STAT.TIMESTAMP
FROM IMPL_OH_ORDER_HEADER OH,
SNCR_ORDER_DISPOSITION STAT,
SNCR_ORDER_DISP_HEAD STATHEAD,
IMPL_CUST_ACCOUNT CUST
WHERE STAT.DISP_TRANSACTION_ID = STATHEAD.DISP_TRANSACTION_ID
AND STATHEAD.TRANSACTION_ID = OH.TRANSACTION_ID
AND OH.ACCOUNT_ID = CUST.ACCOUNT_ID
AND STAT.CATEGORY = 100
AND OH.USER_ID = 'A'
AND CUST.ACCOUNT_NUM = '123'
GROUP BY OH.ORDER_NUMBER,
OH.CREATED_TS,
STAT.STATE,
STAT.TIMESTAMP
ORDER BY STAT.TRX_SEQ DESC;
I have the following Postgres query:
SELECT array_agg("Esns".id )
FROM public."Esns",
public."PurchaseOrderItems"
WHERE
"Esns"."PurchaseOrderItemId" = "PurchaseOrderItems".id
AND "PurchaseOrderItems"."GradeId"=2
LIMIT 2;
The limit will affect the rows. I want it to limit the array_agg() to 2 items. The following query works but I get my output with each entry in quotes:
SELECT array_agg ("temp")
FROM (
SELECT "Esns".id
FROM public."Esns",
public."PurchaseOrderItems"
WHERE
"Esns"."PurchaseOrderItemId" = "PurchaseOrderItems".id
AND "PurchaseOrderItems"."GradeId"=2
LIMIT 4
) as "temp" ;
This give me the following output
{(13),(14),(15),(12)}
Any ideas?
select id[1], id[2]
from (
SELECT array_agg("Esns".id ) as id
FROM public."Esns",
public."PurchaseOrderItems"
WHERE
"Esns"."PurchaseOrderItemId" = "PurchaseOrderItems".id
AND "PurchaseOrderItems"."GradeId"=2
) s
or if you want the output as array you can slice it:
SELECT (array_agg("Esns".id ))[1:2] as id_array
FROM public."Esns",
public."PurchaseOrderItems"
WHERE
"Esns"."PurchaseOrderItemId" = "PurchaseOrderItems".id
AND "PurchaseOrderItems"."GradeId"=2
The parentheses (not "quotes") in the result are decorators for the row literals. You are building an array of whole rows (which happen to contain only a single column). Instead, aggregate only the column.
Also, direct array construction from a query result is typically simpler and faster:
SELECT ARRAY (
SELECT e.id
FROM public."Esns" e
JOIN public."PurchaseOrderItems" p ON p.id = e."PurchaseOrderItemId"
WHERE p."GradeId" = 2
-- ORDER BY ???
LIMIT 4 -- or 2?
)
You need to ORDER BY something if you want a stable result and / or pick certain rows. Otherwise the result is arbitrary and can change with every next call.
While being at it I rewrote the query with explicit JOIN syntax, which is generally preferable, and used table aliases to simplify.