SELECT a.delivery_date,
a.delivery_hour,
a.price as EX-ANTE,
FROM mms_realtime_dispatch_prices_report a
UNION ALL
SELECT b.delivery_date,
b.delivery_hour,
b.price as EX-POST,
FROM mms_realtime_dispatch_prices_report b
UNION ALL
SELECT c.region,
c.dem_rtdel,
c.date,
FROM pub_demand_lwap c;
UNION ALL
SELECT region,
report,
hour,
SUM(q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11)
FROM pub_markets_bids_and_offers
WHERE delivery date=03/16/2011
GROUP BY hour
help! need to combine this four table into one new table no duplicate data
Can you help me in combining this four tables into one table. this is the first time i encounter this. I really need a help :(
In SQL server union works only if there is same number and type of columns return by query.
You need to get your Union query right 1st I can see so many things wrong with Query At the moment,
Your number of columns retunred by each select arent same, Last query is returning 4 columns and other 3,
You are Aliasing Columns in 2nd query, it will not have any effect as Only the Column Names from very first select statement are visible in the result set.
Guessing from the Column names you have Different Data Types that
you are trying to UNION. Datatypes Returned from all selects that you are using in UNION should return the same datatypes e.g
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name1
UNION ALL
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name2
UNION ALL
SELECT Column1_DataType1, Column2_DataType2, Column3_DataType3 FROM Table_Name3
and so on....
Once you have met all these condition then you can do something like this to eliminate duplicate rows from you result set
;with CTE
AS
(
SELECT ID_Column, rn = ROW_NUMBER() OVER (PARTITION BY Column1, Column2, Column3... ORDER BY ID ASC)
FROM ( -- All of your UNION ALL Statements Can go here --)q
)
DELETE FROM CTE
WHERE rn = 1
Related
actually I have two tables ,i.e table 1 and table 2 . I need to display data from both tables . if I use
SELECT * FROM table 1 UNION SELECT * FROM table 2
Now I am getting data randomly from both tables
BUT I need first display table1 data after display table2 data
In practice, this will do what you want (assuming the tables have the same columns):
SELECT * FROM table1
UNION ALL
SELECT * FROM table2;
It is actually safer to use an explicit order by:
SELECT . . .
FROM (SELECT t1.*, 1 as which FROM table1 t1
UNION ALL
SELECT t2.*, 2 as which FROM table2 t2
) t
ORDER BY which;
SQL tables represent unordered sets. There is no ANSI requirement that UNION ALL return values from the first subquery before the second. In practice that works in MySQL.
One way could be adding a column in the both queries which would indicate the source from where it came.
SELECT
*
FROM
(
SELECT *,'T1' AS source FROM table1
UNION
SELECT *,'T2' FROM table2
) AS t
ORDER BY t.source ASC;
See Demo
I have 2 tables in my database which are identical in structure but contain different records. Each table has the field 'keywords' which contains comma delimited data.
At the moment, I'm running 2 MySQL queries to get 2 different results which I then merge, using the following MySQL Statements:
SELECT GROUP_CONCAT(keywords) keywords FROM table1
and
SELECT GROUP_CONCAT(keywords) keywords FROM table2
I'm sure I should be able to get what I need using a single MySQL statement but all of my attempts to use GROUP_CONCAT and UNION have failed.
Thanks for any pointers/help.
Try this :
SELECT CONCAT(GROUP_CONCAT(t1.keywords),',',GROUP_CONCAT(t2.keywords)) keywords FROM table1 t1, table2 t2
If you don't want duplicate values, then it will be like that :
SELECT GROUP_CONCAT(DISTINCT t.keywords) FROM (
SELECT t1.keywords FROM table1 t1
UNION
SELECT t2.keywords FROM table1 t2
) t
It is as simple as that:
SELECT GROUP_CONCAT(keywords) keywords FROM table1
UNION
SELECT GROUP_CONCAT(keywords) keywords FROM table2
Here is a fiddle.
I have used mysql DISTNCT keyword for one of my mySQL query for avoid duplication of data displaying on search box.
But when I add multiple column names on SELECT it doesn't work.
Please advice how to proceed.
$query = "
SELECT * FROM (
SELECT DISTINCT b.title,b.id, b.metakey
FROM categories b
WHERE b.title like '%".$searchc."%' AND b.parent_id BETWEEN 84 AND 107 AND b.level=3 ORDER BY LOCATE('".$searchc."', REPLACE(b.title, ' ', '')), b.title
) CLASS_CAT
UNION ALL
SELECT * FROM (
SELECT DISTINCT a.title, a.id, a.title as metakey
FROM content a join
categories b
on a.categories_id = b.id
WHERE REPLACE(a.title, ' ', '') like '%".$searchc."%'
AND b.parent_id BETWEEN 84 AND 107 AND b.level=3
) CLASS_ITEM
";
SELECT DISTINCT will remove duplicates from the one SELECT statement. UNION ALL directs the system to not look for duplicates between the two sets you are combining (each SELECT).
Use UNION (without ALL) instead. Note that removing the check for duplicates is faster, so if you know a set you're querying is unique, skip the dup check.
Also note that by row duplicates I'm referring to every column in every row. If any column makes a row unique it will appear in the result set. If you only want some columns to be unique you'll need to GROUP BY and aggregate the other columns (e.g. GROUP_CONCAT) or use additional queries to get other related data.
I have two tables. One tracks Part Shipments and the other tracks System shipments.
I am trying to count the customer contacts in each table with the result showing me the total customer contacts for both parts and systems combined.
I am trying to use Union and I would guess from my results I am doing this all wrong. My results end up with two entries for customers. Cust A will have a total of 9 and then another entry of 1. So I am guess there is no merge of the customer contacts and it is just creating a union of both results.
The Code I am using.
SELECT Count(part_shipment.Customer_Station_ID) AS Contact,
part_shipment.Customer_Station_ID AS Customer
FROM part_shipment
GROUP BY part_shipment.Customer_Station_ID
UNION
SELECT Count(system_shipments.Customer_Station_ID) AS Contact,
system_shipments.Customer_Station_ID AS Customer
FROM system_shipments
GROUP BY system_shipments.Customer_Station_ID
ORDER BY Contact DESC
You can't do it like that. The Union just take rows from first query and rows from second query, and "display" them ones after anothers.
UNION requires the creation of derived tables (tables created from a query).
SELECT *
FROM (
SELECT col1, col2
FROM table
) UNION (
SELECT col1, col2
FROM otherTable
)
I also don't think you can use GROUP BY inside the selects that make up the UNION (it's been a while since I used it so I don't remember for sure)
Do you have tried to use a GROUP BY and SUM from the results of UNION query?
I have a table with around 15 columns. What I would like to be able to do, is select a range of IDs and have all column data that is the same, presented to me.
At the minute, I have it structured as the following:
SELECT id, col_a, col_b ... count(id)
FROM table
GROUP BY col_a, col_b ...
Which returns rows grouped together that have identical data within all the rows - which is half what I want, but ideally I would like to be able to get a single row with either the value (if it's the same for every row id) or NULL if there is a single difference.
I'm not sure that it is possible, but I would rather see if it's doable in an SQL query than write some looping logic for PHP to go through and check each row's similarity.
Thanks,
Dan
UPDATE:
Just to keep this up-to-date, I worked through the problem by writing a PHP function that would find which were duplicates and then display the differences. However I have now since made a table for each column, and made the columns as references to the other tables.
E.G. In MainTable, ColA now refers to the table ColA
I'm still solving the problem with the PHP for the time being, mainly as I think it still leaves the problem mentioned above, but at least now Im not storing duplicate information.
Its a hairy thing to do, but you could do it similarly to how David Martensson suggested, I would write it like this, however:
Select a.id, a.col1, a.col2, a.col3
FROM myTable a, myTable b
WHERE a.id != b.id
and a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3
That would give you the ids that are unique, but each result would have the same values for columns 1, 2, and 3. However, I agree with some of the commenters to your question that you should consider an alternative data structure, as this could better take advantage of an RDBMS model. In that case you would want to have 2 tables:
Table name: MyTableIds
Fields: id, attrId
Table name: MyTableAttrs
Fields: attrId, attr1, attr2, attr3, ect
In general, if you have data that is going to be duplicated for multiple records, you should pull it into a second table and create a relationship so that you only have to store the duplicated data 1 time and then reference it multiple times.
Make a join to a subquery with the group by:
SELECT a.id, b.col_a, b.col_b ... b.count)
FROM table a
LEFT JOIN (
SELECT id, col_a, col_b ... count(id) "count"
FROM table GROUP BY col_a, col_b ...
)b on a.id = b.id
That way the outer will select all rows.
If you still want to group answers you could use a UNION instead
SELECT id, col_a ...
WHERE id NOT IN ("SUBQUERY WITH GROUP BY")
UNION
"SUBQUERY WITH GROUP BY"
Not the nicest solution but it should work
It seems doable from how I have understood your question.
And here's a possible pattern:
SELECT
/* GROUP BY columns */
col_A,
col_B,
...
/* aggregated columns */
CASE MIN(col_M) WHEN MAX(col_M) THEN MIN(col_M) ELSE NULL END,
CASE MIN(col_N) WHEN MAX(col_N) THEN MIN(col_N) ELSE NULL END,
...
COUNT(...),
SUM(...),
WHATEVER(...),
...
FROM ...
GROUP BY col_A, col_B, ...