Cast and Sum functions - sql

I am writing a macro which pulls data from Oracle and displays in Excel. In Oracle DB we have a custom table with a column Named "Calculated_Quantity". The datatype of this column is BINARY_DOUBLE. However when I write a query in Excel macro to retreive this column, I get the error as "Data Type is not Supported". So I had to use "Cast" function to bypass this error.
Now I need to sum this column. If I write the statement as
Select Id, SUM(CAST(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
it works fine, but the calculation is wrong.
If I write
Select Id, CAST(SUM(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
I get an error as missing right parenthesis. The parenthesis seem to be correct. Help please! –

Select Id, CAST(SUM(CALCULATED_QUANTITY) AS NUMBER(10)) Qty
from DW.SAMPLE

Related

A column named "date" in SQL

I have a query like this on sql
SELECT DISTINCT panel_pn_side, serial_nb, date, panel_stts
FROM table
and problem is, it doesn't get that date is a column name, maybe because to it, date is a keyword and not a column. I get the following error :
ERROR: column "date" does not exist
Do you guys have a solution for me ?
Thanks
Actually, even though it was shown as "date" in the database, after thorough research it appears that there were two columns, "date_timestampInUTC" and "date_originalTimezone" and that's why SQL didn't recognize the column named date. I have no idea what happened. Thank you all for the answers

When Oracle database link to connect to Excel's using ODBC I have "identifier's name length exceeds maximum"

I tried to get data in Oracle from Excel using ODBC and database link. I use sql like this:
Select * From A222$#TEST2
When TEST2 - name of Excel database link, and A222 - name of my worksheet of Excel file.
However, because in first row of Excel I have big string values like
A1234567890_B1234567890_C1234567890_D1234567890_E1234567890 that converted to very big column name, and when I try to use sql like this:
CREATE VIEW VIEW2 AS Select * From A222$#TEST2;
// or
Select A1234567890_B1234567890_C1234567890_D1234567890_E1234567890 as c1 from A222$#TEST2;
// or
Select "A1234567890_B1234567890_C1234567890_D1234567890_E1234567890" as c1 from A222$#TEST2;
I get following error
ORA-01948: identifier's name length (31) exceeds maximum (30)
ORA-00972: identifier is too long
And I can't change this Excel file or create temp copy of this file.
It's possible:
Using name of Excel columns like A1, AB1, Z1 instead of first row's
values in sql query from ODBC Excel?
Or something fix this problem without changing this Excel file or
creating temp copy of this Excel's file?
Thank you in advance!
I don't know if I fully understand the problem. As Oracle points out, the this count#of#people#how#live#in#San#francisco is not a valid Oracle name. But do you really want a column name, particularly if it is invalid?
I am guessing you want a string value not a column name so use single quotes:
Select 'count#of#people#how#live#in#San#francisco' as c1
from A222$#TEST2;
Also, only one from per select.

Need to make Sum IIF query in linq vb.net

I am new with the linq and i need to do this query with linq in vb.net from a data table
Every column are setted to VAR CHAR if its useful for the query:
SELECT Sum(IIf([ColumnNameToCountValues] IN
('value1','value2','value3')
And [EmployeId] Like '[IDvalue]'
And [PROJECT] LIKE '[ProjectName]',1,0))
AS Total FROM [DatatableName];
This query should return an integer.
where are your error and what are your question also what have you tried? From what you have said "This query should return an integer." May be your output got error, isn't it?
So, why not just change your database column data type to INT

change the formula in a string format into a real formula in a stored procedure

I have a table validationmaster with a column called vrformula. It contains a formula like:
pf > 1
In that pf is one of the column names in the datasource table. I have to check whether pf of all the entries in the datasourse table is > 1 or not, but I don't know how to make it work.
I can fetch that formula correctly but Sql Server considers that formula as a string, I don't know how to change that whole expression into a formula.
For example: select * from datasource where meterid=4716 and pf>=1 is the statement I want to execute, with that formula at the end of the where clause being generated from the vrformula column.
You'll have to use Dynamic SQL.
This is probably obvious, nevertheless, a quick and dirty solution is to execute a SELECT for retrieving the column value, then executing a second SELECT containing the condition.
Here I assume that you are executing the SQL statements from a code written in a programming language (not SQL).

SQL INSERT with sub query

I have a table with 2 columns. I want to provide the 1st columns value but use a select statement to query another table to figure out the value that will go in the 2nd column of the first table.
Heres what I came up with but I know is wrong..
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
(SELECT #ModelId, VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId)
Essentially I want to provide the value for VehicleModelId through #ModelId, but it won't let me use it outside of the select statement.
Try removing the brackets around the SELECT, as presumbably you're seeing an incorrect syntax error?
INSERT INTO VehicleModels_VehicleSubModels (VehicleModelId, VehicleSubModelYearId)
SELECT #ModelId,VehicleSubModelYearId
FROM VehicleSubYearIntermediate
WHERE SubModelId=#SubModelId
AND YearId=#YearId