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

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 ...
)

Related

An object or column name is missing or empty. But its not?

I am on the 7th set of queries I have been working on and all of them have used SELECT * INTO some_table without an issue. For some reason tho the below query in SQL Server is throwing the error
An object or column name is missing or empty. For SELECT INTO
statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed.
Change the alias to a valid name.
Any Idea on what it could be?
Note that running the query without the select into will result in data being returned and displayed as expected.
Select * into MYDB.MY_TBL
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHAA"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'
You have two selects. I think you just want:
SELECT OT.U_ID AS "U_ID",
. . .
INTO MYDB.MY_TBL
FROM . . .
The INTO should follow the SELECT column list.
Or alternatively, you could use a subquery, but that does not seem necessary.
Where #GordonLinoff example works I realized what I forgot and it was my FROM () sub-query setup I normally use.
I will provide an example in contrast to Gordon's method.
IE:
Select * into MYDB.MY_TBL
FROM(
SELECT OT.U_ID AS "U_ID"
...
FROM MYDB.OTHER_TBL OT
WHERE OT.U_ID = 'TEST'
) SUB
Please note these two points:
1.You have two columns with given "CHA" alias
2.It Seems you need to rewrite the query as:
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA_DUPLICATE"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHA_DUPLICATE2"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
INTO MYDB.MY_TBL -- <<<<<<< NOTE
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'

error on statement execution when creating a view

