I need to design report using Date parameters(needs be in UK date format)
Original date format: 2007-11-30 00:00:00.000
So, I am using CONVERT(Date, Start_Date, 103): 2007-11-30 in my query.
Now while designing report I get this error for the main dataset query.
Main dataset Query:
SELECT Col1, Col2, Start_Date, Target_Date, Col3
FROM Table
WHERE (Col1 IN (#Param1))
AND (Col2IN (#Param2))
AND (Start_Date IN (#Start_Date)) AND (Target_Date IN (#Target_Date))
Error: Conversion failed when converting date and/or time from character string
Parameter data type: Date/time
Start_Date dataset:
SELECT Col1, Col2, Start_Date
FROM Table
WHERE (Col1 IN (#Param1)) AND (Col2 IN (#Param2))
In query parameter I am using this code:
=iif(Parameters!Start_Date.Value is nothing,"1/1/1900",Parameters!Start_Date.Value)
O/P: No errors
Target_Date dataset(I want to include Target dates for which Start date is NULL so using below query):
SELECT Col1, Col2, Start_Date, Target_Date
FROM Table
WHERE (Col IN (#Param1)) AND (Col2 IN (#Param2)) AND (Start_Date = #Start_Date)
UNION ALL
SELECT NULL AS Expr1, NULL AS Expr2, NULL AS Expr3, Target_Date
FROM Table AS Table_1
WHERE (Department IN (#Param1)) AND (Col2 IN (#Param2)) AND (#Start_Date IS NULL)
O/P: No errors
Can someone suggest where I am going wrong?
Thanks,
ARK
Assuming that the Start_Date and Target_Date are of a string type then it might be possible you may have a value in there that's not a date format.
You can test for this by using the ISDATE() function:
SELECT DISTINCT Start_Date
FROM Table
WHERE ISDATE(Start_Date) = 0
SELECT DISTINCT Target_Date
FROM Table
WHERE ISDATE(Target_Date ) = 0
Then it is up to you how you would like to treat the invalid data. I would exclude them bu using the following:
SELECT Col1, Col2, Start_Date, Target_Date, Col3
FROM Table
WHERE ISDATE(Start_Date) = 1
AND ISDATE(Target_Date ) = 1
AND Col1 IN (#Param1)
AND Col2IN (#Param2)
AND Start_Date IN (#Start_Date)
AND Target_Date IN (#Target_Date);
Related
I have following data in a PostgreSQL table:
trial start_date end_date
1 20_12_2001 20_01_2005
The expected output is below:
trial start_date end_date Date[(start_end_date)] marker_start_end
1 20_12_2001 20_01_2005 20_12_2001 start
1 20_12_2001 20_01_2005 20_01_2005 end
Is there a way to calculate the additional two columns (Date[(start_end_date)], marker_start_end) without join, but a CASE expression
You can use a lateral join to turn two columns into two rows:
select *
from the_table t
cross join lateral (
values (t.start_date, 'start'), (t.end_date, 'end')
) as x(start_end_date, marker);
The UNION ALL solution might be faster though.
UNION ALL
select trial, start_date, end_date, start_date as date, 'start' marker_start_end from table1
union all
select trial, start_date, end_date, end_date as date, 'end' marker_start_end from table1
UNNEST with CASE
select trial, start_date, end_date,
case when a.num = 1 then start_date else end_date end date,
case when a.num = 1 then 'start' else 'end' end marker_start_end from
(
select trial, start_date, end_date,
unnest(array[1,2]) num from table1
) a
Hidden JOIN (but still join)
select
trial,
start_date,
end_date,
case when a.num = 1 then start_date else end_date end date,
marker_start_end
from table1, (values(1,'start'),(2, 'end')) a(num,marker_start_end)
Db fiddle
I have a table in which i have to find the maximum date for each unique EMPid & testid
below is the input table and expected output
I tried with correlated sub query but that didn't work.
Any quick way to update the table with max date.
You can use a common-table-expression and the OVER clause with PARTITION BY:
WITH CTE AS
(
SELECT EmpId, [Hall Id], testId, Date, [Max date],
MaxDate = MAX(Date) OVER (PARTITION BY EmpId, testId)
FROM dbo.TableName
)
UPDATE CTE SET [Max date] = MaxDate
If you want to see what will happen replace UPDATE with SELECT * FROM.
You can use a CTE to select all maximum dates and join this with your original data like this:
WITH MaxDates AS (
SELECT empid
, testid
, MAX(Date) AS MaxDate
FROM table
GROUP BY empid
, testid
)
SELECT table.*
, MaxDate
FROM table
INNER JOIN MaxDates ON table.empid = MaxDates.empid AND table.testid = MaxDates.testid
I'm trying to get the output of queries within the with clause of my final query as csv or some sort of text files. I only have query access, I'm not allowed to create tables for this database. I have a set of queries that do some calculations on a data set, another set of queries that compute on the previous set and yet another that calculates on the final set. I don't want to run all of it as three seperate queries because the results from the first two are actually in the last one.
WITH
Q1 AS(
SELECT col1, col2, col3, col4, col5, col6, col7
FROM table1
),
Q2 AS(
SELECT AVG(col1) as col1Avg, MAX(col1) as col1Max, col2, col3,col4
FROm Q1
GROUP BY col2, col3, col4
)
SELECT
AVG(col1AVG), col3
FROM
Q2
GROUP BY col3
I would like the results from Q1, Q2 and the final select statement as preferably 3 csv files but I could live with all of it in one csv file. Is this possible?
Thanks!
Edit: Just to clarify, the columns from the queries are very different. I'm definitely pulling more columns from my first query than my second. I've edited the above code a bit to make this more clear.
To combine all the results together you'd use UNION ALL, but the number and data types of the columns must match.
select col1, col2, col2
from blah
union all
select col1, col2, col2
from blah2
union all
... etc
You can reference CTE's in there of course ...
with
cte_1 as (
select ... from ...),
cte_2 as (
select ... from ... cte_1),
cte_3 as (
select ... from ... cte_2)
select col1, col2, col2
from cte_1
union all
select col1, col2, col2
from cte_2
union all
select col1, col2, col2
from cte_3
If your final output is a csv then it looks like you have multiple row formats in there -- checksums? If so, in the queries that you union all together you might like to combine all the columns from each query into one string ...
with
cte_1 as (
select ... from ...),
cte_2 as (
select ... from ... cte_1),
cte_3 as (
select ... from ... cte_2)
select col1||','||col2||','||col2
from cte_1
union all
select col1||','||col2
from cte_2
union all
select col1
from cte_3
I try to select from one table like this:
SELECT * FROM table1 where id=311
and date BETWEEN '2012-09-01' And '2012-09-09'
and col2='a'
and (col3 ='m'
or col3 ='n'
or col3=' ' )
ORDER BY date
In the table I have situation where col3 has values 'm', 'n' or null, but this select doesn't return rows where col3 has a null value
NULL is something other than ' ' use col3 is null
There is three-valued logic in database:
TRUE, FALSE, NULL(unknown)
col3 ='' is a true condition,
col3 is null is an unknown condition.
They are different.
so you must use
col3 is null
You may reference Wikipedia's "NULL" entry.
NULL is not equal to 'm' , 'n' or ' ' . NULL isn't even equal to NULL. Use:
SELECT * FROM table1 where id=311
and date BETWEEN '2012-09-01' And '2012-09-09'
and col2='a'
and (col3 ='m'
or col3 ='n'
or col3 IS NULL)
ORDER BY date
SELECT *
FROM table1
where
id=311
and date BETWEEN '2012-09-01' And '2012-09-09'
and col2='a'
and (
col3 ='m'
or col3 ='n'
or col3 is null
)
ORDER BY date
Here is a nice trick to avoid the OR list, and to also avoid the OR IS NULL:
SELECT *
FROM table1
WHERE id=311
AND zdate BETWEEN '2012-09-01' AND '2012-09-09'
AND col2='a'
AND COALESCE(col3, 'm') IN ('m', 'n' )
ORDER BY zdate
;
My question: I want the records without duplicate, in the same table and in multiple tables? How can I proceed to do this in SQL?
Let me explain what I have tried:
Select distinct Col1, col2
from Table
where order id = 143
Output
VolumeAnswer1 AreaAnswer1 heightAnswer1
VolumeAnswer2 AreaAnswer1 heightAnswer2
VolumeAnswer3 AreaAnswer1 heightAnswer2
Expected Output
It shows the duplicate for the second table, but I need the output to be like:
VolumeAnswer1 AreaAnswer1 heightAnswer1
VolumeAnswer2 heightAnswer2
VolumeAnswer3
I need the same scenario for multiple tables, same duplicate I found for joins also. If it cannot be handled in SQL Server, how can we handle it in .Net? I used multiple select but they used to change it in single select. Each and every column should bind in dropdownlist...
Something like this might be a good place to start:
;with cte1 as (
Select col1, cnt1
From (
Select
col1
,row_number() over(Partition by col1 Order by col1) as cnt1
From tbltest) as tbl_sub1
Where cnt1 = 1
), cte2 as (
Select col2, cnt2
From (
Select
col2
,row_number() over(Partition by col2 Order by col2) as cnt2
From tbltest) as tbl_sub2
Where cnt2 = 1
), cte3 as (
Select col3, cnt3
From (
Select
col3
,row_number() over(Partition by col3 Order by col3) as cnt3
From tbltest) as tbl_sub3
Where cnt3 = 1
)
Select
col1, col2, col3
From cte1
full join cte2 on col1 = col2
full join cte3 on col1 = col3
Sql Fiddle showing example: http://sqlfiddle.com/#!3/c9127/1