I have a scenario where i need to merge 2 tables. but there are no common columns between the two tables.
Table 1
---------------
|grpnr|grpname|
---------------
| 1 |abc |
| 2 |def |
| 3 |ghi |
---------------
Table 2
----------------
| no | text |
----------------
| 0 | qwerty |
| 10 | asdfg |
| 25 | zxcvb |
----------------
Expected result
-------------------------------
| grpnr | Grpname| no| text |
-------------------------------
| 1 | abc | 0 | qwerty |
| 1 | abc |10 | asdfg |
| 1 | abc |25 | zxcvb |
| 2 | def | 0 | qwerty |
| 2 | def |10 | asdfg |
| 2 | def |25 | zxcvb |
| 3 | ghi | 0 | qwerty |
| 3 | ghi |10 | asdfg |
| 3 | ghi |25 | zxcvb |
-------------------------------
I tried using sql joins but im not able to achieve the desired result. can someone help me out with this?
have you tried cross join?
SELECT Table1.col1,
Table1.col2,
Table2.col1,
Table2.col2
FROM tablename Table1
CROSS JOIN tablename Table2;
additional info for this http://www.w3resource.com/sql/joins/cross-join.php
Try this :
SELECT
*
FROM Table_2 t1
CROSS JOIN Table_4 t2
ORDER BY t1.grpnr;
Related
I have 2 tables, tableStock and tableParts:
tableStock
+----+----------+-------------+
| ID | Num_Part | Description |
+----+----------+-------------+
| 1 | sr37 | plate |
+----+----------+-------------+
| 2 | sr56 | punch |
+----+----------+-------------+
| 3 | sl30 | crimper |
+----+----------+-------------+
| 4 | mp11 | holder |
+----+----------+-------------+
tableParts
+----+----------+-------+
| ID | Location | Stock |
+----+----------+-------+
| 1 | A | 2 |
+----+----------+-------+
| 3 | B | 5 |
+----+----------+-------+
| 5 | C | 2 |
+----+----------+-------+
| 7 | A | 1 |
+----+----------+-------+
And I just want to do this:
+----+----------+-------------+----------+-------+
| ID | Num_Part | Description | Location | Stock |
+----+----------+-------------+----------+-------+
| 1 | sr37 | plate | A | 2 |
+----+----------+-------------+----------+-------+
| 2 | sr56 | punch | NULL | NULL |
+----+----------+-------------+----------+-------+
| 3 | sl30 | crimper | B | 5 |
+----+----------+-------------+----------+-------+
| 4 | mp11 | holder | NULL | NULL |
+----+----------+-------------+----------+-------+
List ALL the rows of the first table and if the second table has the info, in this case 'location' and 'stock', add to the column, if not, just null.
I have been using inner and left join but some rows of the first table disappear because the lack of data in the second one:
select tableStock.ID, tableStock.Num_Part, tableStock.Description, tableParts.Location, tableParts.Stock from tableStock inner join tableParts on tableStock.ID = tableParts.ID;
What can I do?
You can use left join. Here is the demo.
select
s.ID,
Num_Part,
Description,
Location,
Stock
from Stock s
left join Parts p
on s.ID = p.ID
order by
s.ID
output:
| id | num_part | description | location | stock |
| --- | -------- | ----------- | -------- | ----- |
| 1 | sr37 | plate | A | 2 |
| 2 | sr56 | punch | NULL | NULL |
| 3 | sl30 | crimper | B | 5 |
| 4 | mp11 | holder | NULL | NULL |
Very simple question and I just can't seem to figure it out.
I have the two tables below. I would like to write a select query that returns the following
| UID | NAME | DESCRIPTION | CC_CONFIG_UID |
├------+---------+-------------+---------------┤
| xxx | HELLO_1 | NULL | abc |
| yyy | WORLD_1 | NULL | hij |
| aaa | NULL | HELLO_2 | efg |
| bbb | NULL | WORLD_2 | klm |
Table A:
| UID | NAME | CC_CONFIG_UID |
|------+---------+---------------|
| xxx | HELLO_1 | abc |
| yyy | WORLD_1 | hij |
Table B:
| UID | DESCRIPTION | CC_CONFIG_UID |
|------+-------------+---------------|
| aaa | HELLO_2 | efg |
| bbb | WORLD_2 | klm |
I have tried
(SELECT * FROM A) UNION (SELECT * FROM B)
But I get the following in return, which is close but not what I need:
| UID | NAME | CC_CONFIG_UID |
├------+---------+---------------┤
| xxx |HELLO_1 | abc |
| aaa |HELLO_2 | def |
| yyy |WORLD_1 | hig |
| bbb |WORLD_2 | klm |
You need to specify the columns. Your tables have three columns, but you want the result set to have four:
select a.uid, a.name, null as description, a.cc_config_uid
from a
union all
select b.uid, null as name, bdescription, b.cc_config_uid
from b;
How Can I count and show how many Opportunity have Stage 3 but dont have Stage 2?
+-------+-------+
| OppID | Stage |
+-------+-------+
| ABC | 1 |
| ABC | 2 |
| ABC | 3 |
| ABC | 4 |
| CDF | 3 |
| CDF | 4 |
| EFG | 1 |
| EFG | 2 |
| EFG | 3 |
| HIJ | 2 |
| HIJ | 3 |
| LMI | 1 |
| LMI | 2 |
| LMI | 4 |
+-------+-------+
The count result is 1
+-------+-------+
| OppID | Stage |
+-------+-------+
| CDF | 3 |
| CDF | 4 |
+-------+-------+
Got it, you could use NOT EXISTS and COUNT DISTINCT in following:
SELECT COUNT(DISTINCT OppID)
FROM tbl AS t1
WHERE NOT EXISTS (SELECT 1 FROM tbl AS t2 WHERE t1.OppID = t2.OppID and t2.Stage = 2) and t1.Stage = 3
Here I'm having table 'TABLE_1' as
|ID | Name | ACID1 | ACVALUE1 | ACID2 | ACVALUE2 | ACID3 | ACVALUE3 |
|----------------------------------------------------------------------
| 1 |ABC | 10 | 82.50 | 20 | 175.95 | 40 | 125.75 |
| 2 |IJK | 30 | 120.55 | 20 | 68.30 | 50 | 25.45 |
| 3 |LMN | 40 | 62.50 | 10 | 87.25 | 30 | 40.50 |
----------------------------------------------------------------------
Another table is AC_TABLE whose ID is recored in above table as ACID1,ACID2,...
___________________
|ID | Name |
|-------------------
|10 | AC1 |
|20 | AC2 |
|30 | AC3 |
|40 | AC4 |
|50 | AC5 |
-------------------
Now what all i want the result in following format
_____________________________
ID | Name | ACName | ACVALUE
------------------------------
1 | ABC | AC1 | 82.50
1 | ABC | AC2 | 175.95
1 | ABC | AC4 | 125.75
2 | IJK | AC3 | 120.55
2 | IJK | AC2 | 68.30
2 | IJK | AC5 | 25.45
3 | LMN | AC4 | 62.50
3 | LMN | AC1 | 87.25
3 | LMN | AC3 | 40.50
-------------------------------
Please help me to get the desired result.
Use Cross Apply to unpivot multiple columns.
First unpivot the Table_1 using cross apply then join the result with the ac_table table. Try this.
SELECT a.id,
a.name,
b.name AS ACName,
cs.ACValue
FROM Table_1 a
CROSS apply (VALUES (ACVALUE1,ACID1),
(ACVALUE2,ACID2),
(ACVALUE3,ACID3))cs(acvalue, ac)
JOIN ac_table b
ON cs.ac = b.id
SQLFIDDLE DEMO
I have one table that looks like this:
+---------------+---------------+-----------+-------+------+
| id_instrument | id_data_label | Date | Value | Note |
+---------------+---------------+-----------+-------+------+
| 1 | 57 | 1.10.2010 | 200 | NULL |
| 1 | 57 | 2.10.2010 | 190 | NULL |
| 1 | 57 | 3.10.2010 | 202 | NULL |
| | | | | |
+---------------+---------------+-----------+-------+------+
And the other that looks like this:
+----------------+---------------+---------------+--------------+-------+-----------+------+
| id_fundamental | id_instrument | id_data_label | quarter_code | value | AnnDate | Note |
+----------------+---------------+---------------+--------------+-------+-----------+------+
| 1 | 1 | 20 | 20101 | 3 | 28.2.2010 | NULL |
| 2 | 1 | 20 | 20102 | 4 | 1.8.2010 | NULL |
| 3 | 1 | 20 | 20103 | 5 | 2.11.2010 | NULL |
| | | | | | | |
+----------------+---------------+---------------+--------------+-------+-----------+------+
What I would like to do is to merge/join these two tables in one in a way that I get something like this:
+------------+--------------+--------------+----------+--------------+
| Date | Table1.Value | Table2.Value | AnnDate | quarter_code |
+------------+--------------+--------------+----------+--------------+
| 1.10.2010. | 200 | 3 | 1.8.2010 | 20102 |
| 2.10.2010. | 190 | 3 | 1.8.2010 | 20102 |
| 3.10.2010. | 202 | 3 | 1.8.2010 | 20102 |
| | | | | |
+------------+--------------+--------------+----------+--------------+
So the idea is to order them by Date from Table1 and since Table2 Values only change on the change of AnnDate we populate the Resulting table with same values from Table2.
After that I would like to go through the resulting table and create another (Final table) with the following.
On Date 1.10.2010. take last 4 AnnDates (so it would be 1.8.2010. and f.e. 20.3.2010. 30.1.2010. 15.11.2009) and Table2 values on those AnnDate. Make SUM of those 4 values and then divide the Table1 Value with that SUM.
So we would get something like:
+-----------+---------------------------------------------------------------+
| Date | FinalValue |
+-----------+---------------------------------------------------------------+
| 1.10.2010 | 200/(Table2.Value on 1.8.2010+Table2.Value on 20.3.2010 +...) |
| | |
+-----------+---------------------------------------------------------------+
Is there any way this can be done?
EDIT:
Hmm yes now I see that I really didn't do a good job explaining it.
What I wanted to say is
I try INNER JOIN like this:
SELECT TableOne.Date, TableOne.Value, TableTwo.Value, TableTwo.AnnDate, TableTwo.quarter_code
FROM TableOne
INNER JOIN TableTwo ON TableOne.id_intrument=TableTwo.id_instrument WHERE TableOne.id_data_label = somevalue AND TableTwo.id_data_label = somevalue AND date > xxx AND date < yyy
And this inner join returns 2620*40 rows which means for every AnnDate from table2 it returns all Date from table1.
What I want is to return 2620 values with Dates from Table1
Values from table1 on that date and Values from table2 that respond to that period of dates
f.e.
Table1:
+-------+-------+
| Date | Value |
+-------+-------+
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
+-------+-------+
Table2
+-------+---------+
| Value | AnnDate |
+-------+---------+
| x | 1 |
| y | 4 |
+-------+---------+
Resulting table:
+-------+---------+---------+
| Date | ValueT1 | ValueT2 |
+-------+---------+---------+
| 1 | a | x |
| 2 | b | x |
| 3 | c | x |
| 4 | d | y |
+-------+---------+---------+
You need a JOIN statement for your first query. Try:
SELECT TableOne.Date, TableOne.Value, TableTwo.Value, TableTwo.AnnDate, TableTwo.quarter_code FROM TableOne
INNER JOIN TableTwo
ON TableOne.id_intrument=TableTwo.id_instrument;