Fastest query for random row from subset of large table - postgresql - ruby-on-rails

I am using rails and Postgresql. I have a very large table that includes a column called status. From this status column a lot of rows have a value of 1. I want to select a random row based on all rows with a value of 1 in the status column. Each row also has an id and they are sequential.
What is the fastest way to get this random row?

Try to use ActiveRecord solution, e.g :
Model_name.where(status: 1).limit(1).order("RANDOM()")
OR :
Model_name.where(status: 1).order("RANDOM()").first
Hope this helps.

You can play with OFFSET option
SELECT * FROM users WHERE status = 1 OFFSET random()*I LIMIT 1;
The I is the number of rows in table.

Related

Query and its total row

I have a table generated by a query with data drawn from another sheet. At the end of of the table, I am adding a total-row.
When the rows generated by the query is more, total-row restricts the table. Is there any way to detect the number of rows generated by the query and insert more rows above the total-row?
I have the number of rows generated by the query displayed in a cell and row number of total-row in another cell. Can a macro be triggered when the first is more than the second?
Regards,
Pravin Kumar.
It is not possible to append the total row, since it depends on the query result and it will generate a redundancy problem. Any solution to that?
add it into your query in array {}. lets say your QUERY outputs 3 columns and 3rd column holds the sum:
={QUERY(A:C, "select A,B,sum(C) where A = 'xx' group by A,B label sum(C)''", 0);
"Total", "", QUERY(A:C, "select sum(C) where A = 'xx' label sum(C)''")}
or to add row between:

Get column headers with query language

I'm using the query language to query data from spreadsheet.
I would like to retrieve the first row(column headers), how do I do that?
Currently I'm using: select * where ( A = -1 )
, the data in A column is never equal to -1, so it returns only column headers.
Is there a straightforward way to do this?
You can use query(A:Z, "select * limit 0", 1) meaning: select all, return at most 0 rows. The result is that only the header row is returned (the 3rd parameter is to make it clear there is 1 header row).
But it's not really natural to use query for this purpose. The function array_constrain is provided for the purpose of truncating an array of data. For example,
=array_constrain(A:Z, 1, 1e7)
returns the first row of the given array. (Since no limit on the number of columns is needed, I gave 1e7 = 10,000,000 as the maximal number of columns. A spreadsheet can't even have that many cells.)

Printing sheets of labels in SSRS 2008 based on data in a table and specific quantities per label

I need to come up with a form in SSRS 2008 that prints labels based on information stored in a table or tables. So far I have been unsuccessful in my online searching. How do I tell SSRS that I want the KitOrder.QuantityCommitted quantity of labels to print for each item? (each item has it's own quantity) I will likely be printing labels for 20-30 items at a time. In case it's helpful to know, there are 18 blank labels per sheet.
edited 10/01/15
Sorry, I played around with with the example you gave and wasn't able to get my report to work. I am not an expert with SSRS.
I have 2 tables I am using to get the info I need for the labels and only need the columns listed below the table names at this point.
KitOrder INNER JOIN Item
KitOrderNumber ItemNumber
Quantity ItemDescription
I am using KitOrderNumber from a dropdown box - IN(#KitOrderNumber) - as my Parameter. The ItemNumber and ItemDescription print on the label. Would there be some custom code I could use that would return 3 labels when KitOrder.Quantity = 3 instead of just 1 label?
Edited 10/05/15 - I was finally able to get my report to work as needed. Thanks so much for your help. The sqlfiddle was quite helpful to me.
I hate when people suggest this when it's not necessary but I think this is one of the times that you'll need to manipulate your data in SQL before the report.
I can't think of an easy way to do it with SSRS that doesn't involve a lot of code.
You create a temp table with your possible quantities then cross join your table to create a separate record for each product based on the number in your Quantity field.
;WITH Quantities AS (
SELECT 1 AS Num
UNION ALL
SELECT 1 + Num
FROM Quantities
WHERE Num <= 30
)
Select * from Products
CROSS JOIN Quantities
WHERE Num <= Quantity
This assumes that your data is in a table called Products.
If you have a query, you can SELECT your fields INTO a #Temp table and use that in the Select statement.
For the report, you can set the Columns peoperty on the Report to use as many columns as your label sheet has.
Here's an SQLFiddle I made that you can play with:
http://www.sqlfiddle.com/#!3/bb413/2

How to find duplicates of a number in excel?

I have 2 columns of account numbers. The first column has far more numbers than the second column. I need to know if the numbers in column 1 show up anywhere in column two.
Try
=VLOOKUP(A1,B:B,1,0)
in a third column, copied down to all rows populated in ColumnA.

sqlite - replacing existing rows in particular range with data from another table

I have table with 1500 rows and nearly 10 columns ..
I have imported new data from csv file into another new table, now I want to copy data from this new table to already existing table's 10th column ..
when I try
insert into presentDB (column10) select column1 from importDB
its inserting another 1500 new rows ..
UPDATE presentDB SET column10 = importDB.column1
this thing is throwing an error ..
UPDATE presentDB SET column10 = (SELECT column1 FROM importDB WHERE rowid between 1 and 1500)
gets executed, but copies the first value in column1.importDB to all the cells in present db.
also what will be the query for
copying data in particular range like 100 to 200 from importDB to 500 to 600 in presentDB
can anyone please show a proper direction :) thanks, any help is appreciated :)
You need to have some column that identifies records.
If you have that, you can use a correlated subquery:
UPDATE presentTab
SET column10 = (SELECT column1
FROM importTab
WHERE importTab.ID = presentTab.ID)

Resources