The following sql returns correct data formatted just as needed, except when no data is returned.
Desired result -- using the coalesce on ename--
is to return 'none' when no data is returned.
What is it I am doing wrong on the coalesce? Why does it not return 'none' when no data is returned?
When no data is returned, we get all the column names and all are null.
all pointers and suggestions appreciated.
select
ltrim(right(convert(varchar(20), tstart, 100), 7))
as 'START TIME',
ltrim(right(convert(varchar(20), tend, 100), 7))
as 'END TIME',
coalesce(vb.tname, 'none') as TITLE,
tr.description as LOCATION
from vwbooks vb
join troom tr
on vb.room = tr.id
where vb.room in(select id
from tblroom
where building = 4971
and vb.tstart >= floor(cast(getdate() as float))
and vb.tstart < ceiling(cast(getdate() as float))
and datepart(hour, vb.tstart) between 6 and 18
You are returning 0 rows. You are interpreting these as null. You need to understand why nothing is returned. The coalesce will work when it gets a row!
Related
In MySQL Database I have a table ABC that consists of a column 'LastDate'.
LastDate which has datatype as DATETIME. The default value for this 'NULL'
I need to write a query for the table which would
Return '1' in these cases.
1) If DATEDIFF(CURRENT_TIME,LastDate) is >15 or if DATEDIFF(CURRENT_TIME,LastDate) is
NULL(i.e defaultVal).
return '0' if DATEDIFF(CURRENT_TIME,LastDate) is <15.
I tried to write an SQL query for this but was unable to do it. Please help me write this Query. Thanks in advance.
You can be explicit about your logic:
select t.*,
(case when DATEDIFF(CURRENT_TIME, LastDate) > 15 or
LastDate is null
then 1 else 0
end) as flag
from t;
This can be simplified to:
select t.*,
coalesce(DATEDIFF(CURRENT_TIME, LastDate) <= 15, 1) as flag
from t;
I have a column(varchar) with date values, I need to find those dates which are expiring in next 30 days.
ExpiringDate
===================
20171208,
20171215,samples
20171130,tested
N/A
No
(empty row)
So, First I need to get values before comma. On the resultset, I need to filter out rows that has only numbers(no 'N/A' or 'No' or empty rows) & then I need to filter those dates which are expiring in next 30 days.
Edited
I have tried the following & resultset seems to be inappropriate
SELECT
DocName,
CategoryName,
AttributeName,
CAST(SUBSTRING_INDEX(AttributeValue, ',', 1) AS DATE) AS ExpiredDate
FROM myDB
WHERE (AttributeName = 'Date of last vessel OVID' OR AttributeName = 'Next Statutory docking' OR
AttributeName = 'Last statutory docking') AND AttributeValue LIKE '%[^0-9]%' AND
DATEDIFF(now(), AttributeValue) <= 30;
Because you are not only storing dates as text, but mixing those dates with entirely non date information, this complicates things. In this case, we can do two checks, one to ensure that the record starts with an actual expected date, and the second to make sure that the date diff is within 30 days from now.
SELECT ExpiringDate
FROM
(
SELECT ExpiringDate
FROM yourTable
WHERE ExpiringDate REGEXP '^[0-9]{8}'
) t
WHERE
DATEDIFF(LEFT(ExpiringDate, 8), NOW()) BETWEEN 0 AND 30;
Note that I use a subquery to first remove rows that do not even have a parseable date. The reason for this is that DATEDIFF will error out if not passed valid dates for both parameters.
Demo
I'm working in a solution in SSRS that it's driving me crazy, I will explain it a bit before shows you my problem:
Select 25 data values from a table with analog input data (from current, voltage, pressure, etc. tags) using parameters #startDate = yeterday 6am and #EndDate = today 6am. Now we have a table with 25 values from 6am to 6am from different tagID's.
My problem starts when just one tagId of 16, it's showing me in the SSRS presentation values 0.0000 when in the Sql output shows me -0.00548...
For practical purpouses I will just use 2 tagIds (MSF_PDI_003, MSF_PDI_004)
Here the SQL Query:
declare #startDate datetime2 = '2017-04-19 11:00',
#endDate datetime2 = '2017-04-20 11:00';
SELECT InstaTime,
MSF_PDI_003,
MSF_PDI_004
FROM (
SELECT
DATEADD(HH,-5,H.time) AS InstaTime,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_003.curval'
Then H.value
ELSE 0
END) as MSF_PDI_003,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_004.curval'
Then H.value
ELSE 0
END) as MSF_PDI_004
FROM hour H
INNER JOIN tag T
ON T.tagId = H.tagId
WHERE T.tagName IN
('analog.MSF_PDI_003.curval', 'analog.MSF_PDI_004.curval')
AND H.time >= #startDate
And H.time <= #endDate
GROUP BY time
) QueryData
order by InstaTime desc
And this the result of the query:
SQL QUERY RESULT IN SQL-SERVER
And these are the expressions that I'm using in the textboxes of the tablix in the SSRS (I used a test dataset for this query: summary_prueba)
=Fields!InstaTime.Value
=Fields!MSF_PDI_003.Value
=Fields!MSF_PDI_004.Value
And here the results of the table in SSRS:
CLICK IMAGE: SAMPLE2 SRSS
What could be the problem around here?
* Is the Only one with negative values, could be forcing a round? Something about a different format I should use?
Driving me nuts and can't find what's wrong. I tried to change the format and is the same, can you guys please help me? Because this thing doesn't let me sleep at night, I'm starting to have nightmares lol.
I'm using SQL Server 2012, and for the report MS Visual Studio Shell 2010.
Let's start by consolidating your query into one statement, instead of the UNION and separate SELECT SUM(...) that you have now:
declare #startDate datetime2 = '2017-04-18 11:00',
#endDate datetime2 = '2017-04-19 11:00';
SELECT InstaTime,
MSF_PDI_003,
MSF_PDI_004
FROM (
SELECT
DATEADD(HH,-5,H.time) AS InstaTime,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_003.curval'
Then H.value
ELSE 0
END) as MSF_PDI_003,
SUM(
CASE
WHEN t.tagName = 'analog.MSF_PDI_004.curval'
Then H.value
ELSE 0
END) as MSF_PDI_004
FROM hour H
INNER JOIN tag T
ON T.tagId = H.tagId
WHERE T.tagName IN
('analog.MSF_PDI_003.curval', 'analog.MSF_PDI_004.curval')
AND H.time >= #startDate
And H.time <= #endDate
GROUP BY InstaTime
) QueryData
order by InstaTime desc
Now, if there is no matching 004 or 003 value, they will report as zero instead of being blank. Try this, and see whether this matches what you need.
All right I found a solution but not the problem, because I still don't understand why if in the sql server I can see data and in the QueryDesigner of SSRS I still have 0.00 values.
This is a relational DB, I use 2 tables, from the same database
DB: Timeseries
Table1: hour, from here I use value,time,tagId
Table2: tag, from here I use tagName,tagId
Both have tagId in common, but I just realized that exaclty just this signal MSF_PDI_004 it's not related to the tagName in the table tag.(Have to be an error when created that signal)
I changed the Query and instead of use the tagName I used the tagId, and everything was fine, I could see the same values in SSRS than I was seeing in the SQL SERVER.
Still I think its strange, because if the query works on SQL SERVER, why should not be transparent for the SSRS?
I hope somebody can explain me this, thank you for the ones who took time for read and help.
I have a table that contains a column (created) that stores a unix timestamp when that item has been created.
Now I want to COUNT() all items that have been created on a weekday (Monday to Friday), compared to all items that have been created on the weekened (Saturday and Sunday).
My query is:
SELECT
IF (WEEKDAY(FROM_UNIXTIME(`created`)) >= 0 AND WEEKDAY(FROM_UNIXTIME(`created`)) >= 4) THEN COUNT(*) AS `weekday`,
IF (WEEKDAY(FROM_UNIXTIME(`created`)) <= 5) THEN COUNT(*) AS `weekend`
FROM `mytable`
But the error I get is
#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 ') THEN COUNT(*), IF (WEEKDAY(FROM_UNIXTIME(created)) <= 5) THEN COUNT(*)' at line 3
Any help is highly appreciated.
You do not appear to be using the correct syntax. http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_if
Quoted from the above url,
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.".
I believe this would be the correct form for your query,
SELECT
DATE(FROM_UNIXTIME(`created`)) AS `date`,
IF (WEEKDAY(FROM_UNIXTIME(`created`)) >= 0 AND WEEKDAY(FROM_UNIXTIME(`created`)) <= 4, COUNT(*), 0), --WeekDay Count
IF (WEEKDAY(FROM_UNIXTIME(`created`)) >= 5 AND WEEKDAY(FROM_UNIXTIME(`created`)) <= 6, COUNT(*), 0) --Weekend Count
FROM mytable
I have found this link as well to the weekday function. Which leads me to believe your ranges were not correct to begin with.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_weekday
Here is the query I'm using to gather daily values from three different "addresses", which reference different sensors in our system.
SELECT DISTINCT
LEFT(changes.date_time, 6) AS 'date',
changes.address AS 'address',
(CASE WHEN changes.address IN (18) THEN - MAX(changes.value * types.multiplier) ELSE MAX(changes.value * types.multiplier) END) AS 'value'
FROM sensor.changes
INNER JOIN sensor.types ON changes.type = types.idx
WHERE
changes.address IN (1, 4, 8)
AND changes.date_time >= '12110100000000'
AND changes.date_time <= '13050100000000'
GROUP BY LEFT(changes.date_time, 6),
changes.address
ORDER BY changes.idx;
The first date contains the following values, with subsequent dates carrying the same addresses:
date address value
130203 1 0.0160
130203 4 0.1220
130203 8 -0.0070
I want to combine these three values, address 1+4+8, into a single row for that date. I've attempted a SUM on the "value" column, but that results in an SQL error: #1111 - Invalid use of group function. Taking out the second GROUP BY column results in one value per date, but the value is not a sum of those values. Using a second query to format the result is OK, but I would prefer if the math was done in the first query because the database server is not local.
EDIT: to clarify what I need as the result:
date address value
130203 1 0.131
The integer under the address column in this case doesn't matter.
You should add a grouping clause.
SELECT Resdate,SUM(Res.address),SUM(Res.value)
FROM
(
SELECT DISTINCT
LEFT(changes.date_time, 6) AS 'date',
changes.address AS 'address',
(CASE WHEN changes.address IN (18) THEN - MAX(changes.value * types.multiplier) ELSE MAX(changes.value * types.multiplier) END) AS 'value'
FROM sensor.changes
INNER JOIN sensor.types ON changes.type = types.idx
WHERE
changes.address IN (1, 4, 8)
AND changes.date_time >= '12110100000000'
AND changes.date_time <= '13050100000000'
GROUP BY LEFT(changes.date_time, 6),
changes.address
ORDER BY changes.idx
) AS Res
GROUP BY Res.Date