Amazon AMC SQL Queries - How to declare a variable - sql

Does anyone know how to declare a date a variable in SQL for Amazon Marketing Cloud? The query UI for AMC uses syntax specific to Amazon and I cannot find this in the documentation or instructional queries.
I'm trying to do something like this, adding a couple date parameters I can then re-use across a few different tables and date fields:
declare start_date date constant cast('2022-6-1' as date)
select
click_date,
sum(clicks) as clicks
from dsp_clicks
where click_date > start_date
group by click_date

I’m learning this db myself and just stumbled upon your question here. It’s probably too late and I’m not great at general SQL as this is my first time really trying to learn it for work but it should go something like this if you’re just looking to gain a useable or custom date:
CAST(campaign_start_date AS DATE)>CAST(‘2022-02-02’ AS DATE)
I hope this helps.

Related

EXTRACT vs CAST in SQL

This question is mostly for an "Optimizing Code" kind of purpose.
So, in SQL, specifically Google BigQuery, there are 2 ways to transform a timestamp into a date or an hour. Using EXTRACT() or CAST().
There might be more ways to do so, but at least those are the ones I know of currently.
CAST() example:
SELECT
CAST(tb.timestamp_field AS DATE) AS date_field, COUNT(*)
FROM
database.table tb
GROUP BY
CAST(tb.timestamp_field AS DATE)
EXTRACT() example:
SELECT
EXTRACT(DATE FROM tb.timestamp_field) AS date_field, COUNT(*)
FROM
database.table tb
GROUP BY
EXTRACT(DATE FROM tb.timestamp_field)
Both methods work for what I'm trying to do, but I would like to know which one would be considered as a "best practice". Or maybe the whole questions could be silly, like asking which is better: "4+3-2" or "4-2+3". Which would be basically the same.
my two cents -
Cast - preferable. Because lot of other big data tools uses similar format so if you have to ever migrate to another big data, you can migrate smoothly.
Also, in your SQL, cast is a direct operation so i think this can be faster. I tested this using one record and this sql took 0.011 sec.
SELECT cast( TIMESTAMP "2018-12-25 05:30:00" as date)
Extract - The SQL you are using is not official - there is nothing like EXTRACT(DATE from timestamp_col). Good way is to use what #
Mikhail Berlyant mentioned. but your sql is working - so i think internally, google big query engine is converting the timestamp to date and the removing time part. So its a two part operation and heavily depends on internal conversion. A little unreliable i think. Also, i think you can run both your query and check performance because perf depends on lot of factors like - environment, amount of data, optimized table, etc.
Also, below SQL took like 0.012 sec. (not a great perf indicator though)
SELECT EXTRACT(DATE FROM TIMESTAMP "2018-12-25 05:30:00")
You can refer to below link for more on EXTRACT or DATE -
https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract

Date/Timestamp column in DBeaver (SQL Client) but I only want the date (using a CSV file)

I'm fairly rookie when it comes to SQL but as of recent I've been having to use it in it's basic form to do very simple tasks like only recalling relevant columns from a table etc.
I'm currently using DBeaver as my SQL Client and for this example I'm tapping straight into a CSV, no problems there. The data I'm working with is transaction data and the table is structured as follows
My problem is that the data is in 15 minute intervals whereas I need a value per day per store per metric (I.E. in the image example, it would return "Site" = 101 - "Metric" = FOODSER3 - "Date" = 2020-08-09 - "Value" = 6.0000)
Firstly, is this possible
Secondly, if so then please could someone let me in on the secret of how and maybe an explanation as to what the resolution is and why so that I can really understand what's going on.
I'm fairly proficient in Javascript and VBA, but so far SQL defeats me at every hurdle.
The structure of such a query is aggregation. Date/time functions are notoriously dependent on a database, but the idea is:
select cast(date as date), site, metric, sum(value)
from t
group by cast(date as date), site, metric;

Where in my query to place the CONVERT to convert DateTime to Date

