Qlikview joins that doesn't join on all matching column names - sql

I'm new to Qlikview and looking for some answers regarding scripting. How can I create Qlikview joins that just join on a specific column (and not all that are having a matching name)? Let's say that I'm having the following tables:
Employee
Id | Person | DepartmentID | Flags
1000 , Bob , 2001 , 1000000
1001 , Sue , 2002 , 1100000
Department
Id | Name | Flags
2001 , HR , 01101111
2001 , R&D , 1100000
What is the best way of joining those tables on the DepartmentID <-> ID field? The data is provided by SQL selects. I'm thinking of writing SQL views using unique names would be one idea, but there must be a simpler way. Please advise.

Karl
First, you really will be better off using the QlikCommunity for these questions, the forum is very well supported by the QlikView user community and you'll get answers quicker, the only reason I found this is that I have a google alert on QlikView.
To your question:
QlikView will automatically create a join on all matching fields and there is no way to stop it doing this, there is also no way to make it join using fields of different names. The answer therefore is to rename your fields in either the SQL Select statement or in the LOAD statement, for example:
Employee:
LOAD ID AS EmpID, Person, DepID, Flags AS Emp_Flags;
SQL SELECT ID, Person, DepartmentID as DepID, Flags FROM .........;
Department:
LOAD ID AS DepID, Name AS DepartmentName, Flags AS Dep_Flags;
SQL SELECT ID, Name, Flags FROM .........;
This should do the trick for you.
One more piece of advice, although QlikView will join on multiple fields it is best to avoid this, so if you do have a join that requires multiple fields then you should create a key specifically for the QlikView table by adding fields together in both table to create a single field.
As I said above, join the QlikCommunity (www.qlikcommunity.com) and you'll find a much better service for your questions.

There is a way to prevent QlikView from joining fields with similar names automatically.
Say, if you two tables Tab1 and Tab 2 with similar field names, however different in their own context, you could use the "noconcatenate" keyword to prevent QlikView from automatically making an association.

you can edit the load script in QlikView (CTRL+E) and use something like this:
Work:
LOAD
EmployeeId,
EmployeePerson,
DepatmentId,
EmployeeFlags,
DepartmentName,
DepartmentFlags
SELECT Employee.Id as EmployeeId, Employee.Person as EmployeePerson, Employee.DepartmentID as DepatmentId, Employee.Flags as EmployeeFlags, Department.Name as DepartmentName, Department.Flags as DepartmentFlags
FROM Employee, Department
WHERE Employee.DepartmentID = Department.Id
(Did not try it, but you should get the idea)

This should work
employee:
LOAD * INLINE [
ID, Person, DepartmentID, Flags
1000, Bob, 2001, 1000000
1001, Sue, 2002, 1100000
];
department:
LOAD * INLINE [
ID, Name, Flags
2001, HR, 01101111
2002, R&D, 1100000
];
left join (employee)
LOAD ID AS DepartmentID,
Name,
Flags AS Department_Flags
Resident department
;
DROP Table department;
The result shoud look like this:
Kind Regards
Daniel

Related

SQL Query Involving Data From Different Tables

I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!
This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';

Which Oracle query is faster

I am trying to display employee properties using C# WPF view.
I have data in '2' different oracle tables in my database:
Those tables structure at high-level is...
Employee table (EMP) - columns:
ID, Name, Organisation
Employee properties table (EMPPR) - columns
ID, PropertyName, PropertyValue
The user will input 'List of Employee Name' and I need to display Employee properties using data in those '2' tables.
Each employee has properties from 40-80 i.e. 40-80 rows per employee in EMPPR table. In this case, which approach is more efficient?
Approach #1 - single query data retrieval:
SELECT Pr.PropertyName, Pr.PropertyValue
FROM EMP Emp, EMPPR Pr
WHERE Emp.ID = Pr.ID
AND Emp.Name IN (<List of Names entered>)
Approach #2 - get IDs list using one query and Get properties using that ID in the second query
Query #1:
SELECT ID
FROM EMP
WHERE Name IN (<List of Names entered>)
Query #2:
SELECT PropertyName, PropertyValue
FROM EMPPR
WHERE ID IN (<List of IDs got from Query#1>)
I need to retrieve ~10K employee details at once where each employee has 40-80 properties.
Which approach is good?
Which query is faster?
The first one, which uses a single query to fetch your results.
Why? much of the elapsed time handling queries, especially ones with modestly sized rows like yours, is consumed going back and forth from the client to the database server.
Plus, the construct WHERE something IN (val, val, val, val ... ... val) can throw an error when you have too many values. So the first query is more robust.
Pro tip: Come on into the 21st century and use the new JOIN syntax.
SELECT Pr.PropertyName, Pr.PropertyValue
FROM EMP Emp
JOIN EMPPR Pr ON Emp.ID = Pr.ID
WHERE Emp.Name IN (<List of Names Inputted>)
Use first approach of join between two tables which is far better than using where clause two times.

Access Sql insert subquery not working

I'm designing a web form to publish to a SharePoint webpage in Microsoft Access 2010. I have a form that uses a combo box to select a team name. I need to enter the team id corresponding to that team name into the employee table. This is what I have so far:
INSERT INTO Employee ( Employee_Name, Team_ID )
VALUES ([Forms]![Add Employee]![txtName], (SELECT MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam]));
It gives me an error saying
Query input must contain at least one table or query.
How do I fix this?
You can either use the VALUES clause to specify hard coded values, or you can use a SELECT statement to generate the stuff that will be inserted. Here you are combining the two. Instead:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team, Employee
WHERE [Team]![Team_Name]=[Forms]![Add Employee]![cmbxTeam];
Slightly unrelated, why are you cross joining employees and teams, when you don't need the Employee table in your SELECT?
This should be equivalent and much much faster:
INSERT INTO Employee ( Employee_Name, Team_ID )
SELECT
[Forms]![Add Employee]![txtName],
MAX(Team.Team_ID)
FROM Team
WHERE [Team]![Team_Name] = [Forms]![Add Employee]![cmbxTeam];

