Been a while since I put a query together but I'm stuck getting my head around something. I want to run a query that using multiple wheres and returns two random examples of each. I can get it to work individually but I have over 200 of these to run and a combined report would be so much easier in case I need to re-run. Any help appreciated!
SELECT PRODUCT, CONTRACT, NUMBER
FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
(PRODUCT, CONTRACT, NUMBER, RAND_NO)
Where PRODUCT = 'M2'
ORDER BY RAND_NO
FETCH FIRST 2 ROWS ONLY
UNION
SELECT PRODUCT, CONTRACT, NUMBER
FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
(PRODUCT, CONTRACT, NUMBER, RAND_NO)
Where PRODUCT = 'M3'
ORDER BY RAND_NO
FETCH FIRST 2 ROWS ONLY
UNION
etc . . . .
4 29750
Been a while since I put a query together but I'm stuck getting my head around something. I want to run a query that using multiple wheres and returns two random examples of each. I can get it to work individually but I have over 200 of these to run and a combined report would be so much easier in case I need to re-run. Any help appreciated!
SELECT PRODUCT, CONTRACT, NUMBER
FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
(PRODUCT, CONTRACT, NUMBER, RAND_NO)
Where PRODUCT = 'M2'
ORDER BY RAND_NO
FETCH FIRST 2 ROWS ONLY
UNION
SELECT PRODUCT, CONTRACT, NUMBER
FROM (PRODUCT, CONTRACT, NUMBER, RAND() FROM db2) Q
(PRODUCT, CONTRACT, NUMBER, RAND_NO)
Where PRODUCT = 'M3'
ORDER BY RAND_NO
FETCH FIRST 2 ROWS ONLY
UNION
etc . . . .
Hi Dan
I'm not familar with DB2 SQL Syntax but if you are then we can look at this from an optimisation point of view. I'm going to ask someone else to look at it as well.
Mary
NeoPa 32,556
Expert Mod 16PB
I honestly have great difficulty following the logic of what's been posted.
However, assuming a table called db2 I've put something together that might fit what you may be asking for :confused:
If not, it should at least be a base from which to build. - SELECT Product,Contract,Number
-
FROM (SELECT TOP 2 Product,Contract,Number
-
FROM db2
-
WHERE Product='M2')
-
UNION SELECT Product,Contract,Number
-
FROM (SELECT TOP 2 Product,Contract,Number
-
FROM db2
-
WHERE Product='M3')
-
UNION SELECT Product,Contract,Number
-
FROM (SELECT TOP 2 Product,Contract,Number
-
FROM db2
-
WHERE Product='M4')
-
...
I suppose I've been confusing everyone really.
To clarify.
I have a table with 5 million rows.
Each row has type, e.g. M1, M2, M3 etc although the naming is not so uniform
For testing I need to pull 2 random examples of each from the table and there are over 200 types to be tested. I can pull two samples of each type using my first query but no more. Repeating this two hundred times would take forever considering that it takes approx 5 mins to run a query on this table.
Hope this helps somewhat .. .
Dan
NeoPa 32,556
Expert Mod 16PB
Dan,
That certainly clarifies things.
That's the good news :(
The method I posted earlier is the only one I can think of (atm at least) which even does this type of selection. I do appreciate your reservations about it mind you. There's no SQL statement that I know of to say 'Return rows in a balanced order, ensuring, on a particular field, that items don't repeat until all other items within that field have shown'.
Using this method, you could build up the SQL in code, then rely on the SQL optimiser to make it work efficiently. I'm pretty sure it won't mind you :(
I'll bear your question in mind if anything else springs to mind.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Simon Elliott |
last post by:
I'd like to do something along these lines:
struct foo
{
int i1_;
int i2_;
};
struct bar
{
|
by: Neil Zanella |
last post by:
Hello,
I would like to know what the C standards (and in particular the C99
standard) have to say about union initializers with regards to the
following code snippet (which compiles fine under...
|
by: Barry Schwarz |
last post by:
Given a union of the form
union {
T1 m1;
T2 m2;}obj;
where T1 and T2 are different scalar (non-aggregate) types.
The C99 standard states that
obj.m1 = value;
if (obj.m2 ...
invokes...
|
by: Denis Pithon |
last post by:
Hi, C lovers!
I stuck on an union problem
Here is snippet of my code
....
/* two pointers of function with repsectively one and two argues */
typedef int (*dce_sn_f)(dce_t*);
|
by: Peter Dunker |
last post by:
Hi,
I will write ANSI C89.
Is the following struct defenition correct ?
I wrote it with VC6(Windows IDE) and at first no Problem.
As I changed a compiler switch to 'no language extension',
the...
|
by: Sean Dolan |
last post by:
typedef struct ntt {
int type;
union {
int i;
char* s;
};
}nt;
nt n;
n.i = 0;
|
by: ranjeet.gupta |
last post by:
Dear ALL
As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and...
|
by: Yevgen Muntyan |
last post by:
Hey,
Why is it legal to do
union U {unsigned char u; int a;};
union U u;
u.a = 1;
u.u;
I tried to find it in the standard, but I only found that
|
by: wugon.net |
last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad
query performance
Env:
db2 LUW V8 + FP14
Problem :
We have history data from 2005/01/01 ~ 2007/05/xx in single big...
|
by: SRK |
last post by:
Hi,
I wanted to use an anonymous union within an structure something like
below -
struct Test
{
union
{
std::string user; //char user;
std::string role; //char role;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |