I have text field in my pgsql DB which contains users identificators. One field can contain one or more ids separated by #. On saving numbers are imploded using PHP, so I can have for example:
Row 1: 3#36#66#33
Row 2: 5#56#33#55.
How should I prepare SQL query to select field with one or more ids without any ambiguity?
Example:
SELECT product FROM cms_product WHERE product_mod LIKE '3'
Both row 1 and 2 can be selected which I don't want. How I can fix it?
EDIT:
I will have to select all rows that contains user ID in this filed, for example:
UserId: 3
Field 1:
55#64#333#2
Field 2:
12#3#55#6423
Field 3;
654#33#11#98
Only field 2 should be selected.
Although you should really work to normalize your DB this is the answer to the posted question
select product
from cms_product
where '3' = any(regexp_split_to_array(product_mod, '#'))
Related
I am pulling data and when I pull in the text field my results for the "distinct ID" are sometimes being duplicated when there are multiple results for that ID. Is there a way to concatenate the results into a single column/row rather than having them duplicated?
It looks like there are ways in other SQL platforms but I have not been able to find something that works in HANA.
Example
Select
Distinct ID
From Table1
If I pull only Distinct ID I get the following:
ID
1
2
3
4
However when I pull the following:
Example
Select
Distinct ID,Text
From Table1
I get something like
ID
Text
1
Dog
2
Cat
2
Dog
3
Fish
4
Bird
4
Horse
I am trying to Concat the Text field when there is more than 1 row for each ID.
What I need the results to be (Having a "break" between results so that they are on separate lines would be even better but at least a "," would work):
ID
Text
1
Dog
2
Cat,Dog
3
Fish
4
Bird,Horse
I see Kiran has just referred to another valid answer in the comment, but in your example this would work.
SELECT ID, STRING_AGG(Text, ',')
FROM TABLE1
GROUP BY ID;
You can replace the ',' with other characters, maybe a '\n' for a line break
I would caution against the approach to concatenate rows in this way, unless you know your data well. There is no effective limit to the rows and length of the string that you will generate, but HANA will have a limit on string length, so consider that.
In table A -> Column X there is some data which has numbers, alphabets and special characters. Most of the records has single line of data but some of them has 2 or 3 lines of data.
1 this is a sample description of data 01/11/2017 # 123'~
Records with two lines of data
1 this is a sample description
2 of data 22/11/2017 #~ 12##'
I need to do a select query to get the records which has 2 lines of data in Column X of table A.
I use TOAD and the above mentioned sample data is from the Grid popup editor
thanks
You could select those rows that contain a new line (do not know your sample data, either chr(10) or chr(13)):
select *
from tableA
where instr(columnX, chr(10)) > 0;
The solution is taken from this SO answer, please do not forget to upvote the linked solution if it helped you.
I have a table which contains geometry lines (ways).There are lines that have a unique geometry (not repeating) and lines which have the same geometry (2,3,4 and more). I want to list only unique ones. If there are, for example, 2 lines with the same geometry I want to drop them. I tried DISTINCT but it also shows the first result from duplicated lines. I only want to see the unique ones.
I tried window function but I get similar result (I get a counter on first line from the duplicating ones). Sorry for a newbie question but I'm learning :) Thanks!
Example:
way|
1 |
1 |
2 |
3 |
3 |
4 |
Result should be:
way|
2 |
4 |
That actually worked. Thanks a lot. I also have other tags in this table for every way (name, ref and few other tags) When I add them to the query I loose the segregation.
select count(way), way, name
from planet_osm_line
group by way, name
having count(way) = 1;
Without "name" in the query I get all unique values listed but I want to keep "name" for every line. With this example I stilll get all the lines in the table listed.
To expound on #Nithila answer:
select count(way), way
from your_table
group by way
having count(way) = 1;
You first calculate the rows you want, and then search for the rest of the fields. So the aggregation doesnt cause you problems.
WITH singleRow as (
select count(way), way
from planet_osm_line
group by way
having count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S
ON P.way = S.way
you can group by way and while taking the data out check the count=1.It will give non duplicating data.
#voyteck
As I understood your question you need to get only non duplicating records of way column and for each row you need to show the name is it
If so, you have to put all the column in select statement, but no need to group by all the columns.
select count(way), way, name
from planet_osm_line
group by way
having count(way) = 1;
I have a powerpivot table that contains 2 columns:
Column 1 contains strings.
Column 2 contains comma delimited strings.
I would like to be able to display all the rows from column 1 when rows from column 2 contains the selection from a filter or slicer. For example:
String Values
ABCD A,A,B
EFGH A,C
if A is selected I would display both rows, if B is selected I would display only row 1...etc.
I know I can split the records - but this is not practical for me - the above is only the top of the iceberg. VBA is out of the question since this will published in SharePoint. Anybody has an idea on how I could do that ? Thanks.
I found the solution in a blog from Javier Guillem:
http://javierguillen.wordpress.com/2012/02/10/simulating-an-approximate-match-vlookup-in-powerpivot/
If in my example the name of the table is "facts", I create a second unlinked table called dimRef that I populate with all possible values that I am interested to match: A,B,C,D...etc.
Then I define the measure M as:
M:=If ( Hasonevalue(facts[Values] ),
Calculate (
LASTNONBLANK (dimRef[String], 1 ),
Filter ( dimRef, SEARCH(dimRef[String],Values(facts[String]),1,0) > 0 )
)
)
I can then use the string column of the facts table and the measure in a pivot table and use dimRef as a selector. If filters the row as per the selection.
One small detail: the measure is not available in PowerView...Anybody knows why ?
I have an entry "123456789" in my table.
Select * from map where col like '%1%5%6%7%9'.
I want to retrieve the records where the order of the input sequence matches, but i also want to ensure that the distance between any 2 matching digits is less than 2.
Is there any way i can specify an offset ?
The input is 189, and it selects the record, but i want 1.8.9 to be within 2 of each other. 12879 would be an acceptable output but 123456789 would not be.
Below statement requires 3 to 5 characters between 1 and 5:
SELECT * FROM map WHERE col LIKE '%1___%5%6%7%9' AND col NOT LIKE '%1______%5%6%7%9%'
Using _s you may force any count of characters.
EDIT: Character corrected. Source: SQLite expression
Check in this SQL Fiddle sample.