Using postgresql to flag a word after a phrase - sql

I have a field called TEXT1 which contains the following text below in quotes. Is there a way where I can flag for instances where the word orange is being mentioned AFTER the Excluding This portion of the text?
I've tried doing where TEXT1 is not like %orange% but that would get rid of instances where the Including This portion of the text contains the word orange and the Excluding This portion didn't contain the word orange. Basically I need help with a function that will flag for when the word orange is mentioned after the phrase Excluding This within the TEXT1 field in my table.
"Including This
The best fruit is an orange
Excluding This
The worst fruit is an orange
"

Sounds like a job for full text search:
WHERE to_tsvector('english', textcol) ##
to_tsquery('english', 'The <-> best <-> fruit <-> is <-> an <-> orange & ! (The <-> worst <-> fruit <-> is <-> an <-> orange)')

Related

Power BI - Is there a way to conditionally format a text column if it does NOT meet a criteria?

as the title states, is there a way to conditionally format a text column if it does NOT meet a criteria - In Power BI?
i.e: Format a column's font colour if it is NOT "x".
e.g:
I want to format the following data (using the letter column) and highlight a, b and c (as they are not equal to x)
ID | code | letter
------------------
1 | 123 | a
2 | 345 | b
3 | 567 | x
4 | 789 | c
------------------
You can always create a measure to determine the desired highlight color for example, and use it, e.g. like this:
Measure = If(FIRSTNONBLANK('Table'[letter]; [letter]) = "x"; BLANK(); "#AABBCC")
See Color formatting by field value:
You can use a measure or a column that specifics a color, either using a text value or a hex code, to apply that color to the background of font color of a table or matrix visual. You can also create custom logic for a given field, and have that logic apply the desired color to the font or background.
You didn't mention what do you want to highlight and how. If the data will not be aggregated and show all rows in a table, you can calculate the highlighting color in conditional column in Power Query Editor, or computed column (DAX). If the highlighting rules will be applied on aggregated data, then you should use DAX measure.
If you want to highlight a cell, return the desired color. Otherwise return null in Power Query Editor, or BLANK() in DAX.

Parse data from Morningstar Direct to worksheet

I have to put together a report every quarter using data pulled off of Morningstar Direct. I have to automate the whole process, or at least parts of it. We have put this report together for the last two quarters, and we use the same format each time. So, we already have the general templates for the report - now I'm just looking for a way to pull the data from Morningstar and putting into the templates correctly.
Does anyone have any general idea where I should start?
A B C D E F
Group Name Weight Gross Net Contribution
Equity 25% 10% 8% .25
IBM 5% 15% 12%
AAPL 7% 23% 18%
Fixed Income 25% 5% 4% .17
10 Yr Bond 10% 7% 5%
Emerging Mrkts
And it goes on breaking things into more groups, and there are many more holdings within each group.
What I want it to do is search until it finds "Equity", for example, and then go over one row, grab the name of the position, its weight, and its net return, and do that for each holding in Equity. The for it to do the same thing in Fixed Income, and on and on - selecting the names, weights, and nets for each holding. Then copy and pasting them into another workbook.
Anyway that is possible?
It sounds like you need to parse your information. By using left(), right(), and mid() you can select the good data and ignore the superfluous. You could separate the data in one cell into multiple cells in the desired format.
A B
Name Address
John Q. Public 123 My Street, City, State, Zip
E (First Name) F (Middle Initial) (extra work to program missing data)
=LEFT(A2,FIND(" ",A2)) =MID(A2,LEN(E2)+1,FIND(" ",MID(A2,LEN(E2)-1,99)))
G (Last Name) H (City)
=MID(A2,(LEN(E2)+LEN(F2)+2),99) =MID(B2,LEN(H2)+2,FIND(",",MID(B2,LEN(H2)+2,99))-1)
I (State)
=MID(B2,(LEN(I2)+LEN(H2)+4),FIND(",",MID(B2,(LEN(I2)+LEN(H2)+4),99))-1)
J (Zip Code)
=MID(B2,(LEN(H2)+LEN(I2)+LEN(J2)+6),99)
This code will parse the name in the cell A2 and address in cell B2 into separate fields.
Similar cuts should allow you to get rid of the unwanted data.
==================================================================
7/8/2015
Your data seems to be your desired output. If so, please provide sanitized input data for comparison. You probably need to loop through your input to find the groups. When the group changes, prepare the summary figures.

