Select Today's date using BigQuery - google-cloud-platform

I'm using Google Cloud SDK (command-line) via C# and I want to select the information for the Current Date(today).
The select is working but I'm not able to bring the latest date on the column DATE
Below is the query I'm using:
var table = client.GetTable("projectId", "datasetId", "table");
var sql = $"" +
$"SELECT " +
$"sku, " +
$"FROM {table} " +
$"WHERE DATE=CurrentDate('America/Sao_Paulo') " +
$"LIMIT 10";
Schema: SKU - String
DATE - Timestamp

Try to use CURRENT_DATE instead of CurrentDate
var table = client.GetTable("projectId", "datasetId", "table");
var sql = $"" +
$"SELECT " +
$"sku, " +
$"FROM {table} " +
$"WHERE DATE=CURRENT_DATE('America/Sao_Paulo') " +
$"LIMIT 10";

Related

How to update columns in existing table by using temporary table columns in Amazon Redshift?

Below code is developed in SQL to update target table columns. Can some one help me to rewrite below query in redshift as I am trying to execute same query on amazon redshift it is giving error as:
Amazon Invalid operation: relation "c" does not exist;
With TempTable As
(
SELECT Left('abcdefghijk',len(TerritoryName)/3) + Substring(TerritoryName,len(TerritoryName)-len(TerritoryName)/3-len(TerritoryName)/3+1,len(TerritoryName)-len(TerritoryName)/3-len(TerritoryName)/3) + Right('ijklmnopqrstuv',len(TerritoryName)/3) As Masked_TerritoryName
,Left('abcdefghijk',len(DistrictName)/3) + Substring(DistrictName,len(DistrictName)-len(DistrictName)/3-len(DistrictName)/3+1,len(DistrictName)-len(DistrictName)/3-len(DistrictName)/3) + Right('ijklmnopqrstuv',len(DistrictName)/3) As Masked_DistrictName
,Left('abcdefghijk',len(RegionName)/3) + Substring(RegionName,len(RegionName)-len(RegionName)/3-len(RegionName)/3+1,len(RegionName)-len(RegionName)/3-len(RegionName)/3) + Right('ijklmnopqrstuv',len(RegionName)/3) As Masked_RegionName
,Left('abcdefghijk',len(RSMTerritoryName)/3) + Substring(RSMTerritoryName,len(RSMTerritoryName)-len(RSMTerritoryName)/3-len(RSMTerritoryName)/3+1,len(RSMTerritoryName)-len(RSMTerritoryName)/3-len(RSMTerritoryName)/3) + Right('ijklmnopqrstuv',len(RSMTerritoryName)/3) As Masked_RSMTerritoryName
,Left('abcdefghijk',len(CCAName)/3) + Substring(CCAName,len(CCAName)-len(CCAName)/3-len(CCAName)/3+1,len(CCAName)-len(CCAName)/3-len(CCAName)/3) + Right('ijklmnopqrstuv',len(CCAName)/3) As Masked_CCAName
,Left('abcdefghijk',len(LCAName)/3) + Substring(LCAName,len(LCAName)-len(LCAName)/3-len(LCAName)/3+1,len(LCAName)-len(LCAName)/3-len(LCAName)/3) + Right('ijklmnopqrstuv',len(LCAName)/3) As Masked_LCAName
,Left('abcdefghijk',len(TMComp)/3) + Substring(TMComp,len(TMComp)-len(TMComp)/3-len(TMComp)/3+1,len(TMComp)-len(TMComp)/3-len(TMComp)/3) + Right('ijklmnopqrstuv',len(TMComp)/3) As Masked_TMComp
,Left('abcdefghijk',len(ASMTerritoryName)/3) + Substring(ASMTerritoryName,len(ASMTerritoryName)-len(ASMTerritoryName)/3-len(ASMTerritoryName)/3+1,len(ASMTerritoryName)-len(ASMTerritoryName)/3-len(ASMTerritoryName)/3) + Right('ijklmnopqrstuv',len(ASMTerritoryName)/3) As Masked_ASMTerritoryName
,TerritoryCode
FROM TargetTable
)
Update C
Set C.TerritoryName = N.Masked_TerritoryName
,C.DistrictName = N.Masked_DistrictName
,C.RegionName = N.Masked_RegionName
,C.RSMTerritoryName = N.Masked_RSMTerritoryName
,C.CCAName = N.Masked_CCAName
,C.LCAName = N.Masked_LCAName
,C.TMComp = N.Masked_TMComp
,C.ASMTerritoryName = N.Masked_ASMTerritoryName
From TargetTable C
Inner Join TempTable N ON C.TerritoryCode = N.TerritoryCode
I don't believe you can use just an alias for the target table. You have "... Update C ...", I expect you need "... Update TargetTable ..." or "... Update TargetTable C ...".
Also you don't need to list TargetTable in the FROM clause as this is assumed. Your join on conditions become where conditions. So you query will look like this:
With TempTable As
(
SELECT ...
FROM TargetTable
)
Update TargetTable C
Set ...
From TempTable N
Where C.TerritoryCode = N.TerritoryCode

