How to apply regular expression on product SKU field in magento - regex

I have let says 3 products
P1 with SKU "xyz_1" and price "10"
P2 with SKU "xyz_2" and price "20"
P3 with SKU "xyz_3" and price "50"
I want a query in magento to get sku xyz with minimum price i.e xyz_1 only.
I want something like this select ,Regex('SKU','myexpression') as SKU to remove last part from i.e "_" part from sku and then apply filter on product collection to get product with minimum price.
Any Ideas how to handle that with Magento?

You could this regex: ^xyz. This will match any string which starts with xyz and ignore what follows.
That being said, you would need to modify your query to sort the results by price since regular expressions do not cater for numerical comparisons (less then, greater than, etc).
EDIT: As per your question, you could adapt some code from here into this:
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT SUBSTRING(sku, 0, INSTR(sku, '_')) AS SKU FROM <your table> WHERE sku REGEXP '^<your sku>' ORDER BY price asc LIMIT 1;");
SELECT INSTR('foobarbar', 'bar');

Related

Trying to replace data based on attributes from two other columns

I need to change the inventory category for a couple of account numbers and only for a couple of companies. The inventory category for these accounts are mapped based on the account number but need to be changed specifically just for two companies. I've tried to filter by the company number and then find/replace, which worked fine, but then I can't unfilter to bring back the rest of the companies. I can't change the category for just those account numbers because it is only different for just those two companies.
Lisa, Here's perhaps a simpler approach than where your current way is taking you.
If I begin with this table:
Then I add a column (Add Column -> Custom Column) with the following:
The formula uses an if statement to determine whether each row has a specific Account (Acct. 4) AND Company (Co. 8). If so, then 99 is returned as a new category value for that row of the new column. If not, then the original Inventory Category is returned as a value for that row of the new column. (Obviously, you would edit this formula accordingly, to support your account, company, and new inventory category values.)
Here's the result:
Then I could delete the original Inventory Category column and rename the remaining New Inventory Category column to Inventory Category.

AWS LogInsights query stats average of count

I have cloudwatch entries that may be group with respect to a certain field. To be clear assume that field is city. I would like to count the entries with respect to cities. This is the easy part.
fields city
|stats count(*) by city
However I also want to get maximum minimum and average of this count, but I can not. Is it possible to have such queries i.e:
fields city
|stats avg(count(*) by city)
The console return an error for such query : mismatched input 'by' expecting {SYM_COMMA, SYM_RParen}
Here's how you'd do it: You'd first get the count (that you already figured) and then get the metrics you want by calling the relevant functions like so:
fields city
|stats count(*) as cityCount, avg(cityCount), max(cityCount), min(cityCount)

Get total count of each distinct value

If I for example have a column of countries that might repeat and the list follows like this: Spain, Spain, Italy, Spain
I want to get the result that I take the number that a country appears in the column and divide it by total number. I have tried:
CountRows = DIVIDE(DISTINCTCOUNT('Report (7)'[Country]); COUNT('Report (7)'[Country]) )
Any suggestions? do I need a new column for that?
The easiest way to achieve this type of calculation is to add one column with the number of occurrence of the selected words divided by the number of row in the table.
You need to use the function Earlier to get the context.
If you have one table named Table1 and your column Country
Something like :
Divide(COUNTROWS(FILTER(table1, Table1[Country] = EARLIER(Table1[Country]))),COUNTROWS(Table1))
Don't forget to put your new column in Percentage type or add some decimal to see the correct data.

Netsuite Saved Search a list of ID's by invoice No

I have a request to extract specific Invoices, over the course of an year. For target invoices i have the following information to use: Invoice Number, Invoice date, Order Ref, Value and currency. I tried extracting few months at a time, but it's too much data.
Is there a way to filter on about 200 unique Invoice No in Netsuite?
Thank you,
Daniel
You can do this with a Formula(Numeric) filter in your criteria like this:
Filter: Formula (Numeric)
Description: is greater than 0
Formula: INSTR(',SLS00000101,SLS00000102,SLS00000103,SLS00000104,SLS00000105,', ',' || {tranid} || ',')
Note that the initial string is a comma separated list of document numbers that begins and ends with a comma.
I'm not sure if there is an upper limit on formula size, but I've used this pattern to find a large number of transactions when I know the document numbers or internal IDs.

if statement function for 1 value but for more than 1 validation

i have some problem when i created a calculated field in tableau.
lets say i have this "City" variable with these kind of data:
1
2
3
4
5
6
7
8
i want to make validate this number into if 1-4 become Region A, 5-8 become Region B and 1-8 is National.
this is my calculated filed code
if [City]=1 or [City]=2 or [City]=3 or [City]=4 THEN "Region A"
elseif [City]=5 or [City]=6 or [City]=7 or [City]=8 THEN "Region B"
elseif [City]=1 or [City]=2 or [City]=3 or [City]=4 or [City]=5 or [City]=6 or
[City]=7 or [City]=8 THEN "National"
END
but after i created this calculated field, only "Region A" and "Region B" and "Null" showed. My thought is maybe that code is overlapping on Region with National (or is not?).
How can i solve this? and where this null come from when my data only have the range from 1-8?
Thank you
edited :
Hi all, just want to confirm that the way i wanted it to look like is with 1 calculated field, when I clicked "Region A" it will show combination of city 1-4, when I clicked "Region B" it will show combination of city 5-6, and when i clicked "National" it will show combination of city 1-8.
Edit: After the clarification in the comments, what you need is a parameter. Create one that can take the values "Region A", "Region B" and "National".
Then create a calculated field with (I didn't use all city values to make it more readable):
CASE [parameter]:
WHEN "Region A" THEN IF [city] = 1 or [city] = 2 THEN [value] END
WHEN "Region B" THEN IF [city] = 3 or [city] = 4 THEN [value] END
WHEN "National" THEN [value] END
END
With the parameter you can now choose the grouping. For [value] you will need to insert the column you want to display (ie. Sales). If [city] has a value that is according to your criteria, it will populate the new calculated field with the value, if it is not, it will be empty, and if it is "National" then it will always use the value you defined.
Old Answer:
It sounds like you want to create a hierarchical structure to display your data on city, region and country level.
If that is the case you have a few options:
You have actual city names instead of 1, 2, 3 in your city column: Tableau will create the hirachy automatically if you define the column as a geographic role.
You have 1,2,3 but only one country: create one field Region with the formula you already have but leave the last ELSEIF out:
if [City]=1 or [City]=2 or [City]=3 or [City]=4 THEN "Region A"
elseif [City]=5 or [City]=6 or [City]=7 or [City]=8 THEN "Region B"
end
Then create a second calculated field Country and just give it the value 'National'
In your dimensions you can now create a hirachy by dragging and dropping th region and city field on top of the country field.
You have 1,2,3 and several countries: replace the value in the second calculated field with this formula and create the hirachy as in 2.:
if [City]=1 or [City]=2 or [City]=3 or [City]=4 or [City]=5 or [City]=6 or
[City]=7 or [City]=8 THEN "Country1"
elseif [City]=9 or [City]=10 or [City]=11 THEN "Country2"
END
A few further notes on that:
You probably want to use a CASE instead of that many IF, as it is easier to read and better to maintain
You might want to base it on the regions instead of the cities since you will only need to maintain eg 2 Regions for your original example versus 8 cities.