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

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

Related

Firebird select distinct with count

In Firebird 2.5 I have a table of hardware device events; each row contains a timestamp, a device ID and an integer status of the event. I need to retrieve a rowset of the subset of IDs with non-0 statuses and the number of instances of the non-0 events for each ID, within a specified date range. I can get the subset of IDs with non-0 statuses in the specified date range, but I can't figure out how to get the count of non-0-status rows associated with each ID in the same rowset. I'd prefer to do this in a query rather than a stored proc, if possible.
The table is:
RPR_HISTORY
TSTAMP timestamp
RPRID integer
PARID integer
LASTRES integer
LASTCUR float
The rowset I want is like
RPRID ERRORCOUNT
-------------------
18 4
19 2
66 7
The query
select distinct RPRID from RPR_HISTORY
where (LASTRES <> 0)
and (TSTAMP >= :STARTSTAMP);
gives me the IDs I'm looking for, but obviously not the count of non-0-status rows for each ID. I've tried a bunch of combinations of nested queries derived from the above; all generate errors, usually on grouping or aggregation errors. It seems like a straightforward thing to do but is just escaping me.
Got it! The query
select rh.RPRID, count(rh.RPRID) from RPR_HISTORY rh
where (rh.LASTRES <> 0)
and (rh.TSTAMP >= :STARTSTAMP)
and rh.RPRID in
(select distinct rd.RPRID from RPR_HISTORY rd where rd.LASTRES <> 0)
group by rh.RPRID;
returns the rowset I need.

SQL script can sum the digits of a number

I created a table with a column containing numbers listed from 1 to 100. I want to delete numbers that divide by 3 without any remainder. Who can recommend me a way (script) to do that. Only logic I could make in this problem is that if the sum of digits of a number can divide by 3 that means any number which correspond to that case could be divisible.
If you are looking to delete the rows with number divisible completely by 3, you can use built-in modulus function
You could say something like this
delete
from myTable
where colNumber%3 = 0
This query should solve your problem
DELETE FROM table WHERE (id % 3) = 0;

Conversion of SQL Server to Oracle Select query

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.

SQL to return one row for each distinct value of a column (do not mind which row)

I have a table with a column named X. X contains number from 0 to 99. But there are duplicates (e.g. 0 is there multiple times! )
Now I need a query that gives any of the rows with 0,1,2,3...99 meaning I get 100 results at with one query, but I don't care which of the x==0 , x==1 ... I get, but just one of them!
Is there such thing in sql?
select distinct x
from your_table
To get a complete record you can group by the X column. But you have to tell the DB which of the duplicate values of the other columns you want.
select x, min(y) as y
from your_table
group by x
If you build a group by X then this value will be distinct. For the other columns you need a so called aggregate function like for example min(). That tells the DB to pick the minimum Y of every X group.

How do I limit the number of records returned for Interbase 7.1?

We have an Interbase 7.1 database and I'm trying to figure out how to limit the number of records returned by the query to just 1. I really only need to know an event code from the last record, in which the query will return hundreds of records if I cannot do some kind of limit.
Thanks in advance!
I think I figured it out. Needed to do something like this...
SELECT * FROM table ORDER BY col ROWS 1
As per the accepted answer:
SELECT * FROM table ORDER BY col ROWS 1
Will return just one result.
There are also several other row limiting options available:
ROWS n Returns the first n rows of the result set, or n percent if used with PERCENT
ROWS m TO n Returns rows m through n, inclusive or the mth to nth percent
ROWS n BY p Returns every pth row of the first n rows
This is particularly handy for paged results.
From the Embedded SQL Guide on the InterBase Product Documentation page: