sql syntax error whith where and group by - sql

I wrote a command in sql server 2014 , the command fails with the following error
command
select * from View_FirstReport
where Date Between '1392/03/20' and '1392/03/19', Insure_ID = '0', User_ID='2'
Group by insure_id, InsureTitle, status
error
Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ','.

You should combine the parts of your query with AND and OR.
select
*
from
View_FirstReport
where
Date Between '1392/03/20' and '1392/03/19'
AND Insure_ID = '0'
AND User_ID='2'
Group by insure_id, InsureTitle, status

Replace the , in your WHERE clause with AND:
SELECT *
FROM View_FirstReport
WHERE
Date Between '1392/03/20' AND '1392/03/19'
AND Insure_ID = '0' AND User_ID='2'
GROUB BY insure_id,InsureTitle,status
A detailed syntax definition of WHERE can for example be found here.

Use AND instead of ,:
SELECT * FROM View_FirstReport
WHERE Date BETWEEN '1392/03/20' and '1392/03/19'
AND Insure_ID = 0
AND User_ID = 2
GROUP BY insure_id, InsureTitle, status
Also, integer columns do not require single quote to surround the values.

You should have to replace your , with and operator and select only those records which you are including in group or MIN , MAX , AVG etc for other columns.
SELECT insure_id, InsureTitle, status FROM View_FirstReport
WHERE Date BETWEEN '1392/03/20' and '1392/03/19'
AND Insure_ID = 0
AND User_ID = 2
GROUP BY insure_id, InsureTitle, status

Try like this,
SELECT insure_id
,InsureTitle
,[STATUS]
FROM View_FirstReport
WHERE [DATE] BETWEEN '13920320'
AND '13920319'
AND Insure_ID = '0'
AND [User_ID] = '2'
GROUP BY insure_id
,InsureTitle
,[STATUS]
This is almost similar to SandeepKumar answer.

Related

SQL Max from Subquery, Msg 102, Incorrect syntax near ')'

I'm trying to get only the Max-Results of the sums per day. I'm trying this with
Select Max(Anzahl) from (Subquery)
The Subquery itself works, but when I put it into the brackets, I get the following error message:
Msg 102, Level 15, State 1, Line 8 Incorrect syntax near ')'.
See below for the whole query.
Many thanks for any hints!
Cheers
Lukas
select Max(Anzahl) from
(
select CONVERT(VARCHAR(10), log1.timestamp, 104) as Date, log1.ID, count(*) Anzahl
from log1
inner join base on log1.ID = base.ID
where abc like '%test%' and log1.xyz = 3
GROUP BY CONVERT(VARCHAR(10), log1.timestamp, 104), log1.ID
)
In SQL every derived table requires an alias so it can be uniquely identified, so
select Max(Anzahl) from
(
select CONVERT(VARCHAR(10), log1.timestamp, 104) as Date, log1.ID, count(*) Anzahl
from log1
inner join base on log1.ID = base.ID
where abc like '%test%' and log1.xyz = 3
GROUP BY CONVERT(VARCHAR(10), log1.timestamp, 104), log1.ID
) as AliasName

how to run a different select statement based on condition in Hive SQL

I would like to know how to run a different select statement based on condition in Hive SQL.
The following query does not work but throws an error.
Error while compiling statement: FAILED: ParseException line 4:2
cannot recognize input near '(' 'SELECT' '1' in expression
specification
SELECT
CASE WHEN '${UN}'!= '' THEN
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in ('${UN}')
)
ELSE
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in (
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
)
)
END
Use UNION ALL with your queries + add conditions for switching corresponding query:
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' and '${D2}')
and t.un in ('${UN}')
and '${UN}'!= '' --switching condition
union all
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' AND '${D2}')
and t.un in
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
and '${UN}'= '' --switching condition

Invalid SQL Syntax CLI0118E

I am trying to execute the below query and getting Invalid SQL Syntax error
. [IBM][CLI Driver] CLI0118E Invalid SQL syntax. SQLSTATE=37000(37000,-99999). Is it anything to do with driver upgrade? It was working fine a while ago. Please advice. Thanks in advance.
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as
statusDesc
from task_log a,(
select id_bus_procss, name_task , id_run,
max(dt_lst_updt) as dt_lst_updt from task_log
where id_run = '1'
and id_bus_procss = '14'
and name_task in ({0})
and dt_lst_updt >= (
select dt_evnt_sts from
sf_evntflow_sts
where id_run = '1' and
id_evntflow ='15'
and cd_evnt_sts in (''CLN'',''RTY'' )
)
group by id_bus_procss, name_task, id_run)
X
where a.dt_lst_updt = X.dt_lst_updt
if you are executing this on stored procedure , Try this may help, Remove the new line character and separate the parameters by
spaces .
Or otherwise try upgrade.......
if you try this query its better?
select a.name_task as nameTask, a.cd_sts as cdSts, a.desc_err_msg as statusDesc
from task_log a,
( select b.id_bus_procss, b.name_task , b.id_run, max(b.dt_lst_updt) as dt_lst_updt
from task_log b inner join sf_evntflow_sts c on b.id_run=c.id_run and c.id_evntflow ='15'
and c.cd_evnt_sts in (''CLN'',''RTY'' ) and b.dt_lst_updt >=c.dt_evnt_sts
where b.id_run = '1' and b.id_bus_procss = '14' and b.name_task in ({0})
group by b.id_bus_procss, b.name_task, b.id_run
) as X
where a.dt_lst_updt = X.dt_lst_updt

