I activated the streaming export from Google Analytics to BigQuery about a year ago but I'm having troubles changing the SQL syntax of the real-time view (ga_realtime_sessions_view_YYYYMMDD) to STANDARD SQL.
How can I change the SQL of this view? The view is defined as:
SELECT *
FROM [XXX.ga_realtime_sessions_20180424]
where exportKey in (
SELECT exportKey
FROM (
SELECT
exportKey,
exportTimeUsec,
MAX(exportTimeUsec) OVER (PARTITION BY visitKey) AS maxexportTimeUsec
FROM [XXX.ga_realtime_sessions_20180424]
)
WHERE exportTimeUsec >= maxexportTimeUsec
);
You can create a standard SQL view of this form:
CREATE VIEW `XXX.ga_realtime_view` AS
SELECT
_TABLE_SUFFIX AS suffix,
ARRAY_AGG(t ORDER BY exportTimeUsec DESC LIMIT 1)[OFFSET(0)].*
FROM `XXX.ga_realtime_sessions_20*` AS t
GROUP BY suffix, visitKey;
This returns the latest row in accordance with exportTimeUsec for each visitKey. When querying the view, filter on the suffix corresponding to the date that you want. For example,
SELECT *
FROM `XXX.ga_realtime_view`
WHERE suffix = '180424';
This returns data from the XXX.ga_realtime_sessions_20180424 table.
Related
I have a flask API and I'm using Flask-SQLAlchemy to handle a SQLite database. I have a table which stores log entries, and I want to limit the maximum number of rows to a number n. Since insertions are also made from another script outside of flask using raw SQL, I created a trigger that checks the number of rows and deletes the oldest ones if the number is higher than n:
CREATE TRIGGER 'trigger_log_insert'
BEFORE INSERT ON 'connection_logs'
WHEN ( SELECT count(*) FROM 'connection_logs' ) > 5
BEGIN
DELETE FROM 'connection_logs'
WHERE id NOT IN ( SELECT id FROM 'connection_logs' ORDER BY id DESC LIMIT 5 );
END
This trigger works as expected, but I am struggling to set it using flask-sqlalchemy. How can I set the trigger / execute raw SQL using flask-sqlalchemy? The SQL only needs to be executed once after db creation so I intent to execute it right after my create_all() statement.
I stumbled upon this StackOverflow answer which suggested a solution that is apparently going to be deprecated soon. I also read the SQLAlchemy documentation about custom DDL, but I don't know how to create this custom DDL with flask_sqlalchemy. When I create the DDL like in the SQLAlchemy documentation, I get an error message saying
DDL object is not bound to an Engine or Connection.:
trigger = DDL(
"CREATE TRIGGER 'trigger_log_insert'"
"BEFORE INSERT ON 'connection_logs'"
"WHEN ( SELECT count(*) FROM 'connection_logs' ) > 5"
"BEGIN"
"DELETE FROM 'connection_logs' WHERE id NOT IN"
"("
"SELECT id FROM 'connection_logs' ORDER BY id DESC LIMIT 5"
");"
"END"
)
event.listen(ConnectionLog, 'after_create', trigger.execute())
My model is defined using flask-sqlalchemy's declarative base model:
class ConnectionLog(db.Model):
__tablename__ = 'connection_logs'
You don't need to create a DDL instance, you can execute the SQL within the listener function. The relevant docs are here.
import sqlalchemy as sa
...
class ConnectionLog(db.Model):
__tablename__ = 'connection_logs'
...
def after_create(target, connection, **kw):
connection.execute(sa.text("""\
CREATE TRIGGER 'trigger_log_insert'
BEFORE INSERT ON 'connection_logs'
WHEN ( SELECT count(*) FROM 'connection_logs' ) > 5
BEGIN
DELETE FROM 'connection_logs' WHERE id NOT IN
(
SELECT id FROM 'connection_logs' ORDER BY id DESC LIMIT 5
);
END
"""
))
# Listen on the underlying table object, not on the model class.
sa.event.listen(ConnectionLog.__table__, "after_create", after_create)
Ensure that the interpreter has read this code before creating the tables.
I want to create a browse such that it will show all the records from one table if the values of a field do NOT exist in another table.
It is possible to get the records using SQL as:
SELECT myField FROM pub.myTable WHERE
NOT EXISTS (SELECT myField FROM pub.myTable2 WHERE myTable2.myField=myTable.myField)
It is also possible using 4GL as:
FOR EACH myTable WHERE
NOT CAN-FIND(FIRST myTable2 WHERE myTable2.myField=myTable.myField)
The problem is when I put this query in a browse as:
OPEN QUERY myBrowse
FOR EACH myTable WHERE
NOT CAN-FIND(FIRST myTable2 WHERE myTable2.myField=myTable.myField)
it gives an error message
CAN-FIND is invalid within an OPEN QUERY. (3541)
The question is, is it possible to write such an OPEN QUERY statement?
I didn't come up with this, Steve Moore shared it on https://community-archive.progress.com/forums/00026/27143.html
define temp-table ttNoOrder
field field1 as char.
create ttNoOrder.
define query q1 for Customer, Order, ttNoOrder.
open query q1 for each Customer no-lock,
first Order of Customer outer-join no-lock,
first ttNoOrder where not available(Order).
get first q1.
repeat while not query-off-end("q1"):
display Customer.CustNum Customer.Name available(Order).
get next q1.
end.
Works even with dynamic queries:
DEFINE TEMP-TABLE ttNoOrder
FIELD field1 AS CHARACTER .
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE ttNoOrder.
CREATE QUERY hQuery .
hQuery:SET-BUFFERS (BUFFER Customer:HANDLE,
BUFFER Order:HANDLE,
BUFFER ttNoOrder:HANDLE) .
hQuery:QUERY-PREPARE ("for each Customer no-lock, ~
first Order of Customer outer-join no-lock, ~
first ttNoOrder where not available(Order)") .
hQuery:QUERY-OPEN() .
hQuery:GET-FIRST () .
REPEAT WHILE NOT hQuery:QUERY-OFF-END:
DISPLAY Customer.CustNum FORMAT ">>>>>>>>>9" Customer.Name AVAILABLE(Order).
hQuery:GET-NEXT ().
END.
I have been trying to pass dynamic date parameters in SSRS's Visual Studio using SSAS and the Query Designer. I am trying to get my MDX query to pull from '1/1/2010' to last month using the CalendarMonth field. Here is my query:
SELECT
NON EMPTY
{
[Measures].[Full Case Quantity]
,[Measures].[Total]
,[Measures].[Verified Total]
} ON COLUMNS
,NON EMPTY
{
[PA Product].[PA Description].[PA Description].ALLMEMBERS*
[PA Product].[PA Product ID].[PA Product ID].ALLMEMBERS*
[Time].[Calendar Month].[Calendar Month].ALLMEMBERS*
[PA Product].[PA Uom].[PA Uom].ALLMEMBERS*
[Distributor].[Dist Name].[Dist Name].ALLMEMBERS*
[Time].[Calendar Year].[Calendar Year].ALLMEMBERS*
[Time].[Month Number Of Year].[Month Number Of Year].ALLMEMBERS
}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM
(
SELECT
{[Concept OG].[Is Concept Display].&[1]} ON COLUMNS
FROM
(
SELECT
{[Concept].[Concept ID].&[6501]} ON COLUMNS
FROM
(
SELECT
NULL : StrToMember(#ToTimeCalendarMonth,CONSTRAINED) ON COLUMNS
FROM [Management]
)
)
)
WHERE
(
[Concept].[Concept ID].&[6501]
,[Concept OG].[Is Concept Display].&[1]
)
CELL PROPERTIES
VALUE
,BACK_COLOR
,FORE_COLOR
,FORMATTED_VALUE
,FORMAT_STRING
,FONT_NAME
,FONT_SIZE
,FONT_FLAGS;
Not sure what the problem is so need to do some digging!
If you run the following in SSMS what is returned? You will need to replace #ToTimeCalendarMonth with whatever your parameter is passing in:
SELECT
{} on 0,
NULL : StrToMember(#ToTimeCalendarMonth,CONSTRAINED) ON 1
FROM [Management]
The parameter will need to be something like this ... I don't know how the members in your cube look so this is a guess:
[Time].[Calendar Month].[Calendar Month].[200604]
I have two tables Projects and Invoices. The database is MS Access ( mdb )
Project table has fields
ProjectID
ProjectName
ProjectStatus
Invoices table has the fields
InvoiceID
ProjectID ( foreign key )
InvoiceType ( Paid and Recieved )
InvoiceChannel ( Coding, Designing and Consulting )
InvoiceAmount
The Projects table is in a one-to-many relation with Invoices table with Project ID as foreign key.
I want a table created from querying the Invoices table like this
How can I achieve this using an SQL query?
Or do I have to do it using server side coding ( I use C# with ASP.NET )
Your posted table schema doesn't have a field that contains the amount to sum. I will refer to it as Amount.
First, create a crosstab query, and save it in the database (I've called it ct):
TRANSFORM Sum(Amount) AS SumOfAmount
SELECT ProjectID, InvoiceType
FROM Invoices
GROUP BY ProjectID, InvoiceType
PIVOT InvoiceChannel In ("Coding","Designing","Consulting");
This will give you the ProjectID and the InvoiceType as the first two columns, with one additional column each for "Coding","Designing" and "Consulting".
To get the total, you need another query:
SELECT ct.*, Coding + Designing + Consulting AS Total
FROM ct;
Bear in mind that if there are no records for a particular ProjectID/InvoiceType/InvoiceChannel, the query will return NULL for that combination, which will result in the Total being NULL. If this query will be running under Access (say via Access.Application) you can use the Nz function, which works like the ?? operator in C#:
SELECT ct.*, Nz(Coding,0) + Nz(Designing,0) + Nz(Consulting,0) AS Total
FROM ct;
If you are accessing data from an .mdb or .accdb via ADO.NET using the OleDbProvider, you probably won't be able to use Nz, and you'll have to use something more complex:
SELECT ct.*,
Iif(IsNull(Coding), 0, Coding) +
Iif(IsNull(Designing), 0, Designing) +
Iif(IsNull(Consulting), 0, Consulting) AS Total
FROM ct;
im creating (in Qt )
QString q = "CREATE VIRTUAL TABLE playlist USING fts3 ("
"from_user , "
"from_id , "
"created_time , "
"created_time_formated , "
"user_id unique )";
then execute
"INSERT OR IGNORE INTO play_list (from_user,from_id,created_time,created_time_formated,user_id) SELECT '....','....','.....','....','123'"
im using full Full-Text Search so this is way im using VIRTUAL TABLE
if i have another row with user_id == 123 it will still insert the row , why ?
The FTS documentation states that:
The same applies to any constraints specified along with an FTS column name - they are parsed but not used or recorded by the system in any way.
So the UNIQUE constraint is ignored.
You can emulate the constraint by using a trigger on a view (not on the table because triggers are not available on virtual tables), and then by inserting the row on that view:
CREATE VIEW playlist_view AS SELECT * FROM playlist;
CREATE TRIGGER insert_playlist INSTEAD OF INSERT ON playlist_view
BEGIN
SELECT RAISE(ABORT, 'column user_id is not unique') FROM playlist
WHERE user_id=new.user_id;
INSERT INTO playlist (from_user, from_id, created_time,
created_time_formated, user_id)
VALUES (NEW.from_user, NEW.from_id, NEW.created_time,
NEW.created_time_formated, NEW.user_id);
END;
-- And you do the insertion on the view
INSERT INTO playlist_view (from_user,from_id,created_time,created_time_formated,user_id) SELECT ...;
Because OR IGNORE won't ignore an explicit RAISE, if you want to ignore the error, you have to replace RAISE(ABORT,...) by RAISE(IGNORE).