Varchar invalid for Sum operator - sql

I have a table called Cos and the datatype of Amt is Float and sample data looks like:
Acct Period F_year Amt
Detf 1 2011 Null
Detf 2 2011 Null
Detf 3 2011 1669.57
FTE 1 2011 3205.11
FTE 2 2011 0
FTE 3 2011 Null
I wrote a query like:
Select Acct,Period,F_year, Sum(AMT) as Amt
from dbo.Cos
Group By Acct,Period,F_year
Where Amt is not null
But i am getting this error:
Msg 8117, Level 16, State 1, Line 1
Operand data type varchar is invalid for sum operator.
Can anyone help me?

Try doing this:
Select Acct,Period,F_year, Sum(isnull(cast(AMT as float),0)) as Amt
from dbo.Cos
Group By Acct,Period,F_year

Apparently, the value "1669.57" is a string. So what does it mean to add this value to another?
The error message is correct: It's not valid to add text values together. If it was valid, I could not tell what the result should be.
You should either change your column type to a numeric type, or convert it somehow before trying to add it.

If Amt is intended to be used for mathematical operations, then it should be type Decimal and not varchar.

Related

SELECT statement for 3 employee Id's

Please can someone help me with the SQL code that list all the data for the 3 Employee ID's:
60-578-2269, 50-218-3739 and 80-772-4580 ? I was only able to pull the data for one Employee ID below:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID = '60-578-2269'
Also, the sql statement that list every employee that was hired after 1st April 2021, in date format 04/01/2021. I ran this:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired> = 01/04/2021
but the error message below came up:
Msg 245, Level 16, State 1, Line 23 Conversion failed when converting
the nvarchar value '4/1/21' to data type int.
For the first query use WHERE IN (...) with a tuple of employee IDs which you want to find:
SELECT *
FROM [dbo].Employee_Certification
WHERE Employee_ID IN ('60-578-2269', '50-218-3739', '80-772-4580');
For the second query, use a proper date literal which SQL Server will recognize:
SELECT *
FROM [dbo].Employee_Certification
WHERE Date_Hired >= '20210401';

Impala: values are in wrong columns in result query

In my result query the values are in wrong columns.
My SQL Query is like:
create table some_database.table name as
select
extract(year from t.operation_date) operation_year,
extract(month from t.operation_date) operation_month,
extract(day from t.operation_date) operation_day,
d.status_name,
sum(t.operation_amount) operation_amt,
current_timestamp() calculation_moment
from operations t
left join status_dict d on
d.status_id = t.status_id
group by
extract(year from t.operation_date) operation_year,
extract(month from t.operation_date) operation_month,
extract(day from t.operation_date) operation_day,
d.status_name
(In fact, it's more complicated, but the main idea is that I'm aggregating source table and making some joins.)
The result I get is like:
#
operation_year
operation_month
operation_day
status_name
operation_amt
1
2021
1
1
success
100
2
2021
1
1
success
150
3
2021
1
2
success
120
4
null
2021-01-01 21:53:00
success
120
null
The problem is in row 4.
The field t.operation_date is not nullable, but in result query in column operation_year we get null
In operation_month we get untruncated timestamp
In operation_day we get string value from d.status_name
In status_name we get numeric aggregate from t.operation_amount
In operation_amt we get null
It looks very similar to a wrong parsing of a csv file when values jump to other columns, but obviously it can't be the case here. I can't figure out how on earth is it possible. I'm new to Hadoop and apparently I'm not aware of some important concept which causes the problem.

add two zero with decimal if number is complete integer in Oracle SQL column

