I have to retrieve data from DB2 for current date, but I am not able to find correct solution for the same.
For example:
SELECT * FROM table WHERE date = current-date
(I know this is wrong, it's just an example.)
I need a correct where condition for DB2.
There are two way to get this:
There is a special system table
enter link description here
SELECT current date FROM sysibm.sysdummy1
So you would need to use a subquery:
SELECT * FROM table
WHERE date = (SELECT current date FROM sysibm.sysdummy1)
There is even a special register
enter link description here
This allows to avoid the subquery:
SELECT * FROM table WHERE date = CURRENT DATE
Related
So I am trying and struggling for few days to extend the schema with the custom groupby using something like this
I have a table with few fields like id, country, ip, created_at.
Then I am trying to get them as groups. For example, group the data based on date, hourly of date, or based on country, and based on country with DISTINCT ip.
I am zero with SQLs honestly. But I tried to play around and get what I want. Here's an example.
SELECT Hour(created_at) AS date,
COUNT(*) AS count
FROM session where CAST(created_at AS date) = '2021-04-05'
GROUP BY Hour(created_at)
ORDER BY date;
SELECT country,
count(*) AS count from (SELECT * FROM session where CAST(created_at AS date) <= '2021-05-12' GROUP BY created_at) AS T1
GROUP BY country;
SELECT country, COUNT(*) as count
FROM (SELECT DISTINCT ip, country FROM session) AS T1
GROUP BY country;
SELECT DATE(created_at) AS date,
COUNT(*) AS count
FROM session
GROUP BY DATE(created_at)
ORDER BY date;
Now I am struggling with two things.
How do I make the date as variables? I mean, if I want to group them for a particular date range/ or today's data hourly, or per quarter gap (more of configurable), how do I add the variables in Hasura's Raw SQL?
Also for this approach I have to add schema for each one of them? Like this
CREATE
OR REPLACE VIEW "public"."unique_session_counts_date" AS
SELECT
date(session.created_at) AS date,
count(*) AS count
FROM
session
GROUP BY
(date(session.created_at))
ORDER BY
(date(session.created_at));
Is there a way to make it more generalized? What I mean is, if it
was in Nodejs I could have done something like
return rawQuery(
`
select ${field} x, count(*) y
from ${table}
where website_id=$1
and created_at between $2 and $3
${domainFilter}
${urlFilter}
group by 1
order by 2 desc
`,
params,
);
In this case, based on whatever field and where clause I send, one query would do the trick for me. Can do something similar in hasura?
Thank you so much in advance.
How do I make the date as variables? I mean, if I want to group them for a particular date range/ or today's data hourly, or per quarter gap (more of configurable), how do I add the variables in Hasura's Raw SQL?
My first thought is this. If you're thinking about passing in variables via a GraphQL for example, the GraphQL would look something like:
query MyQuery {
unique_session_counts_date(where: {created_at: {_gte: "<start date here>", _lte: "<end date here>"}}) {
<...any fields, rollups, etc here...>
}
}
The underlying view/query would follow the group by and order by that you've detailed. Then you'd be able to submit a query of the graphql query and just pass in the pertinent parameters like the $1, $2, and $3 in the raqQuery call.
Also for this approach I have to add schema for each one of them?
The schema? The view? I don't think a view specifically would be required, if a multilevel select or similar query can handle it and perform then a view wouldn't particularly be needed.
That's my first stab at the problem. I'm going to try to work through this problem in a few hours via a Twitch stream # HasuraHQ if you can join, happy to walk through it live.
Dear StackOverflow Community,
as a New to DB2 ,i have a a query
may be its a very basic question for you, please share your knowledge.
i have a start date and End Date.
I need a list of each and every date in between.
Its ok with me ,if it creates a temp table no issue.
Thanks in Advance
You can generate the dates between start and end dates by using Recursive CTE expression. Try below code
with cte(your_columns,startdate,enddate)
as (select your_columns,startdate,enddate,startdate
as derDate
from yourTable
union all
select your_columns,startdate,enddate,derDate+1
from cte where
derDate<=endDate)
select * from cte
I've date stored as [27/Feb/2016:00:24:31 +0530].
I want date format in 27/Feb/2016 and also want to order by it.
I've tried this solution but it returns in form 2016-02-27 and also orders properly.
SELECT
TO_DATE( FROM_UNIXTIME( UNIX_TIMESTAMP( SUBSTR( time, 2, 11), 'dd/MMM/yyyy' ))) AS real_date,
url
FROM cleanned_logs
ORDER BY real_date ASC;
To get desired format i tried with date_format() function.It is not available in 1.2.1 so i switched to it from 1.0.1.
SELECT
DATE_FORMAT( FROM_UNIXTIME( UNIX_TIMESTAMP( SUBSTR(time,2,11),'dd/MMM/yyyy')), 'dd/MMM/yyyy') AS real_date,
url
FROM cleanned_logs
ORDER BY real_date ASC;
It gives me desired format but does not order properly.
UPDATED:
SELECT display_date,COUNT(url) FROM
(
SELECT SUBSTR(time,2,11) as display_date,url,UNIX_TIMESTAMP(SUBSTR(time,2,11),'dd/MMM/yyyy') as real_date FROM cleanned_logs order by real_date ASC
)b group by real_date;
Creates problem in grouping. Here hive expects real_date in select clause.
I think you're mixing up the formatting or display of data, with the underlying data itself. If the table stores a date as a string formatted in one manner, [27/Feb/2016:00:24:31 +0530] it's still a string, and strings sort differently than actual dates, timestamps, or numbers.
Ideally, you would store the date as a TIMESTAMP datatype. When you want to display it, use DATE_FORMAT, and when you want to sort it, use ORDER BY on the underlying data field. So if your field is of type TIMESTAMP called some_time, you could query as
SELECT DATE_FORMAT(some_time, 'dd/MMM/yyyy')
FROM some_table
WHERE some_condition
ORDER BY some_time DESC
If you're stuck with a string that's stored as a valid timestamp value, then you'll have to do more work, perhaps
SELECT SUBSTR(some_time, 2, 11)
FROM some_table
WHERE some_condition
ORDER BY unix_timestamp(SUBSTR(some_time,2,11), 'dd/MMM/yyyy'))
The second option displays the value as desired, and orders by a number -- a unix timestamp is just a number, but it has the same order as the date, so no need to cast that further to an actual date.
I have data that has a date column in the full format but would like the output to only represent the year.
eg: from
SELECT * FROM tablename;
I want something like:
SELECT * FROM tablename WITH 'newdatefield' as SELECT EXTRACT(YEAR FROM TIMESTAMP 'datefield');
I'm not great at SQL - I remember that something like this can be done but can't find how to do it again.
I think you are overthinking this one:
SELECT EXTRACT(YEAR from datefield), The, Other, Fields, You, Want FROM tableName
I tried below two ways they not working
Select * from Table
where SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013'
ALSO This is not working
Select * from Table
where SERV_DATE BETWEEN DATE('03/01/2013') AND
DATE('03/31/2013')
What should be the correct format ?
Did you tried what NealB suggested? The reason for not accepting 03/01/2013 as an entry date format is, that it is region dependent in the US it is March 1, 2013 an in the UK it is January 3, 2013. So without considering the local, it is not certain, what the actual date is.
"why would db2 give error on the same format and will go well when given different format" - Don't forget, that db2 is an old lady and as all old ladies she has peculiarities. You just get used to it and there will be an happy ending.
SELECT * FROM tableName WHERE date(modifiedBy_date) between '2017-07-28' AND '2017-08-01';
Works cool for DB2.
Select * from Table
where SERV_DATE BETWEEN DATE('2013-03-01') AND DATE('2013-03-31');
Worked for me.
Select * from Table
where (SERV_DATE BETWEEN '03/01/2013'AND
'03/31/2013')
Select * from Table
where (SERV_DATE BETWEEN '2013-03-01'AND
'2013-03-31')
select count(*) from TABLE where time_stamp BETWEEN DATE('2018-01-01') AND DATE('2018-01-31');
Here time_stamp is field name and copy your timestamp filed name instead of time_stamp.