Select a column from two tables and put them into one - sql

I don't just mean put them together as in I have one row in table 1 that says "Stuff" and another row in table 2 that says "Things" and that puts them together into a single cell that says "StuffThings", I just mean to simply have all the data in table 1 and table 2 combined into one column. For example....
Table 1:
Item 1
Item 2
Item 3
Table 2:
Item 4
Item 5
Item 6
New Table:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
All my attempts to do this have been something like
Select (Table1.Row + Table2.Row) AS JobNumber
FROM Table1, Table2;
It just does something like
Item 1Item 2
Item 1Item 3
.....
etc.

I think you want a UNION query:
SELECT Table1.Row AS JobNumber
FROM Table1
UNION
SELECT Table2.Row AS JobNumber
FROM Table2
Or UNION ALL, if you want duplicates.

Related

Access SQL - How to get a second column from table field built on row source value list type?

I've created a simple table - table1.
thre is only one talbe (table1)
the table1 has two fields: [table1].[id] and [table1].[method]
the RowSourceType of [table1].[method] is - 'Value list'
the Row Source of [table1].[method] is is ' 1;"A";35;"B";2;"C";3;"D" ' (so two columns).
RowSourceType - Value list
Row Source - 1;"A";35;"B";2;"C";3;"D"
I've populated table1 with rows:
id
method
1
35
2
2
3
1
I'm loking for a query to receive result:
[table1].[id]
[table1].[ method]
1
B
2
C
3
A
(I'd like to avoid to add lookup table)
Thank You in advance.
rgds
You can use Switch:
Select
id,
Switch([method]=1,"A",[method]=35,"B",[method]=2,"C",[method]=3,"D") As MethodCode
From
table1
As #Gustav suggested and perhaps you didn't understand, first step is to create a lookup table, second step is to use that as your RowSource, third step is to build your query
Benefits of this approach is that you don't need to change your Rowsource every time you make a change to the Lookup List
1. Create Lookup Table to match your rowsource
LookupID
LookupValue
1
A
35
B
2
C
3
D
2-a. Change Rowsource of your input field
In Properties | Data
Set Rowsource to
SELECT LookupID, LookupValue FROM LookupTable
Set RowsourceType to
Table/Query
2-b Still in Properties | Format, set up columns and Hide the ID field
ColumnCount =2
ColumnWidths = 0;3cm
3. Build your final query
SELECT Table1.ID, LookupTable.LookupValue
FROM Table1 INNER JOIN LookupTable ON Table1.metod = LookupTable.LookupID;
Results of Query
ID
LookupValue
1
B
2
C
3
A

Select values from one column and display in two columns

I'm stumped trying to figure out a way to display data from one column in two different columns. The data is a bill of materials in Oracle, and I want to display certain items in the bill of materials in a second column.
For example, I have a master item which is 123ABC. Within this item it has the following items ... AA1, BB1, CC1. They all belong to the same column in the same table.
I'm trying to get a query to to display two columns, the first column would be master item, and the second column would be sub item that shows all items beginning with 'AA' like below...
Master Item | Sub Item
-------------------------
123ABC | AA1
So far I have the following...
select distinct msi.item FNUMBER
, msi.description DESCRIPTION
, (select msi1.item
FROM inv.mtl_sys_item msi1
WHERE msi1.item = msi.item)
from
, .mtl_sys_item msi
where msi.segment1 = '123ABC'
order by 1 desc
But this just displays the master item 'ABC123' in both columns instead of the AA1 in the second column.
Your data must have some sort of parent item column. You can use this to do a self-join:
SELECT msi.item as FNUMBER,
msi.description as DESCRIPTION,
msic.item
FROM inv.mtl_sys_item msi JOIN
inv.mtl_sys_item msic
ON msi.item = msic.parent_item -- guessing at the column name
WHERE msi.segment1 = '123ABC'
ORDER BY 1 desc

Oracle: how to group by 1st value in row?

Table has 3 columns in a table: ITEM, SUB_ITEM, DATE_CREATED.
ITEM - item id (string)
SUB_ITEM - item id (int)
DATE_CREATE - date the SUB_ITEM was created (date)
Scenario:
There are 3 Different item's (AAA1, AAB2, ABB3)
All 3 of these item's have multiple sub-items.
Each item has a sub-item that is the same for each of them (eg. All 3 of the item's have a SUB_ITEM = 101010)
I am trying to do something like:
select *
from table
group by ITEM, SUB_ITEM, DATE_CREATED
How do you make it display only 1 row? I don't care if it chooses AAA1 or AAB2 or ABB3, I just want it to pick 1 and remove the rest so it will show 1 row per SUB_ITEM, but still displays at least one of the parent items.
Edit:
Thank you to mathguy for answering the above question.
Question 2:
Is it possible to group by the 1st 2 letters of the item in addition to the sub_item? So instead of returning 1 row, return 2 rows: AAA1 and AAB2 will cascade in to 1 row, and ABB3 will be the 2nd row because 'AA' and 'AB' are different.
Edit 2: See Main Answer comments for answer to question 2
One way is to group by sub_item, and take the max or min over another column (let's say max over date_created) and whatever is in the remaining column IN THE SAME ROW.
select min(item) keep (dense_rank last order by date_created) as item,
sub_item, max(date_created) as date_created
from table_name
group by sub_item
;

Searching for a number in a database column where column contains series of numbers seperated by a delimeter '"&" in SQLite

My table structure is as follows :
id category
1 1&2&3
2 18&2&1
3 11
4 1&11
5 3&1
6 1
My Question: I need a sql query which generates the result set as follows when the user searched category is 1
id category
1 1&2&3
2 18&2&1
4 1&11
5 3&1
6 1
but i am getting all the results not the expected one
I have tried regexp and like operators but no success.
select * from mytable where category like '%1%'
select * from mytable where category regexp '([.]*)(1)(.*)'
I really dont know about regexp I just found it.
so please help me out.
For matching a list item separated by &, use:
SELECT * FROM mytable WHERE '&'||category||'&' LIKE '%&1&%';
this will match entire item (ie, only 1, not 11, ...), whether it is at list beginning, middle or end.

Create View Issue

I have data in two tables that i want to get into a view to then be able to do reporting on and having trouble as only some of the records in the first table have records in the second table.
The data is ranges for a product.
For example I have 2 options the first has 6 ranges of pricing, 3 are stored in table A and 3 in table B linked by the ID of the record in table A. The second has only 3 ranges that are in table A so no record is created in table B for this product.
When I try to do a create view and include the details from the second table I only get the details for the first option and not the second as it is excluding this because there is no record in the second table.
How do I over come this?
TableP1 TableP2
Product_id Unit Range 1 Range 2 Range 3 TableP1_ID Range 4 Range 5 Range 6 TableP1_ID
1 Person 20 18 16 1 14 12 10 1
2 Person 25 22 2
not sure if the above makes sense.
Can you not have two queries and UNION the results together in your VIEW code?
Something like:
CREATE myView
AS
SELECT X,
Y
FROM Table1
UNION
SELECT X,
Y
FROM Table2;