union

Unite two MySQL queries with a UNION or programmatically

I've got two MySQL queries that both insert data into a table. Both have the following format: CREATE TABLE IF NOT EXISTS `data` ( `id` BIGINT NOT NULL AUTO_INCREMENT UNIQUE, PRIMARY KEY (`id`) ) SELECT `field1`, `field2` WHERE `active` = 1 The only differences between the two queries are how field1 and field2 are determined, and some...

Why are UNION queries so slow in MySQL?

When I optimize my 2 single queries to run in less than 0.02 seconds and then UNION them the resulting query takes over 1 second to run. Also, a UNION ALL takes longer than a UNION DISTINCT. I would assume allowing duplicates would make the query run faster and not slower. Am I really just better off running the 2 queries separately? I w...

SQL query to join several columns

I'm trying to join some data together from 2 tables, but on several columns. here's an example: Source table ID | Desc| AAAA| BBBB| Table2 table ID | Text| ID1 | ID2 | ID3 | where ID1, ID2 and ID3 in Table2 are ID's from the Source table I'd like to do a query which yields the results: Table2.Text, Source.Desc(ID...

Results of UNION Selects in SQL Server

Is it possible to UNION queries from tables or views that don't have any result in common? What I'm trying to do is combine data from different views into one result. I have select a,b,null,c from VIEW1, VIEW2 where VIEW1.a = VIEW2.a UNION select null,null,z,null from VIEW3 I would like the result to be a,b,z,c. Is this where I wou...

Union element alignment

If I have a union, C standard guarantees that the union itself will be aligned to the size of the largest element. union U { long l; int i; short s; char c[2]; } u; But what does it say about alignment of individual union elements inside the union? Is the following expression guaranteed to be true? (&u.l == &u.i) && (...

SQL: grouping 2 tables as 1 with join, union, and then?

Here i am again :) I have 5 tables: customers id - name p_orders id - id_customer - code - date p_items id - id_order - description - price and h_orders and h_items, that are exactly the copy of p_orders and p_items. When the p_ tables reach a big amount of rows, i move the oldest to the h_ tables.. they due as history. So, my pro...

SQL Column merge and aggregate functions

I have a simple table with two columns (well two columns of interest, plus just an ID)... I have an ajax search which is just looking for keywords... which are then sent to my real search... the ajax search doesn't care what they are, but they need to be distinct... How can I merge the two columns together: City, Country Krakow, Polan...

Check Constraints in MySQL

Hi guru We have a bunch of tables in Microsoft SQL so that each table has its own check constraint. At the end we have created a partition view that does a "UNION ALL" against these tables. Our attempts to insert into this partition view is quite successful. Select, update and delete all work properly. We want a similar functionality in...

Union query throwing 'Invalid use of null' exception

I have two queries in Access. Both of them are moderately nasty to create, but at the end of the process they do have the same number of fields with the same data types. They both work independently, producing the expected results. Unfortunately, SELECT * FROM [qry vaBaseQuery-S2] UNION ALL SELECT * FROM [qry BaseQuery]; throws two ...

Union of All Intersecting Sets

Given a list of objects with multiple attributes I need to find the list of sets created by a union of all intersecting subsets. Specifically these are Person objects, each with many attributes. I need to create a list of 'master' sets based on a handful of unique identifiers such as SSN, DLN, etc. For instance, if Person A and Person...

Union and struct packing problem

Hi, I'm writing some software where each bit must be exact(it's for the CPU) so __packed is very important. typedef union{ uint32_t raw; struct{ unsigned int present:1; unsigned int rw:1; unsigned int user:1; unsigned int dirty:1; unsigned int free:7; unsigned int frame:20; } __packed; }__packed page_union_t; that is my structu...

Help me out with this MySql full outer join (or union)

This is coming from converting MSSQL to MySql. The following is code I'm trying to get to work: CREATE TEMPORARY TABLE PageIndex ( IndexId int AUTO_INCREMENT NOT NULL PRIMARY KEY, ItemId VARCHAR(64) ); INSERT INTO PageIndex (ItemId) SELECT Paths.PathId FROM Paths, ((SELECT Paths.PathId FROM AllUsers, Paths ...

SQL Union All to display old data with new data?

Ok, In MS Access, I have some reports that use some queries, to show data, within a date range. The queries use aliases, if, sum, and avg functions, and join multiple tables to get its data. I'd like to know if i could use a UNION ALL, with a table that has all the needed fields, to display this new data from this table, along with th...

SQL Union in MS Excel 2007

I have a number of CSV files of data that I want to Union together into a single table in MS Excel. All the CSV files have the same names and number of columns. In a relational database like Access or SQL I could use a Union Query, but this has to be in Excel. How can I quickly merge all of these files into one in Excel? ...

Complex rails query - unions? sub-select? can I still used named_scope?

Part of why I love Rails is that I hate SQL - I think it's more like an assembly language that should be manipulated with higher level tools such as ActiveRecord. I seem to have hit the limits of this approach, however, and I'm out of my depth with the SQL. I have a complex model with lots of sub-records. I also have a set 30-40 named...

Union of intervals

I've got a class representing an interval. This class has two properties "start" and "end" of a comparable type. Now I'm searching for an efficient algorithm to take the union of a set of such intervals. Thanks in advance. ...

SQL: Change Date and group by Fiscal Month End

I have created the following table (via UNION ALL): ID Date Hour Weight Fiscal AAA 1/27/2009 0 10 0 AAA 1/30/2009 0 20 0 AAA 2/14/2009 0 10 0 AAA 2/18/2009 0 20 0 AAA 2/27/2009 0 20 0 AAA 2/28/2009 0 20 0 AAA ...

Union with Count OR Join with Sum - MySQL

Hi, I want to combine three tables - date, lead and click - in a query. The tables looks like this: date: |date| lead: id|time|commission click: id|time|commission The table date is just storing dates and is used when getting dates with no click or lead. So if we have the following data in the tables: date: 2009-06-01 200...

SQL MS SQL Server V.8 UNION problem

Hi, I am using this query: SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID WHERE (pa.AdvertiserID = 31331) AND (pat.LastAdDate > 7 / 1 / 2009) ORDER BY pat.PublicationID And it returns 0 results. What I am trying to d...

UNION SQL statement not working

Hi, Why is this wrong and how can I put it right? SELECT PublicationID FROM (SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID WHERE (pa...