need query for this simple pulling data sql server - sql

I am pulling data, but how do I pull from all data listed till an specific range?
Something like
select *
from xxx
where processdate = '6/30/2013'
this gives me all data only for that specific day..
I want data for all the days till 6/30/2013
I am having problems telling the sql to pull data available till 6/30/2013
This will probably be something really simple, but I cant seem to find the function to tell it
Or will I need to look at the data and see the earliest date, and then do a range

It is quite plain, use <= or < if you want to exclude this day.
select * from xxx where processdate <= '6/30/2013'

Related

TERADATA Query for entire month

I wanted to know if there was a way to extract data from a date column pertaining to an entire month.
SEL * FROM DB.TBLNAME TABLE
WHERE TABLE.DATE_ BETWEEN DATE '01-02-2022' AND DATE ' 28-02-2022'
So in the above query(wrote it here as an example so hope i didnt make any typo mistake) im searching for all dates from the 1st to the 28th of february but wanted to know if there was a more elegant method as to select a month match eg. 02-2022.
I hope my question was detailed enough.
Thanks in advance,
For this you could do something like this to make it easier to develop and change as you would need it
SELECT
*
FROM DB.TBLNAME TABLE
WHERE 1=1
AND MONTH(TABLE.DATE) = 2
AND YEAR(TABLE.DATE) = 2022
This is how I would normally do it as you would then be able to write a python script to go with it or a param to dynamically pull the data you need for the month/year

Use SQL to ensure I have data for each day of a certain time period

I'm looking to only select one data point from each date in my report. I want to ensure each day is accounted for and has at least one row of information, as we had to do a few different things to move a large data file into our data warehouse (import one large Google Sheet for some data, use Python for daily pulls of some of the other data - want to make sure no date was left out), and this data goes from now through last summer. I could do a COUNT DISTINCT clause to just make sure the number of days between the first data point and yesterday (the latest data point), but I want to verify each day is accounted for. Should mention I am in BigQuery. Also, an example of the created_at style is: 2021-02-09 17:05:44.583 UTC
This is what I have so far:
SELECT FIRST(created_at)
FROM 'large_table'
ORDER BY created_at
**I know FIRST is probably not the best clause for this case, and it's currently acting to grab the very first data point in created_at, but just as a jumping-off point.
You can use aggregation:
select any_value(lt).*
from large_table lt
group by created_at
order by min(created_at);
Note: This assumes that created_at is a date -- or at least only has one value per date. You might need to convert it to a date:
select any_value(lt).*
from large_table lt
group by date(created_at)
order by min(created_at);
BigQuery equivalent of the query in your question
SELECT created_at
FROM 'large_table'
ORDER BY created_at
LIMIT 1

Oracle SQL Todays Date from Data

Apologies as I am extremely green within SQL.
I am currently trying to run the below:
SELECT *
FROM test.message
WHERE MESSAGESUBJECT LIKE '%test%'
AND MESSAGEDATE = CURRENT_DATE
However the results are not pulling back - figured this to be the fact the format of the data within the field currently looks like this:
07-MAR-19 08.13.53.00000000 PM
So the obvious solution is to get it to look at the start of the field - where I'm losing track is that I need the query for a job that's running daily so I cant use exacts. Anyone able to assist?
Thanks,
D.
SELECT *
FROM test.message
where MESSAGESUBJECT like '%test%'
and MESSAGEDATE BETWEEN TRUNC(CURRENT_DATE) AND TRUNC(CURRENT_DATE) + 86399/86400
This will get rows where the message date was any time today.
The 86399/86400 bit is the number of seconds in a day minus 1, so it makes the BETWEEN range cover the whole day and none of the next day (which TRUNC(CURRENT_DATE)+1 would do).
Instead of adding 86399/86400, some find it clearer to add a whole day and then subtract out an an INTERVAL of 1 second. That also works, as does:
AND MESSAGEDATE >= TRUNC(SYSDATE)
AND MESSAGEDATE < TRUNC(SYSDATE)+1 -- Thanks #WernfriedDomscheit
Using
AND TRUNC(MESSAGEDATE) = TRUNC(CURRENT_DATE)
is logically correct, but it makes it impossible for Oracle to use an index on MESSAGEDATE if there is one, which can rob you of your best possible performance. But if you have a function based index on TRUNC(MESSAGEDATE), then that last way is also a reasonable option.

BigQuery: SELECT in WHERE-clause with filter based on a value in the current row

I know the title is probably pretty stupid but I have a hard time phrasing it differently.
I have to use BigQuery at work atm for some report. BigQuery is connected to a Google Analytics view of ours. This gives us a dataset with 1 table for each day. The rows of the tables are user-sessions on our site, while columns have some information about the sessions.
The problem I have is the following:
I want to select sessions with transactions, but only if the user was referred to our site by a certain referrer in the last x days before the transaction happened. I'm only familiar with basic SQL and not with any advanced concepts. It's really frustrating to me because this would be a no-brainer with any proper programming language given a .csv of the data, but I'm lacking knowledge of the relevant concepts in SQL.
#standardSQL
SELECT
COUNT(*)
FROM
`dataset.ga_sessions_2017*`
WHERE
totals.transactions > 0 AND
fullVisitorId IN (SELECT
fullVisitorId
FROM
`dataset.ga_sessions_2017*`
WHERE
trafficSource.source = "xyz.com"
) AND
< date difference thing>
I could filter for the date difference like I did with the trafficSource (referrer). The problem for me is that while "xyz.com" is a static thing, I'd need to reference the date value of the current row I'm in. So the date by which I'd filter the 2nd SELECT would be dynamically changing from row to row. Can anyone guide me on how this is usually done? This seems like a thing that would come up often.
I'm not familiar with the GA tables specifically, but having written some wildcard queries in BigQuery before, I think what you're looking for can be done using the _TABLE_SUFFIX pseudo column:
CAST(_TABLE_SUFFIX AS INT64) >= 1217
Where 1217 is today's date in MMDD format minus 3 days, assuming the table names are _20171217, _20171218, etc. Otherwise you can just use REPLACE to remove underscores before casting to an int. There are also functions that will generate today's date for you if you needed this query to run automatically.
Also, I think the fullVisitorId business could be replaced with a simple WHERE trafficSource.source = "xyz.com" but it's hard to say for sure without being able to run the query myself.
So the full query would look something like this:
#standardSQL
SELECT
COUNT(*)
FROM
`dataset.ga_sessions_2017*`
WHERE
totals.transactions > 0 AND
trafficSource.source = "xyz.com" AND
CAST(_TABLE_SUFFIX AS INT64) >= 1217

How to select SQL Server data based on today's date and time?

I'm using SQL Server to COUNT some data which originates from a HTML table. I want to COUNT total rows in the database based on today's date. And then after the date hits tomorrow's date, to set the COUNT value back to zero and to restart the count back from the start.
Is there a query that can help me fetch data based on current date and time?
Something like this:
SELECT COUNT(data_id)
FROM table1
WHERE clock_time BETWEEN 000000 AND 235959
AND date_time IS TODAY's DATE (or something like that)
GROUP BY XXX
ORDER BY XXX
After the clock hits tomorrow's date, I want to reset the COUNT back to zero, and to start a new count back from 00:00:00.
I know there is NOW() query but as far as I know it only shows the date.
Someone told me I could use WHERE DATE(date)=CURDATE() but the SQL Server won't work with that.
Thank you...
You can use convert function like this
where convert(Date,date_time)= CONVERT(Date,GETDATE())
Need not to use time as you want today's data.
Try this one
SELECT COUNT(data_id) FROM table1
WHERE convert(date,date_time) = getdate()