pcregrep multiline SQL match - pcregrep

I'm trying to match following multiline SQL code with pcregrep:
create table work.W4XLP0D as
select distinct
Name,
('ABCD') as xxx length = 4
from &SYSLAST
where Name='Alfred'
;
I wanna match distinct and ('ABCD') but it's possible that there are more than one Attributes in select statment... If tried the following:
[y1e1819#sasbidientw01 ~]$ pcregrep -M '(\n).*\(\".*\"\) as' *.sas
double_distinct.sas: Name,
("ABCD") as xxx length = 4
But I'm not able how to say that there can be multiple line breaks

Related

Joining on column names with spaces

I'm trying to join to tables using PROQ SQL. One of the columns I'm using for the join has a space in the column name. The query I'm using:
PROC SQL;
CREATE TABLE TEST AS
SELECT a.*, b.*
FROM TABLE_1 a
INNER JOIN TABLE_2 b
ON a.CONTNO = b."Contract Number";
RUN;
This is the error I'm getting:
ERROR 22-322: Syntax error, expecting one of the following: a name, *.
How do I fix this?
You just need to add square brackets around the Column name. For example:
b.[Contract Number]
Tips: Using alias (a, b) can be costly. When you only have one table to join, consider typing out the table rather than doing an alias.

Model.group(:id) throws an error "Select list is not in GROUP BY clause contains non aggregated column "id"

I am using Model.group(:category) in order to get unique records based on category field in Rails 5 application.
Table data:
id catgeory description
1 abc test
2 abc test1
3 abc test2
4 xyz test
5 xyz testabc
I want records (1,4) as a result. Therefore I am using Model.group(:category) which works fine for MYSQL whose sql_mode is " " .
Unforunately its throwing an error "SELECT list is not in GROUP BY clause and contains nonaggregated column which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by" whose sql_mode is "only_full_group_by".
whats the best way to change the query to match the mode?
Perhaps try specifying which id you want? You could use MIN(id), MAX(id) etc.
MySQL supports a non-standard extension to SQL described here. To continue using that behavior, you could change the sql_mode to TRADITIONAL in config/database.yml.

ruby on rails' alphabetical order method doesn't place word boundary before "a"? [duplicate]

I use PostgreSQL 9.3.3 and I have a table with one column named as title (character varying(50)).
When I have executed the following query:
select * from test
order by title asc
I got the following results:
#
A
#Example
Why "#Example" is in the last position? In my opinion "#Example" should be in the second position.
Sort behaviour for text (including char and varchar as well as the text type) depends on the current collation of your locale.
See previous closely related questions:
PostgreSQL Sort
https://stackoverflow.com/q/21006868/398670
If you want to do a simplistic sort by ASCII value, rather than a properly localized sort following your local language rules, you can use the COLLATE clause
select *
from test
order by title COLLATE "C" ASC
or change the database collation globally (requires dump and reload, or full reindex). On my Fedora 19 Linux system, I get the following results:
regress=> SHOW lc_collate;
lc_collate
-------------
en_US.UTF-8
(1 row)
regress=> WITH v(title) AS (VALUES ('#a'), ('a'), ('#'), ('a#a'), ('a#'))
SELECT title FROM v ORDER BY title ASC;
title
-------
#
a
#a
a#
a#a
(5 rows)
regress=> WITH v(title) AS (VALUES ('#a'), ('a'), ('#'), ('a#a'), ('a#'))
SELECT title FROM v ORDER BY title COLLATE "C" ASC;
title
-------
#
#a
a
a#
a#a
(5 rows)
PostgreSQL uses your operating system's collation support, so it's possible for results to vary slightly from host OS to host OS. In particular, at least some versions of Mac OS X have significantly broken unicode collation handling.
It seems, that when sorting Oracle as well as Postgres just ignore non alpha numeric chars, e.g.
select '*'
union all
select '#'
union all
select 'A'
union all
select '*E'
union all
select '*B'
union all
select '#C'
union all
select '#D'
order by 1 asc
returns (look: that DBMS doesn't pay any attention on prefix before 'A'..'E')
*
#
A
*B
#C
#D
*E
In your case, what Postgres actually sorts is
'', 'A' and 'Example'
If you put '#' in the middle od the string, the behaviour will be the same:
select 'A#B'
union all
select 'AC'
union all
select 'A#D'
union all
select 'AE'
order by 1 asc
returns (# ignored, and so 'AB', 'AC', 'AD' and 'AE' actually compared)
A#B
AC
A#D
AE
To change the comparison rules you should use collation, e.g.
select '#' collate "POSIX"
union all
select 'A' collate "POSIX"
union all
select '#Example' collate "POSIX"
order by 1 asc
returns (as it required in your case)
#
#Example
A

How to show data in a table by using psql command line interface?

Is there a way to show all the content inside a table by using psql command line interface?
I can use \list to show all the databases, \d to show all the tables, but how can I show all the data in a table?
Newer versions: (from 8.4 - mentioned in release notes)
TABLE mytablename;
Longer but works on all versions:
SELECT * FROM mytablename;
You may wish to use \x first if it's a wide table, for readability.
For long data:
SELECT * FROM mytable LIMIT 10;
or similar.
For wide data (big rows), in the psql command line client, it's useful to use \x to show the rows in key/value form instead of tabulated, e.g.
\x
SELECT * FROM mytable LIMIT 10;
Note that in all cases the semicolon at the end is important.
Step 1. Check the display mode is "on" by using
\x
Step 2. Don't forget the ;
I tried for fifteen minutes just because I forgot the semicolon.
AND USE UPPERCASE ENGLISH.
TABLE users;
And you will get something like
On Windows use the name of the table in quotes:
TABLE "user"; or SELECT * FROM "user";
you should use quotes
example =>
1) \c mytablename
2) SELECT * FROM "mytablename"; OR TABLE "mytablename";
postgres commande line
to show databases : \l
to show tables : \dt
to show data in table x : SELECT * FROM "x";
to exit : \q
If you use schemas, the following will be correct:
SELECT * FROM "schema-name"."table-name";

Join two csv files based on name field

I have two tables with id and name. I want to join them based on the name field and output id and id.
File1:
id, name
a0N3000000A0JNaEAN,Guarda Val
a0Na000000G8CCfEAN,Bentleys House
a0Na000000EQVg6EAH,Alpine Lodge Resort
a0N30000007LwcaEAC,Kulm Hotel
File2:
id, name
a0BQ00000013OeSMAU,Guarda Val
a0BQ00000013OeBMAU,Bentleys House
a0BQ00000013OeVMAU,Alpine Lodge Resort
a0BQ0000001xlQoMAI,Kulm Hotel
What I wish to see is:
id.1, id.2
a0N3000000A0JNaEAN,a0BQ00000013OeSMAU
a0Na000000G8CCfEAN,a0BQ00000013OeBMAU
a0Na000000EQVg6EAH,a0BQ00000013OeVMAU
a0N30000007LwcaEAC,a0BQ0000001xlQoMAI
I have tried to scribble something, but the closest I've got was this:
join -t, -a1 -a2 -1 2 -2 2 -o '0,1.2' <(sort sandees.1.csv) <(sort prodees.1.csv)
Which just prints out the names. For the record, I am using OS X 10.8. I have seen that join behaviour might vary between different OSes.
Thanks
The first thing to do is to get your input files as files; after that is working, you can use the fancier syntax.
The next thing is to do the sorting the way that join requires, namely sort each file on the second field instead of the first field. You need to use:
sort -t, -k2 sandees1.csv >sandees1_sorted.csv
sort -t, -k2 prodees1.csv >prodees1_sorted.csv
Your output format, -o specifies the key plus the second field of the first file. You said you wanted the first field of each file.
join -t, -a1 -a2 -1 2 -2 2 -o '1.1,2.1' sandees1_sorted.csv prodees1_sorted.csv
will produce the desired result.

Resources