Siddhi - Fetching from Event tables, which are not updated within certain time

In Siddhi query, I am importing two stream S1 and S2. If I receive in S1 stream I will insert in event table T1, and when I receive in S2 I will update in the T1 table based on the id, and also I will send the updated values from the table into Output stream O1.
As a part of the requirement, I need to get the content which table T1, which is inserted before 5 min(ie, if a record resides more than 5 min) and send to another output stream O2.
#name('S1')
from S1
select id, srcId, 'null' as msgId, 'INP' as status
insert into StatusTable;
#name('S2')
from S2#window.time(1min) as g join StatusTable[t.status == 'INP'] as t
on ( g.srcId == t.id)
select t.id as id, g.msgId as msgId, 'CMP' as status
update StatusTable on TradeStatusTable.id == id;
#name('Publish')
from S2 as g join StatusTable[t.status == 'CMP'] as t on ( g.srcId == t.id and t.status == 'CMP')
select t.id as id, t.msgId as msgId, t.status as status
insert into O1;
How to add a query in this existing query to fetch the records from TradeStatus table, which receides more than 5 minutes. Since the table cannot be used alone, I need to join it with a stream, how to do this scenario?
String WebAttackSuccess = "" +
"#info(name = 'found_host_charged1') "+
"from ATDEventStream[ rid == 10190001 ]#window.timeBatch(10 sec) as a1 "+
"join ATDEventStream[ rid == 10180004 ]#window.time(10 sec) as a2 on a2.src_ip == a1.src_ip and a2.dst_ip == a1.dst_ip " +
" select UUID() as uuid,1007 as cid,a1.sensor_id as sensor_id,a1.interface_id as interface_id,a1.other_id as other_id,count(a1.uuid) as event_num,min(a1.timestamp) as first_seen,max(a2.timestamp) as last_seen,'' as IOC,a1.dst_ip as victim,a1.src_ip as attacker,a1.uuid as NDE4,sample:sample(a2.uuid) as Sample_NDE4 " +
" insert into found_host_charged1;"+
""+
"#info(name = 'found_host_charged2') "+
"from every a1 = found_host_charged1 " +
"-> a2 = ATDEventStream[dns_answers != ''] "+
"within 5 min "+
"select UUID() as uuid,1008 as cid,a2.sensor_id as sensor_id,a2.interface_id as interface_id,a2.other_id as other_id,count(a2.uuid) as event_num,a1.first_seen as first_seen,max(a2.timestamp) as last_seen,a2.dns_answers as IOC,a2.dst_ip as victim,a2.src_ip as attacker,a1.uuid as NDE5,sample:sample(a2.uuid) as Sample_NDE5 " +
"insert into found_host_charged2; ";
This is part of my work,i use two stream,maybe you can get the data from StatusTable in your second stream.If not yet resolved,you can change StatusTable to S1.

How do I make special characters (from a referenced page item) not error out a mailto button?

I have a dynamic action on a button that runs a small bit of javascript. Basically, it acts as a mailto link and adds some of the page items to the body of the email. it works for the most part but I have noticed that if the value of the page item contains an & the email cuts off at that point in the text. This is what I currently have:
var policy_num = $v('P9_POLICY');
var tclose = $v('P9_TDATE');
var taskt = $v('P9_TYPE');
var taskd = $v('P9_DESC');
var audito = $v('P9_TASK_AUDIT_OUTCOME');
var auditc = $v('P9_NOTE');
location.href= "mailto:" +
"?subject=" + "Please take immediate action" +
"&body="+
"%0APolicy: " + policy_num +
"%0ATask Closed: " + tclose +
"%0ATask Type: " + taskt +
"%0ATask Description: " + taskd +
"%0AAudit Outcome: " + audito +
"%0AAudit Comment: " + auditc ;
If there is a better way to accomplish this kind of mailto function that I would definitely be open to that. This is just the first way I found that actually worked. Thanks!
If your Items contain an &-Character it will act as a control-character. You need to escape it so it won't be interpreted as a control-character anymore.
It should look something like this:
var policy_num = escape($v('P9_POLICY'));
var tclose = escape($v('P9_TDATE'));
var taskt = escape($v('P9_TYPE'));
var taskd = escape($v('P9_DESC'));
var audito = escape($v('P9_TASK_AUDIT_OUTCOME'));
var auditc = escape($v('P9_NOTE'));
location.href= "mailto:" +
"?subject=" + "Please take immediate action" +
"&body="+
"%0APolicy: " + policy_num +
"%0ATask Closed: " + tclose +
"%0ATask Type: " + taskt +
"%0ATask Description: " + taskd +
"%0AAudit Outcome: " + audito +
"%0AAudit Comment: " + auditc ;

