trying to get the following to work, trying to make adjustment for timezone. If I comment out the line AND plan.resetHour=tzHour it shows the column tzHour correctly but can't seem to use the value in the where clause.
SELECT
`issuer`.`tz`
,`account`.`id`
, `plan`.`endDate`
, `plan`.`resetDay`
, `plan`.`resetHour`
, `plan`.`type`
, HOUR(NOW())
, CASE issuer.`tz`
WHEN 'US/Eastern' THEN HOUR(NOW())+1
WHEN 'US/Central' THEN HOUR(NOW())
ELSE HOUR(NOW())
END AS tzHour
FROM
`dvh`.`account`
INNER JOIN `dvh`.`plan`
ON (`account`.`plan` = `plan`.`id`)
INNER JOIN `dvh`.`issuer`
ON (`plan`.`issuer` = `issuer`.`id`)
WHERE plan.type='UNIT'
AND plan.startDate < NOW()
AND plan.endDate >NOW()
AND plan.resetDay=DAYOFWEEK(NOW())
AND plan.resetHour=tzHour
AND account.`active`=1;
Using a column alias in a WHERE clause is illegal. From MySQL docs:
Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
http://dev.mysql.com/doc/refman/5.6/en/problems-with-alias.html
One way to go about this is to move the case into a sub-select.
SELECT
`issuer`.`tz`
,`account`.`id`
, `plan`.`endDate`
, `plan`.`resetDay`
, `plan`.`resetHour`
, `plan`.`type`
, HOUR(NOW())
, tempHour.tzHour
FROM
(
SELECT
CASE `tz`
WHEN 'US/Eastern' THEN HOUR(NOW())+1
WHEN 'US/Central' THEN HOUR(NOW())
ELSE HOUR(NOW())
END AS tzHour
FROM issuer
) AS tempHour,
`dvh`.`account`
INNER JOIN `dvh`.`plan`
ON (`account`.`plan` = `plan`.`id`)
INNER JOIN `dvh`.`issuer`
ON (`plan`.`issuer` = `issuer`.`id`)
WHERE plan.type='UNIT'
AND plan.startDate < NOW()
AND plan.endDate >NOW()
AND plan.resetDay=DAYOFWEEK(NOW())
AND plan.resetHour=tempHour.tzHour
AND account.`active`=1;
solved by moving case to where clause
AND plan.`resetHour` =
CASE
WHEN issuer.`tz`='US/Eastern' THEN HOUR(NOW())+1
WHEN issuer.`tz`='US/Central' THEN HOUR(NOW())
WHEN issuer.`tz`='US/Mountain' THEN HOUR(NOW())-1
WHEN issuer.`tz`='US/Pacific' THEN HOUR(NOW())-2
END
Related
1) I am creating a Calendar View and I would like to add a column which checks if the date in that row is a working day if so then populate with 1, else 0.
I have a table with all UK public holidays and in the calendar table I also have a 'dayoftheweek' field where 1 and 7 is SUN and SAT.
Currently I am trying use a case statement, however I cannot run the script as receive the following error:
SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CASE IF CT.dayofweek = `1` OR CT.dayofweek = `7` OR CT.dt = PH.`H' at line 12 (edited)
This is my script:
ALTER VIEW Calendar_View AS
select `calendar_table`.`dt` AS `date`,
`calendar_table`.`year` AS `year`,
`calendar_table`.`quarter` AS `quarter`,
`calendar_table`.`month` AS `month`,
`calendar_table`.`day` AS `day`,
`calendar_table`.`dayofweek` AS `dayofweek`,
`calendar_table`.`monthName` AS `monthName`,
`calendar_table`.`dayName` AS `dayName`,
`calendar_viewtable`.`week` AS `week`,
`calendar_table`.`isWeekday` AS `isWeekday`
(CASE
WHEN CT.dayofweek = `1` THEN `0'
WHEN CT.dayofweek = `7` THEN `0`
WHEN CT.dt = PH.`Holiday Date` THEN `0`
ELSE `1`
END CASE ) AS IsWorkingDay
from `calendar_table` CT, `Public_Holidays` PH
2) I would also like to add another field which checks if the date in the calendar is the current date and so the populate new field with Yes, else No (I tried using a case statement this didnt work).
This is your code:
(CASE WHEN CT.dayofweek = `1` THEN `0'
WHEN CT.dayofweek = `7` THEN `0`
WHEN CT.dt = PH.`Holiday Date` THEN `0`
ELSE `1`
END CASE ) AS IsWorkingDay
This is what you want:
(CASE WHEN CT.dayofweek = 1 THEN 0
WHEN CT.dayofweek = 7 THEN 0
WHEN CT.dt = PH.`Holiday Date` THEN 0
ELSE 1
END) AS IsWorkingDay
There are two important differences:
END CASE is not valid syntax.
When you use backticks, you are saying that the string is an identifier -- typically a column reference. There are no columns called 0 or 1.
Two other issues with your query are the missing comma and the lack of join conditions. Your from clause should look more like:
from calendar_table ct left join
Public_Holidays ph
on ct.dt = ph.dt -- or whatever the right condition is
Hello I have this query that i am trying to execute and i keep getting this error "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.", Kindly help please.
DECLARE #NUMCOUNT BIT
Select #NUMCOUNT = (SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C') ) THEN 1 else 0 END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
)
IF #NUMCOUNT = '1'
begin
UPDATE R5REQUISITIONS
SET R5REQUISITIONS.REQ_STATUS = 'CP'
end
Ok, it sounds like what you actually want to do is update R5REQUISITIONS when there is no RQL_STATUS = 'C' in R5REQUISLINES, since you said you want to count the records where the RQL_STATUS is A and where it's A or C, and then do the update if the counts are the same.. You can greatly simplify this task with the following query:
UPDATE r5
SET r5.REQ_STATUS = 'CP'
FROM R5REQUISITIONS r5
WHERE NOT EXISTS (SELECT 1 FROM R5REQUISLINES r5q WHERE r5q.RQL_REQ = r5.REQ_CODE AND r5q.RQL_STATUS = 'C')
Your 'SELECT CASE' is returning more than 1 record, so it can't be assigned to #NUMBER. Either fix the sub-query to only return the record your looking for or hack it to return only 1 with a 'LIMIT 1' qualification.
I don't know what your data looks like so I can't tell you why your case subquery returns more records than you think it should.
Try running this and see what it returns, that will probably tell you wall you need to know:
SELECT
CASE WHEN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ('A')
) IN
(SELECT COUNT(R5REQUISLINES.RQL_REQ)
WHERE R5REQUISLINES.RQL_STATUS IN ( 'A','C')
)
THEN 1
ELSE 0
END AS NUMCOUNT1
FROM R5REQUISLINES JOIN
R5REQUISITIONS ON R5REQUISLINES.RQL_REQ = R5REQUISITIONS.REQ_CODE
GROUP BY R5REQUISLINES.RQL_REQ, R5REQUISITIONS.REQ_CODE,R5REQUISLINES.RQL_STATUS
If there is more than 1 row returned, that's where your problem is.
Does Invantive SQL support multiple condition in a single case statement? I the statement below, I did not get any results. Tried the same statement with only 1 condition (no cascade), this retrieved the expected result.
select prj.code
, prj.startdate
, prj.enddate
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}
and case
/* when (prj.enddate is null or prj.enddate >= sysdate)
then 'Y'
when (prj.enddate is not null and prj.enddate <= sysdate)
then 'N' */
when prj.startdate <= sysdate
then 'B'
end
= $P{P_PROJECT_ACTIVE_FROM}
I think you where clause is not correctly formulated. With Exact Online, a project either has:
option 1: no end date,
option 2: an end date in the past
option 3: or an end date in the future
The first part of the case handles option 1 and option 3. The second part handles option 2. So there is never an outcome of 'B' in the case.
To analyze such problems, I recommend include the case in the select clause and removing the filter. That gives you a view of the possible outcomes.
Example:
use 868056,102673
select prj.division
, prj.code
, prj.startdate
, prj.enddate
, case
when prj.enddate is null or prj.enddate >= sysdate
then 'Y'
when prj.enddate is not null and prj.enddate <= sysdate
then 'N'
when prj.startdate <= sysdate
then 'B'
end
indicator
from exactonlinerest..projects prj
where prj.code between $P{P_PROJECT_FROM} and $P{P_PROJECT_TO}
I have the following query:
select sie.invoicedate sie_invoicedate
, sie.Silitem sle_item
, sie.Silitemcode sle_itemcode
, sie.Silitemdescription sle_itemdescription
, sie.Silnetprice sle_netprice
, sie.Silquantity sle_quantity
, sie.Silunitprice sle_unitprice
, ctr.ctr_code ctr_code
, ctr.ctr_name ctr_name
, ctr.parent_code parent_code
, ctr.parent_name parent_name
, gdlsn.ssrserialnumber serialnumber
from SalesInvoicesExploded sie
join customers#inmemorystorage ctr
on ctr.ctr_id = sie.invoiceto
join GoodsDeliveryLineSerialNumbers gdlsn
on gdlsn.salesorderlineid = sie.silid
where sie.invoicedate >= '2016-01-01'
and sie.invoicedate < '2016-01-03'
order
by sie.invoicedate
How can I get the serial numbers only from the date range? In the debugger I see a lot of requests to Exact Online.
For now, there isn't a very good filter possibility to get the result you want.
The problem is that there is no way to perform the gdlsn.salesorderlineid = sie.silid filter on the data set unless the data sets have been fetched from the other side.
Only specific filters are executed server-side (like your invoicedate >= '2016-01-01'). This is quite a hard nut to crack from the program side.
It would work if you can specify a filter that can be determined on beforehand, like that the date in GoodsDeliveryLineSerialNumbers.Created always comes after the invoicedate. It would mean a significant performance improvement if you can narrow down the set based on that date.
I suggest to use something like this, if possible:
select sie.invoicedate sie_invoicedate
, sie.Silitem sle_item
, sie.Silitemcode sle_itemcode
, sie.Silitemdescription sle_itemdescription
, sie.Silnetprice sle_netprice
, sie.Silquantity sle_quantity
, sie.Silunitprice sle_unitprice
, ctr.ctr_code ctr_code
, ctr.ctr_name ctr_name
, ctr.parent_code parent_code
, ctr.parent_name parent_name
, gdlsn.ssrserialnumber serialnumber
from SalesInvoicesExploded sie
join customers#inmemorystorage ctr
on ctr.ctr_id = sie.invoiceto
join GoodsDeliveryLineSerialNumbers gdlsn
on gdlsn.salesorderlineid = sie.silid
where sie.invoicedate >= '2016-01-01'
and sie.invoicedate < '2016-01-03'
-- add the following line, use a date that for sure will yield the rows:
and gdlsn.created >= '2015-12-01'
--
order
by sie.invoicedate
I am getting an error when I execute the query below and I could not figure out the problem. Can anyone please help?
SELECT shifts.consultant_uid AS consultant_uid, shifts.status AS
status , shifts.pay_roll_no AS pay_roll_no, shifts.week_ending AS week_ending, shifts.shifts_date AS shifts_date, shifts.description AS description, shifts.units AS units, shifts.pay_rate AS pay_rate, shifts.charge_rate AS charge_rate, shifts.pay_amount AS pay_amount, shifts.charge_amount AS charge_amount, shifts.margin_amount AS margin_amount, shifts.client_name AS client_name, consultant.user_name AS consultant_name,
FROM a_shifts AS shifts
LEFT JOIN a_users AS consultant ON shifts.consultant_uid = consultant.user_uid
WHERE 1
AND week_ending >= '2013-03-17'
AND week_ending <= '2013-03-24'
You have an extra comma at the end of your field list in the SELECT clause:
... consultant.user_name AS consultant_name,
As a general word of advice, usually the actual error indicates where in the query the error was encountered. In most cases, the location it indicates is just after the problem, because it's the first thing the query encountered which it was unable to parse (in this case, the FROM keyword) that generates the error.
Remove , before FROM clause
SELECT .........consultant.user_name AS consultant_name,
FROM
try this
SELECT shifts.consultant_uid AS consultant_uid, shifts.status AS
status , shifts.pay_roll_no AS pay_roll_no, shifts.week_ending AS week_ending, shifts.shifts_date AS shifts_date, shifts.description AS description, shifts.units AS units, shifts.pay_rate AS pay_rate, shifts.charge_rate AS charge_rate, shifts.pay_amount AS pay_amount, shifts.charge_amount AS charge_amount, shifts.margin_amount AS margin_amount, shifts.client_name AS client_name, consultant.user_name AS consultant_name
FROM a_shifts AS shifts
LEFT JOIN a_users AS consultant ON shifts.consultant_uid = consultant.user_uid
WHERE 1
AND week_ending >= '2013-03-17'
AND week_ending <= '2013-03-24'