Retrieve data from two different table in a single report - sql

I have two table Employee and Salary table, salary consists Salary of employee in a field named Salary_employee.
Second one is Extra Expense, Extra expense consists records related to extra expenses of a company like electricity bills,office maintenance in a field named extra_expense.
(Their is no relationship between these two table).
Finally, I just wanted to show all the expenses of company in a report, for this i need to group both the table. what to use here join or union ??.

If there is no relationship between the two tables, then this really cannot work since you dont know where the expense is supposed to tie into. You should redesign the database if possible as this sounds impossible based on your description.
UPDATE
OK, by the look of your screenshots, I am guessing that this database only stores one companies info? And not multiple?
IF that is correct, AND if all you want to do is squish the data together into one flowing report of expenses, then I would indeed suggest a UNION. A JOIN would not give you the flow you are looking for. A UNION will just smash the two outputs together into one...which I think is what you are asking for?
SELECT ext_amount AS amount, ext_date AS date_of_trans
FROM extra_expenses
UNION
SELECT sal_cash AS amount, sal_dateof_payment AS date_of_trans
FROM employee_salary

It sounds like you don't need to use group or join. Simply query both tables separately within a script and handle them both accordingly to their structure to produce a report.
Join and union are functions which you can use to extract different information on a common thing from separate tables. E.g. if you have a user whose private details are stored in one table, but their profile information is in another table. If you want to display both private details as well as profile info, you can join the two tables by the common user name in order to combine and gather all info on the user in one query.

Related

Update JOIN table contents

I have a table joined from two other tables. I would like this table to stay updated with entries in the other two tables.
First Table is "employees"
I am using the ID, Last_Name, and First_Name.
And the second Table is "EmployeeTimeCardActions"
using columns ID, ActionTime, ActionDate, ShiftStart, and ActionType.
ID is my common column that the join was created by..Joined Table...
Because I usually have a comment saying I did not include enough information, I do not need a exact specific code sample and I think I have included everything needed. If there is a good reason to include more I will, I just try to keep as little company information public as possible
Sounds like you're having your data duplicated across tables. Not a smart idea at all. You can update data in one table when a row is updated in a different one via triggers but this is a TERRIBLE approach. If you want to display data joined from 2 tables, the right approach here is using an SQL VIEW which will display the current data.

multiple Access queries using same criteria

New to this...
I have a table QAQC_Studies that includes titles, dates, and subject matter
I have another table QAQC_Publications that includes citation information for multiple publications resulting from a single study in the first table.
Every 3 months I need to create a report to QC studies added by coworkers so I run the following query (with some additional attributes removed for brevity). The where clause is a list of study IDs they provide me (often 15-20 different studies).
SELECT QAQC_Studies.StudiesID,
QAQC_Studies.NSL,
QAQC_Studies.StudyTitle,
QAQC_Studies.Abstract,
QAQC_Studies.StudyStatus
FROM QAQC_Studies
WHERE [QAQC_Studies].[StudiesID]=26806 or 26845
I'd like to add to that report a list of the publications associated with each study.
How do I write the Where clause in the second query to reference those studies indicated in the first query?
You can use a subquery. Something like:
SELECT [QAQC_Publications].[QAQC_Field]
FROM [QAQC_Publications]
WHERE [QAQC_Publications].[StudiesID] --or whichever field the two tables
--share for publication/study connection
IN (SELECT QAQC_Studies.StudiesID
FROM QAQC_Studies
WHERE [QAQC_Studies].[StudiesID]=26806 or 26845)

Business Objects Universes - joins to restrict data

I'm very new to universe design and would really appreciate a point in the right direction...
I have a table Sales that records details of sales made by staff members. It includes the following fields:
SaleID
StaffID
SaleDate
I also have a table StaffDepts that has the historic department information for each member of staff:
StaffID
DeptName
StartDate
EndDate
I would like to build a universe that pulls through the name of the department in which the member of staff was placed on the date of the sale.
I tried linking the two tables with three joins in Designer (I amended the Expression box in the Edit Join window of each join):
Sales.StaffID = StaffDepts.StaffID
StaffDepts.StartDate <= Sales.SaleDate
(StaffDepts.EndDate IS NULL) OR (StaffDepts.EndDate > Sales.SalesDate)
This failed pretty spectacularly! Can any suggest how I can achieve what I am trying to do?!
Many thanks!
You've created three separate joins in Designer, but I believe you really want them all to work as one. You can (and if I understand what you want to do here, should) create only one join in Designer and include all of the clauses above in it with AND.
I'm assuming the spectacular fail was because by creating three separate joins, you'd introduced loops into your universe. You would only want to create separate joins if you needed the two tables to be joined differently in different contexts -- and in that case, you would create those separate contexts and assign the joins to them accordingly.

SQL Using two tables for a query