Could someone please help in explaining the error I get when executing following statement:
Error starting at line 1 in command:
`
CREATE OR REPLACE FORCE VIEW "PRODILMOWNER"."ZV_LPEUR_I" ("MANDT","BALANCETYPE", "BRANCH", "CURRENCY", "AMOUNT", "COUNTERPARTY", "COUNTERPARTY_PARENT", "AMOUNT_EUR","DESCRIPTION")
AS
SELECT a.balancetype,
a.branch,
a.currency,
cast a.amount as number (25,2),
a.counterparty,
a.counterparty_parent,
a.mandt,
a.description,
(a.amount*
(SELECT c.midspot
FROM ZV_EXCHANGERATES c
WHERE c.currency =a.currency
AND c.valuedate =TO_CHAR(sysdate,'YYYY/MM/DD')
AND a.valuedate = TO_CHAR(sysdate,'DD-MMM-YY')
) ) AS cast amount_eur as number (25,2),
FROM ZT_LP a
Error at Command Line:1 Column:0
Error report:
SQL Error: No more data to read from socket
You may have other errors, but the correct way to express a cast is:
cast(a.amount as number(25, 2)) as amount,
Note the parentheses and the column alias.
If you try to create view as following :
CREATE OR REPLACE FORCE VIEW "PRODILMOWNER"."ZV_LPEUR_I" ("MANDT","BALANCETYPE", "BRANCH", "CURRENCY", "AMOUNT", "COUNTERPARTY", "COUNTERPARTY_PARENT", "AMOUNT_EUR","DESCRIPTION")
AS
SELECT a.balancetype,
a.branch,
a.currency,
cast(a.amount as number(25, 2)) as amount, --cast a.amount as number (25,2),
a.counterparty,
a.counterparty_parent,
a.mandt,
a.description,
a.amount*
(SELECT cast(c.midspot as number(25, 2))
FROM ZV_EXCHANGERATES c
WHERE c.currency =a.currency
AND c.valuedate =TO_CHAR(sysdate,'YYYY/MM/DD')
AND a.valuedate = TO_CHAR(sysdate,'DD-MMM-YY')
) AS amount_eur -- cast amount_eur as number (25,2) --,
FROM ZT_LP a;
by removing parts right next to dashes(--), then no problem remains.
take casts in paranthesis
take second cast inside subselect
remove the comma , just before FROM clause

UNION data type mismatch

Running a UNION query in an access database. I've defined every variable as a int since there was a data type mismatch error prompt. This has not resolved the issue. Each of the variables have values of either 1 or 0 and no nulls. Any ideas?
SELECT
CInt(qryGB.BM∞) AS [BM∞],
CInt(qryGB.PM∞) AS [PM∞],
CInt(qryGB.P∞) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.P0) AS [P0],
CInt(qryGB.C0) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB
UNION ALL SELECT
CInt(qryTMD.BM∞) AS [BM∞],
CInt(qryTMD.PM∞) AS [PM∞],
CInt(qryTMD.P∞) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.P0) AS [P0],
CInt(qryTMD.C0) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD;
Check you don't have any nulls in any of the columns.
Access SQL is a little strange when it comes to nulls (noting that Standard SQL nulls are strange to begin with!). For example you can't cast a null to a data type:
SELECT DISTINCT CINT( NULL ) AS null_cast_to_int FROM AnyPopulatedTable;
errors with "Invalid use of Null".
So all Access SQL nulls are of the same type but what type?:
SELECT DISTINCT TYPENAME ( NULL ) AS type_name FROM AnyPopulatedTable;
does not error and returns 'Null' !!
The one thing I can think of is that doing the CInt() conversion during the UNION may be screwing things up. I'd try doing the conversion in subqueries before doing the UNION. Something like:
SELECT
a.[BM∞],
a.[PM∞],
a.[P∞],
a.[RAG_B<0],
a.[RAG_P<0],
a.[RAG_C<0],
a.[B<0],
a.[P<0],
a.[C<0],
a.[P-1],
a.[C-1],
a.[P0],
a.[C0],
a.[P+1],
a.[P+2],
a.[P+3]
FROM
(SELECT
CInt(qryGB.[BM∞]) AS [BM∞],
CInt(qryGB.[PM∞]) AS [PM∞],
CInt(qryGB.[P∞]) AS [P∞],
CInt(qryGB.[RAG_B<0]) AS [RAG_B<0],
CInt(qryGB.[RAG_P<0]) AS [RAG_P<0],
CInt(qryGB.[RAG_C<0]) AS [RAG_C<0],
CInt(qryGB.[B<0]) AS [B<0],
CInt(qryGB.[P<0]) AS [P<0],
CInt(qryGB.[C<0]) AS [C<0],
CInt(qryGB.[P-1]) AS [P-1],
CInt(qryGB.[C-1]) AS [C-1],
CInt(qryGB.[P0]) AS [P0],
CInt(qryGB.[C0]) AS [C0],
CInt(qryGB.[P+1]) AS [P+1],
CInt(qryGB.[P+2]) AS [P+2],
CInt(qryGB.[P+3]) AS [P+3]
FROM qryGB) as a
UNION ALL SELECT
b.[BM∞],
b.[PM∞],
b.[P∞],
b.[RAG_B<0],
b.[RAG_P<0],
b.[RAG_C<0],
b.[B<0],
b.[P<0],
b.[C<0],
b.[P-1],
b.[C-1],
b.[P0],
b.[C0],
b.[P+1],
b.[P+2],
b.[P+3]
FROM
(SELECT
CInt(qryTMD.[BM∞]) AS [BM∞],
CInt(qryTMD.[PM∞]) AS [PM∞],
CInt(qryTMD.[P∞]) AS [P∞],
CInt(qryTMD.[RAG_B<0]) AS [RAG_B<0],
CInt(qryTMD.[RAG_P<0]) AS [RAG_P<0],
CInt(qryTMD.[RAG_C<0]) AS [RAG_C<0],
CInt(qryTMD.[B<0]) AS [B<0],
CInt(qryTMD.[P<0]) AS [P<0],
CInt(qryTMD.[C<0]) AS [C<0],
CInt(qryTMD.[P-1]) AS [P-1],
CInt(qryTMD.[C-1]) AS [C-1],
CInt(qryTMD.[P0]) AS [P0],
CInt(qryTMD.[C0]) AS [C0],
CInt(qryTMD.[P+1]) AS [P+1],
CInt(qryTMD.[P+2]) AS [P+2],
CInt(qryTMD.[P+3]) AS [P+3]
FROM qryTMD) as b

multiplying modified colums to form new column

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.

If statement in SQL issue

I'm using Access to run queries on a database called dbo_item. It contains a column called itm_class that contains numbers 1-100. It also contains columns called itm_len, itm_wid, itm_hgt, pal_len, pal_wid, pal_hgt. Depending on the item class number, I need it to calculate the volume either by item dimensions or by pallet dimensions. I have tried a few different things, but am not really sure how to get the If statement to work. Any help would be appreciated.
I.E. If itm_class equals 1,5,22,45,67,89,97,98,99,100 then volume is (itm_len * itm_wid * itm_hgt) and for all other itm_class numbers, volume is (pal_len * pal_wid * pal_hgt). Is there any way to do this?
You can use a iif condition for this.
IIF(itm_class in (1,5,22,45,67,89,97,98,99,100), itm_len * itm_wid * itm_hgt, pal_len * pal_wid * pal_hgt)
It works like , If condition(1st argument) is True, select the first value (2nd argument) or else select the other (3rd argument)
It depends on if you are doing this statement in visual-basic or if you are doing it at a database level (function/stored procedure) or if you are doing it with the query. The simplest way would be with a case when statement in your query such as:
select case when itm_class in (1,2,3,4) then (itm_len * itm_wid * itm_hgt)
when itm_class in (5,6,7,8) then (itm_len * itm_wid)
else (itm_len * itm_wid) end volume_calculation
from some_table