CloudWatch Query join two records having same id - amazon-cloudwatch

I have a cloudwatch log in the following format
id
name
city
1
John
1
New York
2
Seatle
2
Mike
I am looking for a way to fetch log data in the following format combining empty and non-empty values
id
name
city
1
John
New York
2
Mike
Seatle
How can I achieve this using CloudWatch log filter query?

I don't think that's possible at the moment with just CloudWatch. There's nothing in the log insights functions that would do that.
You could try analyzing your logs in Athena, but that does add cost and complexity. You can use the CloudWatch / Athena connector which would let you query logs with SQL. Alternatively you could export the logs to S3 for further analysis.

Related

SQL different null values in different rows

I have a quick question regarding writing a SQL query to obtain a complete entry from two or more entries where the data is missing in different columns.
This is the example, suppose I have this table:
Client Id | Name | Email
1234 | John | (null)
1244 | (null) | john#example.com
Would it be possible to write a query that would return the following?
Client Id | Name | Email
1234 | John | john#example.com
I am finding this particularly hard because these are 2 entires in the same table.
I apologize if this is trivial, I am still studying SQL and learning, but I wasn't able to come up with a solution for this and I although I've tried looking online I couldn't phrase the question in the proper way, I suppose and I couldn't really find the answer I was after.
Many thanks in advance for the help!
Yes, but actually no.
It is possible to write a query that works with your example data.
But just under the assumption that the first part of the mail is always equal to the name.
SELECT clients.id,clients.name,bclients.email FROM clients
JOIN clients bclients ON upper(clients.name) = upper(substring(bclients.email from 0 for position('#' in bclients.email)));
db<>fiddle
Explanation:
We join the table onto itself, to get the information into one row.
For this we first search for the position of the '#' in the email, get the substring from the start (0) of the string for the amount of characters until we hit the # (result of positon).
To avoid case-problems the name and substring are cast to uppercase for comparsion.
(lowercase would work the same)
The design is flawed
How can a client have multiple ids and different kind of information about the same user at the same time?
I think you want to split the table between clients and users, so that a user can have multiple clients.
I recommend that you read information about database normalization as this provides you with necessary knowledge for successfull database design.

Counting Records Over a Rolling Period of Time SQL (Based on Last Record)

