Conversion of SQL Server to Oracle Select query - vb.net

I have a Select query:
SELECT
SAMPLE_NUMBER, SAMPLE_TYPE, STORAGE_ADDRESS, EXTERNAL_NUMBER
FROM
SYSTMX2.TM2_SAMPLES
And what I need is to add two more columns with the results of some .Net syntax code so I would end up with something like"
SELECT
SAMPLE_NUMBER, SAMPLE_TYPE, STORAGE_ADDRESS, EXTERNAL_NUMBER,
SomeCodearound(STORAGE_ADDRESS) as RowPosition,
SomeCodearound(STORAGE_ADDRESS) as ColumnPosition
FROM
SYSTMX2.TM2_SAMPLES
The row and column positions are based upon where they would fall in a 9 x 9 Grid. 9 numbers for columns across the top and 9 numbers as rows down the side. This would be a lab specimen box that would hold 81 vials. Every vial has a number from 1 to 81 and is the last three characters of the STORAGE_ADDRESS value similar to FR2-S01-R01-001 or FR2-S01-R01-081. Vial number 1 would be in column 1 and row 1; vial 81 in row 9 column 9. My .net code for the row is to take the last three character of the STORAGE_ADDRESS and test with decimal.
TryParse(STORAGE_ADDRESS.Substring(STORAGE_ADDRESS.Length - 3), value)
and then take that value and convert with:
CInt(Decimal.Truncate(((value+ 9 - 1) / 9))).
And the column code is:
(value + 9) - (CInt(Decimal.Truncate(((value + 9 - 1) / 9))) * 9).
I need to make it into an inline Select statement that I can call from a web service to the Oracle server, I do not have any way to create anything database side. Right now what I do is to call the result recordset add a couple of columns and loop the results and add the values. I know there has to be a better way.

This Oracle query returns row and column positions:
select storage_address,
floor((to_number(substr(storage_address, 13, 3))-1)/9)+1 rowposition,
mod(to_number(substr(storage_address, 13, 3))-1, 9)+1 colpostion
from t
Here is SQLFiddle with test values.

Related

Calculated query with parameters in HANA/CrystalReports

I'm having trouble trying to explain my necessity, so I'll describe the scenario.
Scenario:
Product A has a maximum production of 125KG at a time.
The operator received a production order of 1027,5KG of product A.
The operator have to calculate how many rounds he'll have to
manufacture and adjust the components quantity for each round.
We want to create a report where this calculations are already done and what we believe would be the first step, based on the values of this scenario, is to return something like this:
ROUND QUANTITY(KG)
1 125
2 125
3 125
4 125
5 125
6 125
7 125
8 125
9 27,5
After that, the recalculation of the components could be done with simple operations.
The problem is that we couldn't think of a way to get the desired return and we also couldn't think of a different way of achieving the said report.
All we could do is get the integer part of the division
SELECT FLOOR(1027.5/125) AS "TEST" FROM DUMMY
and the remainder
SELECT MOD(1027.5,125) AS "TEST" FROM DUMMY
We are using:
SAP HANA SQL
Crystal Reports
SAP B1
Any help would be appreciated
Thanks in advance!
There are several ways to achieve want you described.
One way is to translate the requirement into a function that takes the two input parameter values and returns the table of production rounds.
This can look like this:
create or replace function production_rounds(
IN max_production_volume_per_round decimal (10, 2)
, IN production_order_volume decimal (10, 2)
)
returns table (
production_round integer
, production_volume decimal (10, 2))
as
begin
declare rounds_to_produce integer;
declare remainder_production_volume decimal (10, 2);
rounds_to_produce := floor( :production_order_volume / :max_production_volume_per_round);
remainder_production_volume := mod(:production_order_volume, :max_production_volume_per_round);
return
select /* generate rows for all "max" rounds */
s.element_number as production_round
, :max_production_volume_per_round as production_volume
from
series_generate_integer
(1, 1, :rounds_to_produce + 1) s
UNION ALL
select /* generate a row for the final row with the remainder */
:rounds_to_produce + 1 as production_round
, :remainder_production_volume as production_volume
from
dummy
where
:remainder_production_volume > 0.0;
end;
You can use this function just like any table - but with parameters:
select * from production_rounds (125 , 1027.5) ;
PRODUCTION_ROUND PRODUCTION_VOLUME
1 125
2 125
3 125
4 125
5 125
6 125
7 125
8 125
9 27.5
The bit that probably needs explanation is SERIES_GENERATE_INTEGER. This is a HANA-specific built-in function that returns a number of records from a "series". Series here is a sequence of periods within a min and max limit and with a certain step-size between two adjacend periods.
More on how this works can be found in the HANA reference documentation, but for now just say, this is the fastest way to generate a result set with X rows.
This series-generator is used to create all "full" production rounds.
For the second part of the UNION ALL then creates just a single row by selecting from the built-in table DUMMY (DUAL in Oracle) which is guaranteed to only have a single record.
Finally, this second part needs to be "disabled" if there actually is no remainder, which is done by the WHERE clause.