SELECT query using group by

I don't know why the following query is not working:
SELECT whs_code, pdt_code,case_dt_yyyymmdd, fresh_frozen_status,
SUM(qty_cases_on_hand)-Qty, SUM(qty_weight_on_hand)-Wt, operation
FROM
(
SELECT whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation,SUM(qty_cases_on_hand) AS Qty, SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
)
WHERE operation='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation`
The error is :
Msg 156, Level 15, State 1, Line 4 Incorrect syntax near the keyword 'WHERE'.
To make it simple to understand what i am trying to do here, please see the example
I need data as result of
(SELECT x FROM tbl_table Where column y='SU')-(SELECT x FROM tbl_table Where column y='RU')
SELECT ru.whs_code,
ru.pdt_code,
ru.case_dt_yyyymmdd,
ru.fresh_frozen_status,
ru.operation,
ru.Qty - su.Qty AS Qty_Diff,
ru.Wt - su.Wt AS Wt_Diff
FROM
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='RU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) ru,
(
SELECT whs_code,
pdt_code,
case_dt_yyyymmdd,
fresh_frozen_status,
operation,
SUM(qty_cases_on_hand) AS Qty,
SUM(qty_weight_on_hand) AS Wt
FROM tbl_inventory_activity_rpt1
WHERE operation ='SU'
GROUP BY whs_code,pdt_code,case_dt_yyyymmdd,fresh_frozen_status,operation
) su
WHERE ru.whs_code = su.whs_code
AND ru.pdt_code = su.pdt_code
AND ru.case_dt_yyyymmdd = su.case_dt_yyyymmdd
AND ru.fresh_frozen_status = su.fresh_frozen_status
AND ru.operation = su.operation;

Nvarchar to decimal and filter the result

I am having trouble making a query that converts nvarchar column to decimal and display only values that are are > 90, <90 and =90.
With query that I built, at first it seemed like it gives the desired result, but it doesn't. The column(Price) has both null values and non numeric values like 'red',abcde' etc.Also, the values in nvarchar column (price) has 1,2,3 or more numbers after the decimal point like 90.0, 1234.5643, 34.00 etc.
Here is the query I wrote -
select * from PriceList A1
join (
select * from priceList
where ISNUMERIC(price) <>0)
)A2
on A1.productId = A2.productId
and A1.ObjectKey=a2.ObjectKey
and CAST(A1.price as decimal(10,4)) > '90'
I get few rows back result and then there is an error -
Msg 8114, Level 16, State 5, Line 2
Error converting data type nvarchar to numeric.
What is wrong with the query?
Edited below. This works except for =(equal to) condition I get 'Error converting nvarchar to numeric'. The error sis same for = 90 or = 900 etc. Wonder what's wrong now?!
select * from PriceList A1
join
( select * from PriceList where isnumerice(price)<>0
and price like '%[^a-zA-z]%' ) A2
on A1.productid = A2.productId and A1.ObjectKey=a2.ObjectKey
where CAST(A1.convertedvalue as decimal(20,8)) > '90'
and price like '%[^a-zA-z]%'
It is because CAST(A1.price as decimal(10,4))>90 is giving error for the non numeric values like 'red','abcde', etc
Try this
select * from
(
select A1.* from PriceList A1
join (
select * from priceList
where ISNUMERIC(price) <>0)
)A2
on A1.productId = A2.productId
and A1.ObjectKey=a2.ObjectKey
and A1.price not like '%[^0-9]%'
) as t where CAST(price as decimal(10,4)) > '90'
Use a case statement any time you want to do a conversion, and then compare or do arithmetic with the converted value:
case
when isnumeric(fieldname) = 1 then cast(fieldname as decimal(10,4))
else null end
So:
select *
from priceList
where case
when isnumeric(fieldname) = 1 then cast(fieldname as decimal(10,4))
else null
end > 90