multiplying modified colums to form new column - sql

I want a query like:
select tp.package_rate, sum(sell) as sold_quantity , select(tp.package_rate * sold_quantity ) as sell_amount from tbl_ticket_package tp where tp.event_id=1001
here the system firing error while doing the multiplication as
sold_quantity is invalid column
another problem is that in multiplication I want to use package_rate which got by select query from tp.package_rate but it multiplying with all package_rate of the table but I want only specific package_rate which was output of select query
What would you suggest? I want to bind this query in gridview . is there any way to do it using ASP.net gridview?

Your problem is that you are referring to sold_quantity here :
select(tp.package_rate * sold_quantity )
The alias is not recognized at this point.You will have to replace it with sum(sales). You will also have to group by tp.package_rate.
Your query should ideally be like :
select tp.package_rate, sum(sell) as sold_quantity ,
(tp.package_rate * sum(sell) ) as sell_amount from tbl_ticket_package tp
where tp.event_id=1001 group by tp.package_rate;
I am guessing that tp.package_rate is unique for a given event_id, from latter part of your question. If that's not the case, the sql you have written makes no sense.

Related

Ambigously defined column in a subquery

I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)

weird where clasue in TSQL

In the code snippet below I see a column in a where clause that I have never seen or used before. The code works and is not my code. My question is if this is some kind of "customized" TSQL language or if is legitimate to have a column in a where clause and if so what is the scope of it.
For example this clause " WHERE FMTREFl :vall" why the :val1?
SELECT 1
,FMTTRANTYPE
,FMTSTATUS
,FMTTIMEDATE
,FMTREF2
,FMTREF3
,FMTSEQNO
FROM FMTRANS
WHERE FMTREFl :vall
OR FMTHDR ANY (
SELECT FMTHSEQNO
FROM FMTRANSHDR
WHERE FMTHICPNO :val2
)
UNION ALL
SELECT 0
,FMTTRANTYPE
,FMTSTATUS
,FMTTIMEDATE
,FMTREF2
,FMTREF3
,FMTSEQNO
FROM FMTRANSIN
WHERE FMTREFl :val3
OR FMTHDR ANY (
SELECT FMTHSEQNO
FROM FMTRANSHDR
WHERE FMTHICPNO :val4
)
ORDER BY FMTTIMEDATE ASC

MS Access query produces invalid procedure call

My query in access works fine if i only use the below query with a select statement. As soon as it becomes an append query it produces an "invalid procedure call" error.
I have narrowed down the offending columns as being "Publ" and "PublLong". Both are long text strings. If I remove these two columns the query updates without an error.
Here is a sample data point found in the [Bezeichung] Field:
publications.bank.com/publ-dl-ch/pdf/WhatsUp_20181113_en.pdf
I checked the table that it is being inserted to and the data types are the same nor did i see any other setting that would block the insertion.
How can i get it to work?
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
xlsx_Output.[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM xlsx_Output
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
#Func! indicates one of your functions is causing an error.
Your query uses multiple functions that run into trouble when your input doesn't meet that format, pre-filter instead of filtering on an error, since you can't filter on an error when appending:
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM (SELECT * FROM xlsx_Output WHERE Len(Bezeichnung) > 5 AND Bezeichnung LIKE "*?.?*" AND Bezeichnung LIKE "*_????????*" AND Bezeichnung LIKE "*?\?*")
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
Since I don't know exactly where the errors occur, I can't write up a proper filter to identify them, but judging by your query they should include a slash and a symbol after that slash, an underscore and at least 8 symbols after that underscore, and a dot with at least one symbol before and after the dot.

Issue with union query - select list not compatible

Been working on this query for some time now... Keep getting the error "Corrosponding select-list expressions are not compatible. I am selecting the same # of columns in each select statement.
create volatile table dt as (
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gpv.c_val,
gpv.i_val,
gcv.c_pol,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
and gcv.c_pol between 'lac100001' and 'lac100004'
UNION
SELECT
gcv.I_SYS_IDV,
gcv.i_pln,
gcv.c_typ_cov,
gcv.d_eff,
gcv.d_eff_pln,
gcv.c_sta,
gcv.d_sta,
gcv.c_mde_bft_fst,
gcv.a_bft_fst,
gcv.c_mde_bft_sec,
gcv.a_bft_sec,
gcv.c_mde_bft_trd,
gcv.a_bft_trd,
gcv.p_cre_hom,
gcv.c_cl_rsk,
gcv.c_pol,
gpv.i_val,
gpv.i_pln,
gpv.i_prv
FROM Pearl_P.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
where NOT EXISTS(
SELECT 1
FROM pearl_p.tltc906_gcv gcv,
pearl_p.tltc912_gpv gpv
WHERE gcv.i_pln > 0
AND gcv.i_pln = gpv.i_pln
and gpv.i_prv = '36'
)
) with data
PRIMARY INDEX (i_sys_idv)
on commit preserve rows;
You should check the data types of each column. The data types must be compatible between each SELECT statement.
The last 4 values of your second select statement don't match the ones in your first statement. Try naming(using aliases) those columns the same thing(pairing them). To union you need to have the same columns between the sets.
Check out: http://msdn.microsoft.com/en-us/library/ms180026.aspx
The following are basic rules for combining the result sets of two
queries by using UNION:
The number and the order of the columns must be the same in all
queries.
The data types must be compatible.
Are the data types of each column the same in both portions of the query?
If the first character of the column name indicates the data type (i = integer, c = character, etc.) I'm guessing that the problem is with the second to last column in the select list. The first query is selecting gcv.c_pol, the second query is selecting gpv.i_pln.
Start commenting-out lines until it works. I.e., comment out all of the fields in each select list, and then one-by-one, un-comment the first one out, then the second, etc. You'll find it eventually.

How to reuse a dynamic column in another column calculation in MySQL?

I have this kinda query:
SELECT
IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg,
(SELLOFF1 / weekavg) AS procent
FROM .....
it tells me: Unknown column 'weekavg' in 'field list', this happens after I've added the divide, prior to that worked ok.
You can use a sub-query:
SELECT (selloff1 / weekavg) AS procent
FROM (
SELECT
selloff1, IF(daycode=1,(SELECT...),(SELECT...)) AS weekavg
FROM ...
)