selecting multiple times from two tables to get two different result - sql

I am using apex application building to create a report.
Table
staff
id name
201 john
302 sarah
787 bob
product
product_editor_id product_name
201 car
307 shoe
728 airbag
as you can see in the staff table I have staff details and in the product I have the staff id to show the staff that created it.
in my report I have selected everything from the staff table, but you see where I have staff id i don't want to show the id but instead their name
I have rewritten a query for this. The reason why I am asking this question is that in the previous query I had something like this
select * from staff Inner join
company_car ON
staff.staff_car_id=company_car.vehicle_id_staffid
if that query doesn't make sense. Basically I did this to only display staff that has a company car and the vehicle_id_staffid is the same as the staff id.
anyways on top for that query I need to add my new query to it which is
select distinct name from staff
left join product on
staff.id=product.product_editor_id
I tried
select * from staff Inner join
company_car ON
staff.staff_car_id=company_car.vehicle_id_staffid
UNION
select distinct name from staff
left join product on
staff.id=product.product_editor_id
but apex said ORA-01789: query block has incorrect number of result columns
I want the result from it to be different. The first query should have any problem with the second query because they are but doing their own thing.

Probably the easiest way would be to create original report with staff_id in it.So
create new report as new page or as region on the page.
-something like :
Select * from company_car
So you get report that will have staff_id as one of the columns.Then you edit page properties and go to report.
Select report attributes and from the list of columns you have in the report you chose staff_id column edit ( pencil icon on the left).
Go to display as and select 'Display as Text based on LOV dont save state'
List of value will appear. Go to list of values section
and type :
select id r, name d from staff;
or
select id r, name||' '||surname d from staff;
in here you need to link staff_id with name and their surname which will be displaying on the report instead of meaningless id. Alternatively you can create static LOV and use it to populate this column or you can try to build JOIN statement.

Related

How to add multiple employees to a Product

I am very NEW to SQL and know few things here and there.
I am puzzled with this for a couple of days and can't find an answer for it. What I am trying to do is to assign multiple people to a project in way that their name would appear based on their Job Assignments.
For example:
I have table called Products and in that table I have:
Model_ID -> This is set to Primary Key
Model_Name,
PM,
Designer
I have another table called Employee and in that table I have:
Employee_ID -> This is set to Primary Key
First_Name,
Last_Name,
email,
Job_Title
Under Job_Title, for each Employee, I would have their position in the company like PM, Designer, AE, Server PM, etc.
What I am trying to do is to list a corresponding Employee name under a Products table PM column if person is assigned as a PM and under Designer column if person is assigned as a Designer for that product. I have included screenshot of my diagram...
I understand I don't have them linked because everything I tried so far, I couldn't make it work. I am just trying to illustrate what I'm trying to do.
Any help/ pointers would be greatly appreciated and I am open to create new tables if needed (like separate job functions out of Employee table into a separate table, etc.)
The end result should look like:
If I'm understanding your question correctly, the following should do the trick:
Select p.Product_ID, p.Model_Name, e1.First_Name as PM, e2.First_Name as Designer
from Product p
left join Employee e1 on e1.Employee_ID = p.PM
left join Employee e2 on e2.Employee_ID = p.Designer
If you want to use full names, then:
Select p.Product_ID, p.Model_Name, e1.First_Name + ' ' + e1.Last_Name as PM, e2.First_Name + ' ' + e2.Last_Name as Dessigner
from ...
I used Left Joins in case not every product has both a PM and Designer assigned yet. You should probably have two foreign key relationships on your project table though, one for PM and one for Designer, and both will use the Employee table's Employee_ID as the key.
Design View
If you want to do this in Design View, I think you will need to add a second copy of the Employee table and 'link' the PM field to Employee_ID in one Employee table and the Designer field to Employee_ID in the other.
I hope this helps.

How to get information from table

I have an sql table that has 2 columns, one is id_User from a table named applicationUsers and the other one id_Schedule from the table schedule, when i insert information on them i have to do it by the id, but also i have tha table showed on a datagridview, it shows the 2 colums correct, but how can i get for example i have in the main table 1 1, that the name of the user 1 is David and the name of the schedule is special.
How can i show the name from the user 1(David) and the name from the schedule 1(special) in the datagridview?
You would need to execute a query that joins all three tables, e.g.
SELECT u.UserName, s.ScheduleName
FROM User u INNER JOIN UserSchedule us
ON u.UserID = us.UserID INNER JOIN Schedule s
ON s.ScheduleID = us.ScheduleID
Populate a DataTable from that query and bind it to a DataGridView and you'll see the user name and schedule name in the two columns.

New report from two tables with reportviewer doesnt have true result

I create a new view in SQL Server 2008 from two tables that have relation on a field. I want to create a report and do grouping on that common field.
For example:
table1: student(ID,first-name,last-name,phone,address,...)
table2: courses(ID,fk_ID,Course,....)
Now I want to have report that shows all data from both tables with grouping on ID from student table, that must show courses information separated for every student.
my query is:
SELECT TOP (100) PERCENT
dbo.tbl_student.ID,
dbo.tbl_student.firstname, dbo.tbl_student.lastname,
dbo.tbl_courses.Coursename,
dbo.tbl_Courses.CourseDate, dbo.tbl_courses.coursetype,
FROM
dbo.tbl_student LEFT OUTER JOIN
dbo.tbl_courses ON dbo.tbl_student.ID = dbo.tbl_courses.fk_id
ORDER BY
dbo.tbl_student.firstname DESC
But when I create a new report from this view, it shows just one record for every group. I spent 2 hours to solve the problem but I did not succeed.
please help me to create report from two or more tables.
Now it shows one record duplicates for several times for every group
Have you tried a query like this:
SELECT s.[ID], s.[first-name], s.[last-name], s.[phone], ...
c.[ID], c.[Course], ...
FROM student s
LEFT OUTER JOIN
courses c ON s.[ID] = c.[fk_ID]