I'm relatively new to SQL and trying to pull the following from an Oracle SQL database.
Let's say I have a table of users and the time that they logged in that looks like this:
Name LOG_IN
Jim 13:00:05
Patrick 13:02:23
Steve 13:02:44
Emma 13:03:16
Steve 13:04:44
Jim 13:04:05
Jim 13:05:05
Jim 13:05:06
Patrick 13:05:17
Emma 13:05:18
Steve 13:08:13
Say I want to run a report which tells me their last user login in and all logins that happened within a timeframe of 5 minutes before the last login. If I was doing this in another language, I would just a for .. loop to get the last login and then count back to previous logins and compare if the login time falls within the 5 min window. I am unsure how I would accomplish the same thing with Oracle SQL. For example, for someone like Jim, his last login is at 13:05:06 so I would want all the times he logged in between 13:00:06 and 13:05:06, which would be:'
Name LOG_IN
Jim 13:04:05
Jim 13:05:05
Jim 13:05:06
So the very first login (at 13:00:05) would not be included because it's not in the range.
The same report would return results for the other users as well, so for Steve, the following would be returned:
Name LOG_IN
Steve 13:04:44
Steve 13:08:13
And the first login (at 13:02:44) would not be returned.
When I first looked at this, I thought the requirement was to pull all transactions within a 5 minute of the time of the report, but I have since learned I need to do this rolling period calculation based on last login.
select Name, LOG_IN
from <table_name> A where LOG_IN >
(select max(LOG_IN) from <table_name> where Name=A.Name)-(1/24./60.*5.);
Here's a sqlfiddle link:
http://sqlfiddle.com/#!4/b231e/11/0
(don't know how long it will be persistent...)

I am wondering if there is an elegant way to apply either a combination of query, Arrayformula, sort, functions in Google Sheets to do the following

Google Sheets Problem. I have a master list that has columns which are employers, job post, # of spots, parameter x, parameter y,...etc.
"Master Sheet" #a tab
Employers Job Spots
John Cleaner 1
Mike Cleaner 2
John Cleaner 3
John Server 5
Alice Cook 1
Dave Cook 1
Mary Cleaner 3
Alice Server 5
Alice Cleaner 2
Dave Server 4
Mike Server 3
Alice Server 1
This is what I would like "Output Sheet" #another tab with two columns. 1st is Jobs and 2nd is # of employers that account for 80% of the jobs in that category plus any additional filters. The idea is to give a single # that gives an 80/20 rule type metric. The trick is to Sort one column from highest to lowest first. I can do this but in multiple steps that seem annoyingly inefficient. I wonder if there is a better way where I can put everything in one cell and drag down or do a query function. The output looks like below.
Job # of employers that account for ~80% of all the jobs in that category + filters
Cleaner ~3
Cook 1
Server ~3
#because total Cleaner jobs is 11. 80% is 8.8. And sorting employers highest to lowest (after accounting for duplicates), 3 employers represent 80% of the Cleaner jobs available. Server total is 21, 80% is 16.8, so ~3 employers represent 80% of the Server jobs available.
Thank you all for your help.
To take 80%:
=query(A15:C26, "Select B, sum(C)*8/100 group by B label B 'Job'")
you will get
{0.88, 0.16, 1.44)
But the next you can continue by yourself

PL/SQL-> inserting 500 users to a table from excel

We have a Oracle table which consists of following columns: user,code,date,number,origin.eg:
|user, code, date, number, origin|
|--------------------------------|
|stah, LK, 10-1, 20091, WEST |
|hats, LJ, 12-2, 30001, ESTA |
This table already has 1000 users. Now we want to add another 500 new users to this table and also grant all those 500 users same code->{'LD','LM','MK',CK'}.i.e each user should have same code (And date,number,origin can be null).
I have 500 users in excel. Can someone help me to figure out how to insert these into a table?
Build your script manually in excel by using formulars:
user code date number origin skript
stah LK 10-1 20091 West =CONCAT("INSERT INTO mytable values(""";A2;"""";""";B2;"""";""";C2;"""";""";D2;"""";""";E2;"""";");")
hats LJ 12-2 30001 ESTA INSERT INTO mytable values("hats"";B2;"";"12-2"";D2;"";"ESTA");
First row shows formular - second shows the result..
You could use SQL Developer to import data from spreadsheet into table. Look here: http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r30/SQLdev3.0_Import_Export/sqldev3.0_import_export.htm#t3
You can use free data migration tool PDI which is very helpful and
manageable
You can schedule it by using windows or linux scheduler
here is a tutorial link how it migrate excel to oracle
https://www.youtube.com/watch?v=RDxdy_8mOa0

Vendor agnostic SQL to concatenate field values across records

I have the following DB Schema :-
Data is ...
Location Table
1. New York
2. London
3. Tokyo
4. Melbourne
OtherNames Table (aka Aliases)
1. NYC
1. New York City
4. Home
3. Foo
3. PewPew
What I'm trying to do, as SQL, is get the following results :-
ID, Name, Name + Aliases
eg.
1 | New York | new york nyc new york city
2 | London | NULL
3 | Tokyo | tokyo foo pewpew
4 | Melbourne | melbourne home
I'm not sure how to get that LAST column.
It's like I want to have a SubQuery which COALESCE's the OtherName.Name field, per Location row... ?
It's related to a previous question I have .. but my previous question doesn't give me the proper results I was after (I didn't ask the right question, before :P)
NOTE: I'm after a TSQL / Non server specific answer. So please don't suggest GROUP_CONCAT();
SQL isn't suited to this kind of operation (1NF violation and all that), therefore the various workarounds in SQL will be vendor-specific. If you want something vendor-independent then use something that will consume vanilla SQL (rather than generate it) e.g. a report writer or 3GL application ;)
If you're using SQL Server 2005 onwards, I personally like the XPATH approach