473,889 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Db2 Union

2 New Member
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 . . . .
Feb 13 '07 #1
4 29771
MMcCarthy
14,534 Recognized Expert Moderator MVP
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
Feb 14 '07 #2
NeoPa
32,584 Recognized Expert Moderator MVP
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.
Expand|Select|Wrap|Line Numbers
  1. SELECT Product,Contract,Number
  2. FROM (SELECT TOP 2 Product,Contract,Number
  3.       FROM db2
  4.       WHERE Product='M2')
  5. UNION SELECT Product,Contract,Number
  6. FROM (SELECT TOP 2 Product,Contract,Number
  7.       FROM db2
  8.       WHERE Product='M3')
  9. UNION SELECT Product,Contract,Number
  10. FROM (SELECT TOP 2 Product,Contract,Number
  11.       FROM db2
  12.       WHERE Product='M4')
  13. ...
Feb 14 '07 #3
danindublin
2 New Member
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
Feb 14 '07 #4
NeoPa
32,584 Recognized Expert Moderator MVP
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.
Feb 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
8164
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
3340
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 gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
2
4364
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 undefined behavior because my reference to the union is via a
10
5085
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*);
2
5065
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 compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
73
4082
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
2387
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 compiler), and it is, what I conclude from the below code union data_type {
30
3306
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
5
3855
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 table, we try separate this big table into twelve tables and create a view
3
7730
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;
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11203
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10794
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10443
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4650
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.