Is there other way to rewrite/improve this query, trying to make it with less typo and if possible improve performance:
Select
(Select Sum(value) from table1
where code = 'B2'
and date between DATE '2017-01-01'
and DATE '2017-03-31')
+
(Select Sum(value) from table2
where code = 'B2'
and date between DATE '2017-04-01'
and DATE '2017-04-30')
I also tried with union all but this still is not what I need:
Select Sum(value)
from (Select code, value from table1
Where date between DATE '2017-01-01'
and DATE '2017-03-31')
union all
(Select code, value from table1
Where date between DATE '2017-04-01'
and DATE '2017-04-30')
where code = 'B2'
Thanks
Your first query is fine . . . assuming you have a from dual at the end.
For performance, you want indexes on table1(code, date, value) and table2(code, date, value). Note that the order of the columns in the indexes is important.
If, with typo you mean that you have the criteria code = 'B2' twice in your query, you can move it to your from clause. Anyway, be aware that a subquery can return NULL. Use NVL (or COALESCE) to deal with this.
select
nvl((select sum(value) from table1
where code = x.code and date between date '2017-01-01' and date '2017-03-31'), 0)
+
nvl((select sum(value) from table2
where code = x.code and date between date '2017-04-01' and date '2017-04-30'), 0)
from (select 'B2' as code from dual) x;
Related
I ran into a simple bit of code, but I've been wracking my brains ever since.
It's either counter-intuitive, or I'm missing something, either simple or fundamental. No idea.
WITH T1 AS
(
SELECT TO_DATE ('14/11/19', 'DD/MM/YY') as SomeDate FROM DUAL
UNION
SELECT TO_DATE ('28/11/19', 'DD/MM/YY') as SomeDate FROM DUAL
),
T2 AS
(
SELECT TO_DATE ('14/11/19', 'DD/MM/YY') as SomeDate FROM DUAL
UNION
SELECT TO_DATE ('28/11/19', 'DD/MM/YY') as SomeDate FROM DUAL
)
SELECT * FROM T1 WHERE T1.SomeDate >= TO_DATE ('05/11/19','DD/MM/YY')
AND NOT EXISTS
(SELECT 1 FROM T2
WHERE T2.SomeDate >= TO_DATE ('05/11/19','DD/MM/YY')
AND T2.Somedate < T1.Somedate
);
Excluding all other conditions, the basic version does something like the code above.
The subquery returns all rows where SomeDate = 14/11/19 since those are the ones that are greater than the 5th but strictly lower than the 28th.
And Not Exists should evaluate to false when rows are returned.
So by my logic, that means that excluding all rows with 14/11/19, the code above should return 28/11/19.
But it returns '14/11/2019'.
Could someone please explain what I'm missing?
Thank you very much.
I would write the query like this so I can read it more easily:
WITH T1 AS (
SELECT DATE '2019-11-14' as SomeDate FROM DUAL UNION ALL
SELECT DATE '2019-11-28' as SomeDate FROM DUAL
),
T2 AS (
SELECT DATE '2019-11-14' as SomeDate FROM DUAL UNION ALL
SELECT DATE '2019-11-28' as SomeDate FROM DUAL
)
SELECT *
FROM T1
WHERE T1.SomeDate >= DATE '2019-11-05' AND
NOT EXISTS (SELECT 1
FROM T2
WHERE T2.SomeDate >= DATE '2019-11-05' AND
T2.Somedate < T1.Somedate
);
I'm not sure where your confusion is. There are two possibilities for T1.SomeDate, 2019-11-14 and 2019-11-18.
Only the earlier date (2019-11-14) has no rows in T2 that are less than the date. Hence, NOT EXISTS matches this date.
I suspect that you are confused by the inequality in the subquery.
I have this query
SELECT DISTINCT fc,cohort, value
FROM test.input_test
WHERE value IS NULL AND date BETWEEN '2019-07-01' AND '2019-09-30';
I would like to select all rows where the value is null in the whole data range (not only on some specific dates).
One method is window functions:
SELECT DISTINCT fc, cohort, value
FROM (SELECT it.*,
SUM(CASE WHEN it.value IS NULL THEN 1 ELSE 0 END) OVER () as null_cnt
FROM test.input_test it
WHERE it.date >= '2019-07-01' AND it.date < '2019-10-01'
) it
WHERE null_cnt = 0;
Note that I changed the date comparisons to avoid BETWEEN. Here is a good blog explaining why BETWEEN is a bad idea with dates. Most of the points are relevant even if you do not use SQL Server.
How about using an exists clause:
SELECT DISTINCT fc, cohort, value
FROM test.input_test
WHERE
date BETWEEN '2019-07-01' AND '2019-09-30' AND
NOT EXISTS (SELECT 1 FROM test.input_test
WHERE date BETWEEN '2019-07-01' AND '2019-09-30' AND
value IS NOT NULL);
As #Gordon mentioned, if you want to include the entire months of July, August, and September 2019, then the date range you should use is:
WHERE date >= '2019-07-01' AND date < '2019-10-01'
Could you possibly try below :
SELECT DISTINCT IT.fc, IT.cohort, IT.value
FROM test.input_test AS IT
LEFT JOIN
(
SELECT fc,cohort, value
FROM test.input_test
WHERE value IS NOT NULL AND date BETWEEN '2019-07-01' AND '2019-09-30'
) AS QUERY_2 ON IT.fc = QUERY_2.fc AND IT.cohort = QUERY_2.cohort AND IT.value = QUERY_2.value
WHERE QUERY_2.fc IS NULL
I am trying to write a statement and just a bit puzzled what is the best way to put it together. So I am doing a UNION on a number of tables and then from there I want to produce as the output a count for the UserID within that day.
So I will have numerous tables union such as:
Order ID, USERID, DATE, Task Completed.
UNION
Order ID, USERID, DATE, Task Completed
etc
Above is layout of the table which will have 4 tables union together with same names.
Then statement output I want is for a count of USERID that occurred within the last 24 hours.
So output should be:
USERID--- COUNT OUTPUT-- DATE
I was attempting a WHERE statement but think the output is not what I am after exactly, just thinking if anyone can point me in the right direction and if there is alternative way compared to the union? Maybe a joint could be a better alternative, any help be appreciated.
I will eventually then put this into a SSRS report, so it gets updated daily.
You can try this:
select USERID, count(*) as [COUNT], cast(DATE as date) as [DATE]
from
(select USERID, DATE From SomeTable1
union all
select USERID, DATE From SomeTable2
....
) t
where DATE <= GETDATE() AND DATE >= DATEADD(hh, -24, GETDATE())
group by USERID, cast(DATE as date)
First, you should use union all rather than union. Second, you need to aggregate and use count distinct to get what you want:
So, the query you want is something like:
select count(distinct userid)
from ((select date, userid
from table1
where date >= '2015-05-26'
) union all
(select date, userid
from table2
where date >= '2015-05-26'
) union all
(select date, userid
from table3
where date >= '2015-05-26'
)
) du
Note that this hardcodes the date. In SQL Server, you would do something like:
date >= cast(getdate() - 1 as date)
And in MySQL
date >= date_sub(curdate(), interval 1 day)
EDIT:
I read the question as wanting a single day. It is easy enough to extend to all days:
select cast(date as date) as dte, count(distinct userid)
from ((select date, userid
from table1
) union all
(select date, userid
from table2
) union all
(select date, userid
from table3
)
) du
group by cast(date as date)
order by dte;
For even more readability, you could use a CTE:
;WITH cte_CTEName AS(
SELECT UserID, Date, [Task Completed] FROM Table1
UNION
SELECT UserID, Date, [Task Completed] FROM Table2
etc
)
SELECT COUNT(UserID) AS [Count] FROM cte_CTEName
WHERE Date <= GETDATE() AND Date >= DATEADD(hh, -24, GETDATE())
I think this is what you are trying to achieve...
Select
UserID,
Date,
Count(1)
from
(Select *
from table1
Union All
Select *
from table2
Union All
Select *
from table3
Union All
Select *
from table4
) a
Group by
Userid,
Date
I have following table tbl in database and I have dynamic joining date 1-1-2012 and I want this date is between (Fall and spring) or (spring and summer) or (summer and fall).I want query in which i passed only joining date which return semestertime and joining date in Oracle.
Semestertime joiningDate
Fall 10-13-2011
Spring 2-1-2012
Summer 6-11-2012
Fall 10-1-2015
If I understand your question correctly:
SELECT *
FROM your_table
WHERE joiningDate between to_date (your_lower_limit_date_here, 'mm-dd-yyyy')
AND to_date (your_upper_limit_date_here, 'mm-dd-yyyy`);
What about something like that:
select 'BEFORE' term,
t."Semestertime", to_char(t."joiningDate", 'MM-DD-YYYY')
from (
select tbl.*, rownum rn from tbl where tbl."joiningDate" < to_date('1-1-2012','MM-DD-YYYY')
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- your reference date
order by tbl."joiningDate" desc) t
where rn = 1
union all
select 'AFTER' term,
t."Semestertime", to_char(t."joiningDate", 'MM-DD-YYYY')
from (
select tbl.*, rownum rn from tbl where tbl."joiningDate" > to_date('1-1-2012','MM-DD-YYYY')
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-- your reference date
order by tbl."joiningDate" asc) t
where rn = 1
This will return the "term" before and after a given date. You will probably have to adapt such query to your specific needs. But that might be a good starting point.
For example, given your business rules, you might consider using <= instead of <. You you might require to have the result displayer a column instead of rows. Bu all of this shouldn't be too had to change.
As an alternate solution using CTE and sub-queries:
with testdata as (select to_date('1-1-2012','MM-DD-YYYY') refdate from dual)
select v.what, tbl.* from tbl join
(
select 'BEFORE' what, max(t1."joiningDate") d
from tbl t1
where t1."joiningDate" < to_date('1-1-2012','MM-DD-YYYY')
union all
select 'AFTER' what, min(t1."joiningDate") d
from tbl t1
where t1."joiningDate" > to_date('1-1-2012','MM-DD-YYYY')
) v
on tbl."joiningDate" = v.d
See http://sqlfiddle.com/#!4/c7fa5/15 for a live demo comparing those solutions.
got a table with dates and prices.
Date Price
2012-01-01 25
2012-01-05 12
2012-01-10 10
Is there some kind of function that lets me find what the current price where at '2012-01-07'? Without me knowing of the other dates.
Pseudoquery: select price where currentprice('2012-01-07')
Thanks!
MySQL:
select price from your_table
where date <= '2012-01-07'
order by date desc
limit 1
SQL Server:
select top 1 price from your_table
where date <= '2012-01-07'
order by date desc
If you don't have use of ROW_NUMBER(), and want a generic solution, you need to join on a sub-query.
Get the date you want, then get the data for that date.
SELECT
*
FROM
yourTable
INNER JOIN
(
SELECT MAX(yourDate) AS maxDate FROM yourTable WHERE yourDate <= #dateParameter
)
AS lookup
ON yourTable.yourDate = lookup.maxDate
select price
from table1 t
where t.date = ( select max(t2.date)
from table1 t2
where t2.date <= '2012-01-07' )
Note this is not the copy&paste answer, as we're not not knowing what is the datatype for your date column.