How can I use C++ to update an SQLite row relative to its original value?

I am trying to update a row in a table in an SQLite database using C++, but I want to update it relative to its current value.
This is what I have tried so far:
int val=argv[2];
string bal = "UPDATE accounts SET balance = balance + " + argv[1] + "WHERE account_id = " + bal + argv[2];
if (sqlite3_open("bank.db", &db) == SQLITE_OK)
{
sqlite3_prepare( db, balance.c_str(), -1, &stmt, NULL );//preparing the statement
sqlite3_step( stmt );//executing the statement
}
So that the first parameter is the account_id, and the second parameter is the current balance.
However, this does not work. What can I do to have the database successfully update?
Thank you!
EDIT: Sorry for the confusion. The primary situation is having a table with many entries, each with a unique account id. For example, one has an id of 1 with a balance of 5.
If I run this program with the parameters "1 5", the balance should now be 10. If I run it again with "1 7", it should be 17.
You cannot use the + operator to concatenate C-style strings and string literals. A quick and dirty fix:
string bal = string("UPDATE accounts SET balance = balance + ") + argv[1] + string( " WHERE account_id = " ) + argv[2];

Select nth to nth row while table still have values unselected with python and pyodbc

I have a table with 10,000 rows and I want to select the first 1000 rows and then select again and this time, the next set of rows, which is 1001-2001.
I am using the BETWEEN clause in order to select the range of values. I can also increment the values. Here is my code:
count = cursor.execute("select count(*) from casa4").fetchone()[0]
ctr = 1
ctr1 = 1000
str1 = ''
while ctr1 <= count:
sql = "SELECT AccountNo FROM ( \
SELECT AccountNo, ROW_NUMBER() OVER (ORDER BY Accountno) rownum \
FROM casa4 ) seq \
WHERE seq.rownum BETWEEN " + str(ctr) + " AND " + str(ctr1) + ""
ctr = ctr1 + 1
ctr1 = ctr1 + 1000
cursor.execute(sql)
sleep(2) #interval in printing of the rows.
for row in cursor:
str1 = str1 + '|'.join(map(str,row)) + '\n'
print "Records:" + str1 #var in storing the fetched rows from database.
print sql #prints the sql statement(str) and I can see that the var, ctr and ctr1 have incremented correctly. The way I want it.
What I want to achieve is using a messaging queue, RabbitMQ, I will send this rows to another database and I want to speed up the process. Selecting all and sending it to the queue returns an error.
The output of the code is that it returns 1-1000 rows correctly on the 1st but, on the 2nd loop, instead of 1001-2001 rows, it returns 1-2001 rows, 1-3001 and so on.. It always starts on 1.
I was able to recreate your issue with both pyodbc and pypyodbc. I also tried using
WITH seq (AccountNo, rownum) AS
(
SELECT AccountNo, ROW_NUMBER() OVER (ORDER BY Accountno) rownum
FROM casa4
)
SELECT AccountNo FROM seq
WHERE rownum BETWEEN 11 AND 20
When I run that in SSMS I just get rows 11 through 20, but when I run it from Python I get all the rows (starting from 1).
The following code does work using pyodbc. It uses a temporary table named #numbered, and might be helpful in your situation since your process looks like it would do all of its work using the same database connection:
import pyodbc
cnxn = pyodbc.connect("DSN=myDb_SQLEXPRESS")
crsr = cnxn.cursor()
sql = """\
CREATE TABLE #numbered (rownum INT PRIMARY KEY, AccountNo VARCHAR(10))
"""
crsr.execute(sql)
cnxn.commit()
sql = """\
INSERT INTO #numbered (rownum, AccountNo)
SELECT
ROW_NUMBER() OVER (ORDER BY Accountno) AS rownum,
AccountNo
FROM casa4
"""
crsr.execute(sql)
cnxn.commit()
sql = "SELECT AccountNo FROM #numbered WHERE rownum BETWEEN ? AND ? ORDER BY rownum"
batchsize = 1000
ctr = 1
while True:
crsr.execute(sql, [ctr, ctr + batchsize - 1])
rows = crsr.fetchall()
if len(rows) == 0:
break
print("-----")
for row in rows:
print(row)
ctr += batchsize
cnxn.close()