Just learning SQL and I've searched many options about converting a DateTime into a Date, and I do not want current date. It's a super simple query from this website: https://sqlzoo.net/wiki/Guest_House_Assessment_Easy
SELECT booking_date, nights
FROM booking
WHERE guest_id=1183
But the output is with the timestamp and I just want the date. I've searched so many forums and tried all their suggestions, including this:
SELECT CONVERT(varchar(10), <col>, 101)
So I've done:
SELECT CONVERT(varchar(10), booking_date,101), nights
FROM booking
WHERE guest_id=1183
But I'm getting syntax errors. This is probably so simple and you'll all think me an idiot, but I'd greatly appreciate help. It's driving me nuts.
When I fiddled about at your sqlzoo link I got the error
execute command denied to user 'scott'#'localhost' for routine 'gisq.to_date'`.
When I googled gisq.to_date I got this link https://sqlzoo.net/wiki/Format_a_date_and_time
Which has examples of how this dialect represents dates. See if you can work it out. Something like this:
SELECT date_format(booking_date,'%d/%m/%Y')
FROM booking
You didn't post the error in your question which is a big mistake. When you get an error message, you actually have something to work from.
It is also very important to note that the query above returns a string, not a date. It's only good for display, not for date arithmetic
TBH that seems like a terrible site to learn on as it gives no clues about the dialect. it looks like Oracle but to_date and trunc don't work.
The use of convert() suggests that you think you are uinsg SQL Server. If you only want the date component of a date/time data type, then you can use:
SELECT CONVERT(DATE, booking_date), nights
FROM booking
WHERE guest_id = 1183;
The syntax error suggests that you are not using SQL Server.
CONVERT() is bespoke syntax for SQL Server. Examples of similar functionality in other databases are:
DATE(booking_date)
TRUNC(booking_date)
DATE_TRUNC('day', booking_date)
In addition, what you see also depends on the user-interface.
In your case, the data is being stored as a date with no time component, but the UI is showing the time. For that, you want to convert to a string. That site uses MariaDB -- which is really a flavor of MySQL-- and you would use:
DATE_FORMAT(booking_date, '%Y-%m-%d')

MS Access. SQL. get current time

I'm using MS Access and I'm trying to build SQL query. I have this table:
VISIT
VISIT_ID DATE_OF_VISIT COST.
I need to get current date in query, then I need to get year from this date, then I need to get sums of money for each month of that year as a result.
Does anyone know how to get current date?
Do I need to use some sort of cycles?
SELECT Month(date_of_visit), Sum(cost)
FROM VISIT
WHERE Year(date_of_visit) = Year(Date())
GROUP BY Month(date_of_visit);
In MS Access, for a query like this, I use the design tool. If I want the SQL, I use the design tool and then select the SQL view. Within the design tool, consider using the date() function or the now() function, or take a look at Access help. There are a number of options available and one should give you exactly what you are needing. That way, MS Access does all the coding work.

SQL - User input in query

I have this. "Detail all films shown by the club between any two given dates, inputted by the user. For example a club member must be able to input a start date and an end date for the parameters for the query"
Now, how would I go about doing the user input? Only way I can think of would to be using php or something with a html form getting the values and then submitting them as variables in the query. However, that's not what is needed. So, how is this done so the query would ask for values? Or can't you?
My query so far looks like so.
SELECT film.title, film.desc, show.sdate, show.fdate
FROM film
INNER JOIN show
ON film.ID=show.filmID
WHERE sdate = '&userstart' AND fdate = '&userend'
How do I go about with the user input? Also, is the query correct? I have no way of testing, I only have a design not an implementation.
Thanks a lot
Edit: Using Windows system, MS SQL Server.
Here's the code for a stored procedure:
CREATE PROCEDURE SomeName(#UserStart DATETIME, #UserEnd DATETIME)
AS BEGIN
SELECT somestuff
FROM sometable
WHERE somedate BETWEEN #UserStart AND #UserEnd
END
#Kyle93 -
Looking at your WHERE Clause:
WHERE sdate = '&userstart' AND fdate = '&userend'
This will only get you Films that had a sdate(Start Date?) equal to the date value entered.
You might want to use Greater than, Less Than operators....
Such as:
WHERE sdate <= '&userend' AND fdate >= '&userstart'
(Note comparing sdate to UserEnd Date to make sure Film Showing started EARLIER than the UserEnd Date...etc)
This way - when a show starts OR ends within the date range - it will be selected.
Are you familiar at all with LINQ? It's a way to make db calls from c#.
-- I would handle this with HTML as you thought, and use Javascript/Ajax to reference c# code that uses LINQ for db calls. Might be a bit complicated however, if you're not familiar with much of that. I can recommend good tutorials if interested tho.