Add a dot notation in Label SQL - sql

I would like to return my label in SQL so they look as dot notation.
Something like this should be return:
|---------------------|------------------|
| product.attribute | product.order |
|---------------------|------------------|
| product A | 34 |
|---------------------|------------------|
However, when I try to insert the dot in my label.
It returns me an error.
I am sure it is something obvious that I missed.
Example of query :
SELECT product as "product.attribute", Count(Order) as "product.order",
from orderTable
Limit 100

Probably you are using MySQL, in this case you should use backticks:
SELECT `product` AS `product.attribute`, COUNT(`order`) AS `product.order`,
FROM `orderTable` LIMIT 100
Otherwise the engine will complain about using reserved keywords (like order).

I depends on the database:
In Oracle, DB2, or PostgreSQL you can do:
SELECT product as "product.attribute"...
In SQL Server, or Sybase you can do:
SELECT product as [product.attribute]...
In MariaDB or MySQL you can do:
SELECT product as `product.attribute`...

Related

Use of LIKE in PostgreSQL with brackets

I try to be so specific as possible.
Currently I use MS SQL Server 2012.
A simplified table PlanMission contain these rows
|---------------------|---------------------|
| Bold_Id | MCurrentStates |
|---------------------|---------------------|
| 10776 |[original[scheme] |
|---------------------|---------------------|
| 10777 |[operative][inproc] |
|---------------------|---------------------|
| 10778 |[operative][closed] |
|---------------------|---------------------|
| 10779 |[operative][planopen]|
|---------------------|---------------------|
The Bold_id column is just an ID that is unique.
The column MCurrentStates is a VARCHAR column containing states as substrings.
A state is simply a string surrounded by brackets like [planopen]
So the column may be empty or have many states like example above.
IN MS SQL if I do like this
SELECT Bold_Id, MCurrentStates
FROM PlanMission
WHERE MCurrentStates LIKE '%[planopen]%'
it don't work. It just list all rows that are not empty in MCurrentStates.
It is solved by insert []
SELECT Bold_Id, MCurrentStates
FROM PlanMission
WHERE MCurrentStates LIKE '%[[]planopen]%'
That works fine.
Now I want to do this also for PostgreSQL.
Simple solution is to remove the brackets.
But my question is how can I do this with a query that is the same for both MS SQL and PostgreSQL?
Try:
SELECT Bold_Id, MCurrentStates
FROM PlanMission
WHERE MCurrentStates LIKE '%/[planopen/]%' ESCAPE '/';

PostgreSQL Reverse LIKE

I need to test if any part of a column value is in a given string, instead of whether the string is part of a column value.
For instance:
This way, I can find if any of the rows in my table contains the string 'bricks' in column:
SELECT column FROM table
WHERE column ILIKE '%bricks%';
But what I'm looking for, is to find out if any part of the sentence "The ships hung in the sky in much the same way that bricks don’t" is in any of the rows.
Something like:
SELECT column FROM table
WHERE 'The ships hung in the sky in much the same way that bricks don’t' ILIKE '%' || column || '%';
So the row from the first example, where the column contains 'bricks', will show up as result.
I've looked through some suggestions here and some other forums but none of them worked.
Your simple case can be solved with a simple query using the ANY construct and ~*:
SELECT *
FROM tbl
WHERE col ~* ANY (string_to_array('The ships hung in the sky ... bricks don’t', ' '));
~* is the case insensitive regular expression match operator. I use that instead of ILIKE so we can use original words in your string without the need to pad % for ILIKE. The result is the same - except for words containing special characters: %_\ for ILIKE and !$()*+.:<=>?[\]^{|}- for regular expression patterns. You may need to escape special characters either way to avoid surprises. Here is a function for regular expressions:
Escape function for regular expression or LIKE patterns
But I have nagging doubts that will be all you need. See my comment. I suspect you need Full Text Search with a matching dictionary for your natural language to provide useful word stemming ...
Related:
IN vs ANY operator in PostgreSQL
PostgreSQL LIKE query performance variations
Pattern matching with LIKE, SIMILAR TO or regular expressions in PostgreSQL
This query:
SELECT
regexp_split_to_table(
'The ships hung in the sky in much the same way that bricks don’t',
'\s' );
gives a following result:
| regexp_split_to_table |
|-----------------------|
| The |
| ships |
| hung |
| in |
| the |
| sky |
| in |
| much |
| the |
| same |
| way |
| that |
| bricks |
| don’t |
Now just do a semijoin against a result of this query to get desired results
SELECT * FROM table t
WHERE EXISTS (
SELECT * FROM (
SELECT
regexp_split_to_table(
'The ships hung in the sky in much the same way that bricks don’t',
'\s' ) x
) x
WHERE t.column LIKE '%'|| x.x || '%'
)

SQL: Select distinct based on regular expression

Basically, I'm dealing with a horribly set up table that I'd love to rebuild, but am not sure I can at this point.
So, the table is of addresses, and it has a ton of similar entries for the same address. But there are sometimes slight variations in the address (i.e., a room # is tacked on IN THE SAME COLUMN, ugh).
Like this:
id | place_name | place_street
1 | Place Name One | 1001 Mercury Blvd
2 | Place Name Two | 2388 Jupiter Street
3 | Place Name One | 1001 Mercury Blvd, Suite A
4 | Place Name, One | 1001 Mercury Boulevard
5 | Place Nam Two | 2388 Jupiter Street, Rm 101
What I would like to do is in SQL (this is mssql), if possible, is do a query that is like:
SELECT DISTINCT place_name, place_street where [the first 4 letters of the place_name are the same] && [the first 4 characters of the place_street are the same].
to, I guess at this point, get:
Plac | 1001
Plac | 2388
Basically, then I can figure out what are the main addresses I have to break out into another table to normalize this, because the rest are just slight derivations.
I hope that makes sense.
I've done some research and I see people using regular expressions in SQL, but a lot of them seem to be using C scripts or something. Do I have to write regex functions and save them into the SQL Server before executing any regular expressions?
Any direction on whether I can just write them in SQL or if I have another step to go through would be great.
Or on how to approach this problem.
Thanks in advance!
Use the SQL function LEFT:
SELECT DISTINCT LEFT(place_name, 4)
I don't think you need regular expressions to get the results you describe. You just want to trim the columns and group by the results, which will effectively give you distinct values.
SELECT left(place_name, 4), left(place_street, 4), count(*)
FROM AddressTable
GROUP BY left(place_name, 4), left(place_street, 4)
The count(*) column isn't necessary, but it gives you some idea of which values might have the most (possibly) duplicate address rows in common.
I would recommend you look into Fuzzy Search Operations in SQL Server. You can match the results much better than what you are trying to do. Just google sql server fuzzy search.
Assuming at least SQL Server 2005 for the CTE:
;with cteCommonAddresses as (
select left(place_name, 4) as LeftName, left(place_street,4) as LeftStreet
from Address
group by left(place_name, 4), left(place_street,4)
having count(*) > 1
)
select a.id, a.place_name, a.place_street
from cteCommonAddresses c
inner join Address a
on c.LeftName = left(a.place_name,4)
and c.LeftStreet = left(a.place_street,4)
order by a.place_name, a.place_street, a.id

CONTAINS sql function doesn't match underscores

I have some table 'TableName' like Id,Name:
1 | something
2 | _something
3 | something like that
4 | some_thing
5 | ...
I want to get all rows from this table where name containes 'some'.
I have 2 ways:
SELECT * FROM TableName WHERE Name like '%some%'
Result is table :
1 | something
2 | _something
3 | something like that
4 | some_thing
But if I use CONTAINS function
SELECT * FROM TableName WHERE CONTAINS(Name,'"*some*"')
I get only
1 | something
3 | something like that
What should I do to make CONTAINS function work properly?
The last time I looked (admittedly SQL Server 2000) CONTAINS didn't support wildcard matching at the beginning of words, only at the end. Also, you might need to check your noise files to see if the "_" character is being ignored.
Also see
How do you get leading wildcard full-text searches to work in SQL Server?
http://doc.ddart.net/mssql/sql70/ca-co_15.htm
If you read this article you will see that * means prefix this means that word must start with this, but like means the word contains key phrase.
Best Regards,
Iordan
Try this:
SELECT * FROM TableName WHERE CONTAINS(Name,'some')

Oracle in-line method to produce CSV for relation

I have the following tables:
ALERT (ID,Name)
1 | Alert A
2 | Alert B
ALERT_BRAND_XREF (ALERT_ID, BRAND_ID)
1 | 1
1 | 2
2 | 1
BRAND (ID, NAME)
1 | Brand A
2 | Brand B
I am trying to write one statement to return a list of alerts with the applicable brands as a CSV list in one field. Desired results:
Alert A | Brand A, Brand B
Alert B | Brand A
Is there a way to do this without writing a separate function? I would like to do it in one self-contained SQL statement if possible.
This is Oracle 9i.
In MySQL this would be easy with the GROUP_CONCAT() function, but it looks like to do the equivalent in Oracle it's a little messy:
Oracle group_concat() updated (again)
Look to this solutions, its very useful. Using SYS_CONNECT_BY_PATH and analytic functions.
Here's another way to turn a repeating group into a comma setarated list.
It uses the MODEL clause of Oracle's dialect of SQL. (Oracle 10g)
http://plsqlnotes.blogspot.com/2007/09/using-model-for-generating-csv-by_2227.html#links
(Replaces my previous wrong answer).
In oracle you can use wm_concat()
SELECT a.Name , wm_concat(b.Name) FROM
ALERT a,
ALERT_BRAND_XREF abx ,
BRAND b
where a.id = abx.ALERT_ID
and abx.BRAND_ID = b.id
group by a.Name;