Line wrapping with white space padding using stringtemplate

How can I pad a sentence with white space so that it is printed in a 'block'.
I want to print a receipt. for a given item I want to print the quantity, item name, and price.
12 x Example short name £2.00
1 x This is an example of
a long name £10.00
The 'block' to which I refer to is shown below.
12 x |Example short name | £2.00
1 x |This is an example of |
|a long name | £10.00
Using the example above I can easily handle the formatting of the quantity and the price. Using string formatting. Then for the item name the best method to use, I think, is to split the item name in code, and use the wrap functionality of StringTemplate, but I don't know how to pad the remainder of the line with whitespace
I am using .Net and StringTemplate 4. Here is a simplified version of the template I have. Assuming I have an item with the properties Quantity, ItemName (split into an array of strings), and Price.
<item.Quantity; format="{0,4}"> x <item.ItemName; wrap, separator=" ", anchor><item.Price; format="£{0,8}">
Now at the moment the only way I can think to get it to work is to calculate the white space in code and add it to the item name array.
So is there a neat way to do it in StringTemplate?

write data to oracle database, but first check it in mapping table

Here is what I want to do:
I have 2 tables in oracle database. First one for my data, and second for mapping.
Mapping table has 2 columns and it's like this:
olive - green
marine - green
grass - green
green - green
navy - blue
sky - blue
light blue - blue
etc.
First table (for my data) has more columns, and I should fill every row in that table with various number of colors from mapping table.
and I have txt file with this:
olive,
navy,
green,
#
blue,
sky,
olive,
marine,
#
blue,
light blue,
#
etc.
So, I should basically write that data to my first table in database, but not with those values from txt file, but with adequate value from mapping table.
I hope I explained it well enough. Help pls?

Multi-parameter matching + weighted random pick in Redis

Let's say I have a set of objects with properties:
Object Quantity Color Shape Kind
----------------------------------------
APPLE 12 RED ROUND FRUIT
APPLE 3 GREEN ROUND FRUIT
ORANGE 6 ORANGE ROUND FRUIT
CARROT 0 RED CONICAL VEGETABLE
RADISH 24 RED ROUND VEGETABLE
Object and all properties except quantity are represented as strings. Quantity is a number.
I must compose a random list of objects, based on user's query.
Query contains values for all string properties (that is, all properties except quantity).
Value in query may be either exact property value, or a wildcard (meaning "any value would do for this property"), or a negation — "NOT this exact property value".
Query result is an object, picked by weighted random from all object with matching properties. Weight for the random pick is the quantity.
For example:
Query -> Probabilities -> Example
random result
-----------------------------------------------------------------------------
* ROUND FRUIT -> APPLE 12 / APPLE 3 = APPLE 15 -> APPLE
!GREEN ROUND FRUIT -> APPLE 12 / ORANGE 6 -> ORANGE
RED * * -> CARROT 0 / APPLE 12 / RADISH 24
= APPLE 12 / RADISH 24 -> RADISH
RED CONICAL VEGETABLE -> CARROT 0
= (none) -> (none)
For self-education purposes, I would like to build this system using Redis for data storage.
The question is — how to do this elegantly and with least amount of application logic (as opposed to in-Redis operations)? Weights and negation kind of spoil the picture. Otherwise it would be nicely doable with sets.
Any hints are welcome.
Since redis can only query keys and not values, a good option is to store the individual values of each object in seperate redis lists.
For example, when you add the object ...
APPLE 12 RED ROUND FRUIT
you would store it as
hmset obj:1 name apple qty 12 color red shape round kind fruit
and then ...
sadd name:apple obj:1,
sadd color:red obj:1
sadd shape:round obj:1
This way you have a way to interrogate sets directly and be able to pick the object using a random number based on, for example, the total number of items in the set returned.
Hope that helps. If you need more explanation, hit me up.