How to calculate dynamically a column containing a formula in SQL Server a return the value

I've been searching for this but I dont know how to ask for this specific information.
Lets imagine I have something like this in my sql table.
Columns:
width : 10
height: 15
Depth: 6
And I have a formula column like this
formula: (W*2)+H+D+3
where I replace W with Width, H with Height, and D with depth.
using sql Replace, I am able to do this and convert the final result as
(10*2)+15+6+3
The problem is that I need the calculation to be done in the column as a result.
I tried this direcly in SQL
select 1 as width ,
2 as height ,
3 as depth ,
'H*2+W' as formula,
replace(Replace(REPLACE('H*2+W','W',1),'H',2),'D',3) as result
I need to be able to do this directly in the query, and calculate each row result with its specific value.
I tried doing Execs inside Select, but no luck. this would run 1000 querys if my initial query has 1000 rows...
How can I make this the best possible way?
You can't easily do what you want. SQL Server does not have a convenient parsing mechanism for creating code from strings. You could use dynamic SQL and a cursor, but that is cumbersome.
However, you may be able to store your formulas using coefficents:
formula w_coeff h_coeff d_coeff constant
(W*2)+H+D+3 2 1 1 3
'H*2+W' 1 2 0 0
Then you can do:
select v.*, f.*,
(f.w_coeff * v.w + f.h_coeff * v.h + f.d_coeff * v.d + f.constant)
from (values (1, 2, 3)) v(w, h, d) cross join
formulas f;

SAP HANA random number generator

I want to write a statement that generates a random pick from a set of four values (2,4,6, and 8). Below is the select statement that I have so far
SELECT
CASE
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 0 AND 2.00 THEN 2
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 2.01 AND 4.00 THEN 4
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 4.01 AND 6.00 THEN 6
WHEN RN_GENERATOR.RANDOM_NUMBER BETWEEN 6.01 AND 8.00 THEN 8
END AS ORDER_FREQUENCY
FROM (SELECT ROUND(RAND()*8,2) AS RANDOM_NUMBER FROM DUMMY) RN_GENERATOR
is there a more intelligent way of doing this?
Looks to me as if your requirement can be fulfilled but his statement
select
ROUND(rand()*4, 0, ROUND_CEILING) * 2 as ORDER_FREQUENCY
from dummy;
RAND() * 4 spreads the value range of possible outcomes for the RAND() function from 0..1 to 0..4.
ROUND( ... , 0, ROUND_CEILING) rounds the number to the next larger or equal integer and leaves no decimal places. For this example this means that the output of this rounding can only be 1, 2, 3 or 4.
*2 simply maps the four possible values to your target number range 2, 4, 6, 8. If the multiplication wouldn't suffice, you could also use the MAP() function for this.
And that's it. Random numbers picked from the set of (2, 4, 6, 8).
You can randomly sort a data set by using
ORDER BY Rand()
and select first one as your random value
Here is an example
select top 1 rownum
from Numbers_Table(10) as nt
where rownum in (2,4,6,8)
order by rand();
Numbers_Table function returns a numbers table on HANA database and I filter only the values that you want to see as your possible random values using WHERE clause
TOP 1 clause in SELECT command returns the first integer which is randomly ordered
I hope it helps

SQL Select where id is in `column`

I have a column that has multiple numbers separated by a comma. Example for a row:
`numbers`:
1,2,6,66,4,9
I want to make a query that will select the row only if the number 6 (for example) is in the column numbers.
I cant use LIKE because if there is 66 it'll work too.
You can use like. Concatenate the field separators at the beginning and end of the list and then use like. Here is the SQL Server sytnax:
where ','+numbers+',' like '%,'+'6'+',%'
SQL Server uses + for string concatenation. Other databases use || or the concat() function.
You should change your database to rather have a new table that joins numbers with the row of your current table. So if your row looks like this:
id numbers
1 1,2,6,66,4,9
You would have a new table that joins those values like so
row_id number
1 1
1 2
1 6
1 66
1 4
1 9
Then you can search for the number 6 in the number column and get the row_id

SQL Server SQL Select: How do I select rows where sum of a column is within a specified multiple?

I have a process that needs to select rows from a Table (queued items) each row has a quantity column and I need to select rows where the quantities add to a specific multiple. The mulitple is the order of between around 4, 8, 10 (but could in theory be any multiple. (odd or even)
Any suggestions on how to select rows where the sum of a field is of a specified multiple?
My first thought would be to use some kind of MOD function which I believe in SQL server is the % sign. So the criteria would be something like this
WHERE MyField % 4 = 0 OR MyField % 8 = 0
It might not be that fast so another way might be to make a temp table containing say 100 values of the X times table (where X is the multiple you are looking for) and join on that