Oracle sql view rows that id exist and doesn't exist - sql

I have two tables: "Orders", "Names"
Some orders doesn't exist in "Names", but I need to get them too.
Example:
Orders:
ID
1
2
3
Names
ID
Name
1
Name1
2
Name2
If ID from Orders doesn't exist in Names, column Name will be null
Result:
ID
Name
1
Name1
2
Name2
3
If I try "WHERE ORDERS.ID = NAMES.ID", ID number 3 doesn't match, so it will not show up.

Related

DB2-Find the count of number of occurrences of different values for each primary key in a table

I have a table like
ID
value
1
ABC
1
ABC
1
ABCD
2
ABC
Column 2 can either have ABC or ABCD
Now I want to find the count for values in column 2 for each ID
ID
count (ABC)
count (ABCD)
1
2
1
2
1
0
How to write DB2 query to get the output in the above format for count?
Found a solution
Select ID, sum(decode(value, 'ABC',1,0)) as count_abc
, sum(decode(value, 'ABCD',1,0)) as count_abcd
from table
group by ID

Selection of Data From Multiple Tables having Relations

I need to select data from Multiple Table having Relation like
A->B->C (A have one-to-many relationship with B and B have many-to-one relationship with C).
As in given Picture of Database, I need to select sum of total_amount from petty_claim and vendors_claim on a specific Expense type and specific office(Invoice_main) group by Month(Invoice_main.date) click here to view picture
Given Below are exemplary tables having no real values but similar to original tables. All Values are Random.
Need Help for this. Thanks for your time
Table: Invoice_main
Columns: invoice_ID Office_ID Date
row: 1 2 2018-05-5
row: 2 1 2018-04-4
row: 3 1 2018-05-3
Table: Office
Columns: office_ID Office
row: 1 Support
row: 2 HR
row: 3 Billing
Table: Petty_claim
Columns: invoice_id Expense_id Amount
row: 1 2 1000
row: 2 3 1000
Table: Vendors_claim
Columns: invoice_id Expense_id Amount
row: 3 1 2000
row: 4 3 2000
Table: Expense_Types
Columns: expense_id dept_id
row: 1 Type A
row: 2 Type B
row: 3 Type C
OUT PUT Required
Month Expense_Type Office_1 Office_2
1 Type A 1000 2000
2 Type B 2033 1034
It will be something like this:
SELECT SUM(invoice.total_amount)
FROM (SELECT date, total_amount
FROM invoice_main main , invoice_petty_claim petty, invoice_expense_type expense_type
WHERE main.office = 'XXX' and main.id=petty.invoice_id and petty.expense_type = expense_type.type
UNION ALL
SELECT date, total_amount
FROM invoice_main main, invoice_vendors_claim vendor, invoice_expense_type expense_type
WHERE main.office = 'XXX' and main.id=vendor.invoice_id AND vendor.expense_type = expense_type.type
) invoice
GROUP BY invoice.date

Get duplicate on single column after distinct across multiple columns in SQL

I have a table that looks like this:
name | id
-----------
A 1
A 1
B 2
C 1
D 3
D 3
F 2
I want to return id's 1 and 2 because they are duplicate on names. I don't want to return 3, because it is distinct for D 3.
Basically, I'm thinking of doing a query to first get a distinct pairing, so the above reduces to
name | id
-----------
A 1
B 2
C 1
D 3
F 2
And then doing a duplicate find on the id column. However, I'm struggling to find the correct syntax to construct that query.
You should be able to get the result you want by using a GROUP BY along with a HAVING clause that counts the distinct names. The HAVING clause will filter for those ids that have more than one distinct name:
select id
from Table1
group by id
having count(distinct name) > 1
Here is a demo

group multiple rows into single row multiple columns

My data tables look like this:
User table:
UserID Parameter Value
1 1 1
2 1 1
1 2 2
Parameter Table:
ID Name
1 "Age"
2 "Gender"
Value Table:
ParameterID ValueID Name
1 1 "17"
2 1 "Male"
Is it possible to group the user table into single rows with multiple columns to look something like this?
UserID Age Gender
1 1 1

Where (or even any kind of join) doesn't work with Temp table in SQLite

I have to collect data from many sources into a temp table before performing another SELECT on this table and another table. The temp table has a column called RoomID while the table (which I want to join with my temp table) also has a column called RoomID (I join these 2 tables to get more info about specified Room via its RoomID), but the returned rows are not all rows (all which I think should meet the WHERE condition).
Here is my temp table (after creating and perform a SELECT on it like this: SELECT * FROM MyTempTable):
ID1 | Name1 | ID2 | Name2 | RoomID
NULL NULL 2 A 2
2 A NULL NULL 2
1 B NULL NULL 3
3 C NULL NULL 4
4 D 5 E 8
All the values in RoomID are populated and all these RoomIDs are also present in the table I want to join with MyTempTable, here is that table (call it Room):
RoomID | Name | Note
1 Dining-room NULL
2 Bathroom NULL
3 Pantry NULL
4 Living room NULL
5 Sitting room NULL
6 Music room NULL
7 Office room NULL
8 Library NULL
With those tables above, the following query should return all the matched rows as you (and I expect):
SELECT ID1, Name1, ID2, Name2, MyTempTable.RoomID as [RoomID], Name as [Room Name], Note
FROM MyTempTable, Room
WHERE MyTempTable.RoomID=Room.RoomID
But it doesn't. I want to say more about MyTempTable, in fact it is created from 3 UNIONs, and all the values in its ZoomIDs are collected from other tables (which take part in the UNION clauses). I don't know if it matters. However the SELECT performs on the last MyTempTable shows that MyTempTable does have a column of RoomID populated with some rows. Here is what it should be after performing the SELECT above:
ID1 | Name1 | ID2 | Name2 | RoomID | Room Name | Note
NULL NULL 2 A 2 Bathroom NULL
2 A NULL NULL 2 Bathroom NULL
1 B NULL NULL 3 Pantry NULL
3 C NULL NULL 4 Living room NULL
4 D 5 E 8 Library NULL
But there isn't such a full result, the result contains only 2 rows and I even don't know why they are always those 2 rows not of any other 2 rows in the set of rows above:
ID1 | Name1 | ID2 | Name2 | RoomID | Room Name | Note
2 A NULL NULL 2 Bathroom NULL
1 B NULL NULL 3 Pantry NULL
I guess, the MyTempTable.RoomID refers to the original table in the UNION which I used to create MyTempTable, because I can see that all the returned rows seem to only belong to that table.
This is really strange, and if it's not a bug, I think the things we can do with Temp table in SQLite are so limited.
Update
You can see that, there is no row with ZoomID equal to 4 returned, but if I change the SELECT to this, it's returned - why is that?
SELECT ID1, Name1, ID2, Name2, MyTempTable.RoomID as [RoomID], Name as [Room Name], Note
FROM MyTempTable, Room
WHERE MyTempTable.RoomID=4 and Room.RoomID=4
I'm sorry to say that, all the fault belongs to me. I made a mistake in creating my tables where in this the ZoomID has type of varchar and in that it has type of int. The difference in types makes the Where Condition unable to be met. And it seems that the auto-conversion is applied only when there is at least 1 constant (like 4 above) in the comparison expression.
Sorry!