Ms-Access: counting from 2 tables

I have two tables in a Database
and
I need to retrieve the number of staff per manager in the following format
I've been trying to adapt an answer to another question
SELECT bankNo AS "Bank Number",
COUNT (*) AS "Total Branches"
FROM BankBranch
GROUP BY bankNo
As
SELECT COUNT (*) AS StaffCount ,
Employee.Name AS Name
FROM Employee, Stafflink
GROUP BY Name
As I look at the Group BY I'm thinking I should be grouping by The ManID in the Stafflink Table.
My output with this query looks like this
So it is counting correctly but as you can see it's far off the output I need to get.
Any advice would be appreciated.
You need to join the Employee and Stafflink tables. It appears that your FROM clause should look like this:
FROM Employee INNER JOIN StaffLink ON Employee.ID = StaffLink.ManID
You have to join the Eployee table twice to get the summary of employees under manager
select count(*) as StaffCount,Manager.Name
from Employee join Stafflink on employee.Id = StaffLink.EmpId
join Employee as Manager on StaffLink.ManId = Manager.Id
Group by Manager.Name
The answers that advise you on how to join are correct, assuming that you want to learn how to use SQL in MS Access. But there is a way to accomplish the same thing using the ACCESS GUI for designing queries, and this involves a shorter learning curve than learning SQL.
The key to using the GUI when more than one table is involved is to realize that you have to define the relationships between tables in the relationship manager. Once you do that, designing the query you are after is a piece of cake, just point and click.
The tricky thing in your case is that there are two relationships between the two tables. One relationship links EmpId to ID and the other links ManId to ID.
If, however, you want to learn SQL, then this shortcut will be a digression.
If you don't specify a join between the tables, a so called Cartesian product will be built, i.e., each record from one table will be paired with every record from the other table. If you have 7 records in one table and 10 in the other you will get 70 pairs (i.e. rows) before grouping. This explains why you are getting a count of 7 per manager name.
Besides joining the tables, I would suggest you to group on the manager id instead of the manager name. The manager id is known to be unique per manager, but not the name. This then requires you to either group on the name in addition, because the name is in the select list or to apply an aggregate function on the name. Each additional grouping slows down the query; therefore I prefer the aggregate function.
SELECT
COUNT(*) AS StaffCount,
FIRST(Manager.Name) AS ManagerName
FROM
Stafflink
INNER JOIN Employee AS Manager
ON StaffLink.ManId = Manager.Id
GROUP BY
StaffLink.ManId
I don't know if it makes a performance difference, but I prefer to group on StaffLink.ManId than on Employee.Id, since StaffLink is the main table here and Employee is just used as lookup table in this query.

How to do this in SQL (Access SQL)

I have a table that has the following fields:
1- Name
2- Dept
3- DateWorked
5- Session
the table filled with data about people from different departments that worked on different date and different sessions (AM/PM). I want to count the number of sessions that all people in a specific dept worked. Please note that:
1- Several people work on a department.
2- Each person may work several sessions. (on different date)
3- I want to know thw number of session that any persion of a department worked. for example, it doesn't matter if on say 28/12/2012 AM, two person from sale department worked, It should be counted as one.
I maybe able to write access VBA code to do this, but I prefer a sql query to do this.
Can you please give me a clue how I can do this?
I am using Access 2003.
select t1.Dept, count(*) from
(
select distinct Dept, DateWorked, Session from t
) AS t1
group by t1.Dept;
Please give a create table and sone date for example. But i would tray:
SELECT Dep, DateWorked, COUNT(*) as ammount FROM your_table GROUP BY DateWorked, Dep