Column amount contains data like - 200.203, 200, 5.10, 45.20, 10 and 5000213.012
Now I want to select if the number does not include any decimal digit then we add .00 in the returning result.
Expected result - 200.203, 200.00 ,5.10, 45.20 , 10.00 and 5000213.012
You can use TO_CHAR to format the value so that it has at least 2 decimal places:
SELECT value,
TO_CHAR( value, 'FM9999999990.009' ) AS formatted_value
FROM table_name
Which, for the sample data:
CREATE TABLE table_name ( value ) AS
SELECT 200.203 FROM DUAL UNION ALL
SELECT 200 FROM DUAL UNION ALL
SELECT 5.10 FROM DUAL UNION ALL
SELECT 45.20 FROM DUAL UNION ALL
SELECT 10 FROM DUAL UNION ALL
SELECT 5000213.012 FROM DUAL;
Outputs:
VALUE | FORMATTED_VALUE
----------: | :--------------
200.203 | 200.203
200 | 200.00
5.1 | 5.10
45.2 | 45.20
10 | 10.00
5000213.012 | 5000213.012
db<>fiddle here
Following some comments, the OP's question is effectively:
How can I change this number to have a given format?
When you consider that a NUMBER data type has no format and 5, 5.0 and 5.00000 are exactly the same value and the database does not store how many trailing zeroes a decimal value has then this question does not entirely make sense as there is no way to give a number a format.
Instead the question can be formulated as either:
How can I get <insert name of client program> to display numbers so that they always have at least 2 decimal places?
We can't answer this question without knowing the client program so I'll skip it.
Or:
How can I display a number from an Oracle query so that it is formatted with at least 2 decimal places?
Since NUMBERs never have any formatting of their own then this must involve a conversion to another data-type which can represent the number with a format (i.e. a string). So the result is that if you want a NUMBER data type then Oracle will not give it a format (but the client program might) but if you want to change the NUMBER to a string data type then you can give it a format (but it won't be a NUMBER as its now a string that just happens to contain digits) and, as described above, TO_CHAR does that.
This will do:
select to_number(to_char(value, '9999999.99')) from dual;

group yearmonth field by quarter in sql server

I have a int field in my database which represent year and month like 201501 stands for 2015 Jan,
i need to group by reporting_date field and showcase the quarterly data .The table is in the following format .Reporting_date is an int field rather than a datetime and interest_payment is float
reporting_date interest_payment
200401 5
200402 10
200403 25
200404 15
200406 5
200407 20
200408 25
200410 10
the output of the query should like this
reporting_date interest_payment
Q1 -2004 40
Q2 -2004 20
Q3 -2004 40
Q4 -2004 10
i tried using the normal group by statement
select reporting_date , sum(interest_payment) as interest_payment from testTable
group by reporting_date
but got different result output.Any help would be appreciated
Thanks
before grouping you need to calculate report_quarter, which is equal to
(reporting_date%100-1)/3
then do select
select report_year, 'Q'+cast(report_quarter+1 as varchar(1)), SUM (interest_payment)
from
(
select
*,
(reporting_date%100 - 1)/3 as report_quarter,
reporting_date/100 as report_year
from #x
) T
group by report_year, report_quarter
order by report_year, report_quarter
I see two problems here:
You need to convert reporting_date into a quarter.
You need to SUM() the values in interest_payment for each quarter.
You seem to have the right idea for (2) already, so I'll just help with (1).
If the numbers are all 6 digits (see my comment above) you can just do some numeric manipulation to turn them into quarters.
First, convert into months by dividing by 100 and keeping the remainder: MOD(reporting_date/100).
Then, convert that into a quarter: MOD(MOD(reporting_date/100)/4)+1
Add a Q and the year if desired.
Finally, use that value in your GROUP BY.
You didn't specify which DBMS you are using, so you may have to convert the functions yourself.

smallest and largest date issue

I wanted to get smallest date and largest date stored in database.
For that i used following query:
select min(sauda_date) as MinDate,max(sauda_date) as MaxDate from tradefile
And I got Following result:
But i know this result is wrong.
It should be:
28 Feb 2013 as MinDate and 22 Mar 2013 as MaxDate.
Because of this i refered following question:
SQL grammar for SELECT MIN(DATE)
and changed my query to:
select min(sauda_date), max(sauda_date) from tradefile group by sauda_date
But i got following result:
While my result should be :
28 Feb 2013 as MinDate and 22 Mar 2013 as MaxDate.
Also tried with this query:
select min(convert(datetime,sauda_date)), max(convert(datetime,sauda_date)) from tradefile group by convert(datetime,sauda_date)
but got no result.[expected]
Where am i making mistake? please help me.
Note: Sauda_Date is of type nvarchar here.
"Note: Sauda_Date is of type nvarchar here."
There's your problem, then - if you select the min or max of a nvarchar type, then SQL uses lexicographical sorting - as in 0 to 9, a to z. What else could it use? How could it possibly know that you intend them to be interpreted as datetime?
If you cannot change the schema and make the column dates, then you can cast/convert the datatype to datetime before using min() or max() on them.
So you want something like
select min(convert(datetime,sauda_date)), max(convert(datetime,sauda_date)) from tradefile
Try this:
select
min(CAST(sauda_date AS DATETIME)) as MinDate,
max(CAST(sauda_date AS DATETIME)) as MaxDate
from tradefile