I'm learning SQL for a while and I'm stuck with this problem.
I have two tables for a storage company.
In one table for the bookings for the whole company consisting booking no., date, product nr., quantity and storage room.
And in one table I have storage facility name and storage rooms to define the room belong to which facility
I want to have the sum of the quantity of one specific product in one specific storage facility
SELECT sum(Quantity) from Bookings where ProductNo=8888 and Storage.StorageFacility="Central"
Do I have to use a more complex query for the solution?
Your storage facility is in a different table than the BOOKINGS table (and they are related via STORAGE_ROOM). So you need to join these tables:
select sum(Quantity)
from bookings b
join storage_facility_details sfd
on b.storage_room = sfd.storage_room
where sfd.storage_facility = 'x'
and bookings.productno='8888'
The syntax of SQL can be slightly different on different database systems. Usually quotes (") are used to refer to tables and similar objects. Use single quotes (') to give values. Also, all the tables that you refer columns of need to appear after FROM. If the columns quantity and storagefacility appear in both tables you need to fully quality the name.
Also you need to join the two tables on a column, so the productno must also be present in your storage table.
SELECT sum(quantity) FROM bookings, storage
WHERE (bookings.productno = storage.productno)
AND
(bookings.productno='8888' AND storage.storagefacility='Central');

SQL join basic questions

When I have to select a number of fields from different tables:
do I always need to join tables?
which tables do I need to join?
which fields do I have to use for the join/s?
do the joins effects reflect on fields specified in select clause or on where conditions?
Thanks in advance.
Think about joins as a way of creating a new table (just for the purposes of running the query) with data from several different sources. Absent a specific example to work with, let's imagine we have a database of cars which includes these two tables:
CREATE TABLE car (plate_number CHAR(8),
state_code CHAR(2),
make VARCHAR(128),
model VARCHAR(128),);
CREATE TABLE state (state_code CHAR(2),
state_name VARCHAR(128));
If you wanted, say, to get a list of the license plates of all the Hondas in the database, that information is already contained in the car table. You can simply SELECT * FROM car WHERE make='Honda';
Similarly, if you wanted a list of all the states beginning with "A" you can SELECT * FROM state WHERE state_name LIKE 'A%';
In either case, since you already have a table with the information you want, there's no need for a join.
You may even want a list of cars with Colorado plates, but if you already know that "CO" is the state code for Colorado you can SELECT * FROM car WHERE state_code='CO'; Once again, the information you need is all in one place, so there is no need for a join.
But suppose you want a list of Hondas including the name of the state where they're registered. That information is not already contained within a table in your database. You will need to "create" one via a join:
car INNER JOIN state ON (car.state_code = state.state_code)
Note that I've said absolutely nothing about what we're SELECTing. That's a separate question entirely. We also haven't applied a WHERE clause limiting what rows are included in the results. That too is a separate question. The only thing we're addressing with the join is getting data from two tables together. We now, in effect, have a new table called car INNER JOIN state with each row from car joined to each row in state that has the same state_code.
Now, from this new "table" we can apply some conditions and select some specific fields:
SELECT plate_number, make, model, state_name
FROM car
INNER JOIN state ON (car.state_code = state.state_code)
WHERE make = 'Honda'
So, to answer your questions more directly, do you always need to join tables? Yes, if you intend to select data from both of them. You cannot select fields from car that are not in the car table. You must first join in the other tables you need.
Which tables do you need to join? Whichever tables contain the data you're interested in querying.
Which fields do you have to use? Whichever fields are relevant. In this case, the relationship between cars and states is through the state_code field in both table. I could just as easily have written
car INNER JOIN state ON (state.state_code = car.plate_number)
This would, for each car, show any states whose abbreviations happen to match the car's license plate number. This is, of course, nonsensical and likely to find no results, but as far as your database is concerned it's perfectly valid. Only you know that state_code is what's relevant.
And does the join affect SELECTed fields or WHERE conditions? Not really. You can still select whatever fields you want and you can still limit the results to whichever rows you want. There are two caveats.
First, if you have the same column name in both tables (e.g., state_code) you cannot select it without clarifying which table you want it from. In this case I might write SELECT car.state_code ...
Second, when you're using an INNER JOIN (or on many database engines just a JOIN), only rows where your join conditions are met will be returned. So in my nonsensical example of looking for a state code that matches a car's license plate, there probably won't be any states that match. No rows will be returned. So while you can still use the WHERE clause however you'd like, if you have an INNER JOIN your results may already be limited by that condition.
Very broad question, i would suggest doing some reading on it first but in summary:
1. joins can make life much easier and queries faster, in a nut shell try to
2. the ones with the data you are looking for
3. a field that is in both tables and generally is unique in at least one
4. yes, essentially you are createing one larger table with joins. if there are two fields with the same name, you will need to reference them by table name.columnname
do I always need to join tables?
No - you could perform multiple selects if you wished
which tables do I need to join?
Any that you want the data from (and need to be related to each other)
which fields do I have to use for the
join/s?
Any that are the same in any tables within the join (usually primary key)
do the joins effects reflect on fields specified in select clause or on where conditions?
No, however outerjoins can cause problems
(1) what else but tables would you want to join in mySQL?
(2) those from which you want to correlate and retrieve fields (=data)
(3) best use indexed fields (unique identifiers) to join as this is fast. e.g. join
retrieve user-email and all the users comments in a 2 table db
(with tables: tableA=user_settings, tableB=comments) and both having the column uid to indetify the user by
select * from user_settings as uset join comments as c on uset.uid = c.uid where uset.email = "test#stackoverflow.com";
(4) both...