Returning duplicated values only once from a join query

I'm trying to extract info from a table in my database based on a persons job. In one table i have all the clients info, in another table linked by ID_no their job title and the branches theyre associated with. the problem I'm having is when i join both tables I'm returning some duplicates because a person can be associated with more than one branch.
I would like to know how to return the duplicated values only once, because all I care about for the moment is the persons id number and what their job title is.
SELECT *
FROM dbo.employeeinfo AS ll
LEFT OUTER JOIN employeeJob AS lly
ON ll.id_no = lly.id_no
WHERE lly.job_category = 'cle'
I know Select Distinct will not work in this situation since the duplicated values return different branches.
Any help would be appreciated. Thanks
I'm using sql server 2008 by the way
*edit to show result i would like
------ ll. ll. lly. lly.
rec_ID --employeeID---Name-----JobTitle---Branch------
1 JX100 John cle london
2 JX100 John cle manchester
3 JX690 Matt 89899 london
4 JX760 Steve 12345 london
I would like the second record to not display because i'm not interested in the branch. i just need to know the employee id and his job title, but because of how the tables are structured it's returning JX100 twice because he's recorded as working in 2 different branches
You must use SELECT DISTINCT and specify you ONLY want person id number and job title.
I don't know exactly your fields name, but I think something like this could work.
SELECT DISTINCT ll.id_no AS person_id_number,
lly.job AS person_job
FROM dbo.employeeinfo AS ll LEFT OUTER JOIN
employeeJob AS lly ON ll.id_no = lly.id_no
WHERE lly.job_category = 'cle'

How to use the result from a second select in my first select

I am trying to use a second SELECT to get some ID, then use that ID in a second SELECT and I have no idea how.
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
This post got long, but here is a short "tip"
While the syntax of my select is bad, the logic is not. I need that "x" somehow. Thus the second select is the most important. Then I have to use that "x" within the first select. I just don't know how
/Tip
This is the only thing I could imagine, I'm very new at Sql, I think I need a book before practicing, but now that I've started I'd like to finish my small program.
EDIT:
Ok I looked up joins, still don't get it
SELECT Employee.Name
FROM Emplyee, Employment
WHERE x = Employment.DistributionID
LEFT JOIN Distribution ON
(SELECT Distribution.DistributionID FROM Distribution
WHERE Distribution.Location = 'California') AS x
Get error msg at AS and Left
I use name to find ID from upper red, I use the ID I find FROM upper red in lower table. Then I match the ID I find with Green. I use Green ID to find corresponding Name
I have California as output data from C#. I want to use California to find the DistributionID. I use the DistributionID to find the EmployeeID. I use EmployeeID to find Name
My logic:
Parameter: Distribution.Name (from C#)
Find DistributionID that has Distribution.Name
Look in Employment WHERE given DistributionID
reveals Employees that I am looking for (BY ID)
Use that ID to find Name
return Name
Tables:
NOTE: In this example picture the Employee repeats because of the select, they are in fact singular
In "Locatie" (middle table) is Location, I get location (again) from C#, I use California as an example. I need to find the ID first and foremost!
Sory they are not in english, but here are the create tables:
Try this:
SELECT angajati.Nume
FROM angajati
JOIN angajari ON angajati.AngajatID = angajari.AngajatID
JOIN distribuire ON angajari.distribuireid = distribuire.distribuireid
WHERE distribuire.locatie = 'california'
As you have a table mapping employees to their distribution locations, you just need to join that one in the middle to create the mapping. You can use variables if you like for the WHERE clause so that you can call this as a stored procedure or whatever you need from the output of your C# code.
Try this solution:
DECLARE #pLocatie VARCHAR(40)='Alba'; -- p=parameter
SELECT a.AngajatID, a.Nume
FROM Angajati a
JOIN Angajari j ON a.AngajatID=j.AngajatID
JOIN Distribuire d ON j.DistribuireID=d.DistribuireID
WHERE d.Locatie=#pLocatie
You should add an unique key on Angajari table (Employment) thus:
ALTER TABLE Angajari
ADD CONSTRAINT IUN_Angajari_AngajatID_DistribuireID UNIQUE (AngajatUD, DistribuireID);
This will prevent duplicated (AngajatID, DistribuireID).
I don't know how you are connecting Emplyee(sic?) and Employment, but you want to use a join to connect two tables and in the join specify how the tables are related. Joins usually look best when they have aliases so you don't have to repeat the entire table name. The following query will get you all the information from both Employment and Distribution tables where the distribution location is equal to california. You can join employee to employment to get name as well.
SELECT *
FROM Employment e
JOIN Distribution d on d.DistributionID = e.DistributionID
WHERE d.Location = 'California'
This will return the contents of both tables. To select particular records use the alias.[Col_Name] separated by a comma in the select statement, like d.DistributionID to return the DistributionID from the Distribution Table