Statement to get complete table ? In SQL Server? - sql-server-2008

In SQL Server, I am trying to get the rows from the two tables where first table is master table and second is child table. like I have a table which contain file extensions with id, and description of extension. second table is having employees who having extension ids from Master table. now i want to get the total master table and if i place where condition for particular employee his extensionid and description.
When I write left outer join it is getting only that particular employee extension details,
my sql statement like this:
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId
where FEEM.EmpID = '004135'
Result is giving only one row, but I need the complete master table and particular employee details can you help me.

how about
select *
from Tbl_File_Extn_M FEM
left outer join Tbl_File_Extn_EmpLink_M FEEM on FEM.ExtId = FEEM.ExtId AND FEEM.EmpID = '004135'

Related

Find names from team members in mysql [duplicate]

I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
the other contains actual data, 2 of which columns require a TO and FROM domain names. So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.
Simple.
Now I need to actually display both domain names on the webpage. I know how to display one or the other, via the LEFT JOIN domains ON reviews.rev_dom_for = domains.dom_url query, and then you echo out the dom_url, which would echo out the domain name in the rev_dom_for column.
But how would I make it echo out the 2nd domain name, in the dom_rev_from column?
you'd use another join, something along these lines:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
EDIT:
All you're doing is joining in the table multiple times. Look at the query in the post: it selects the values from the Reviews tables (aliased as rvw), that table provides you 2 references to the Domain table (a FOR and a FROM).
At this point it's a simple matter to left join the Domain table to the Reviews table. Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.
Then in the SELECT list, you will select the DOM_URL fields from both LEFT JOINS of the DOMAIN table, referencing them by the table alias for each joined in reference to the Domains table, and alias them as the ToURL and FromUrl.
For more info about aliasing in SQL, read here.
Given the following tables..
Domain Table
dom_id | dom_url
Review Table
rev_id | rev_dom_from | rev_dom_for
Try this sql... (It's pretty much the same thing that Stephen Wrighton wrote above)
The trick is that you are basically selecting from the domain table twice in the same query and joining the results.
Select d1.dom_url, d2.dom_id from
review r, domain d1, domain d2
where d1.dom_id = r.rev_dom_from
and d2.dom_id = r.rev_dom_for
If you are still stuck, please be more specific with exactly it is that you don't understand.
Read this and try, this will help you:
Table1
column11,column12,column13,column14
Table2
column21,column22,column23,column24
SELECT table1.column11,table1.column12,table2asnew1.column21,table2asnew2.column21
FROM table1 INNER JOIN table2 AS table2asnew1 ON table1.column11=table2asnew1.column21 INNER TABLE table2 as table2asnew2 ON table1.column12=table2asnew2.column22
table2asnew1 is an instance of table 2 which is matched by table1.column11=table2asnew1.column21
and
table2asnew2 is another instance of table 2 which is matched by table1.column12=table2asnew2.column22

Invalid Column Name with INNER JOIN and Multiple Tables

Im currently writing a stored procedure in SQL to print results from multiple tables to find the top ten products purchased, but I am getting the syntax error
"Invalid column name 'ProductID'".
This appears on the 2nd INNER JOIN statement at sod.ProductID
My code below
CREATE PROCEDURE usp_top10ProfitableProducts
AS
BEGIN
SELECT TOP 10 sp.StoreProductID, sup.ProductName, sum(sod.Quantity) AS quantitysold, (sum(sod.Quantity) * sum(sod.unitPrice)) - (sum(sod.Quantity) * sum(sp.costPrice)) AS Profit
FROM SalesOrderDetails sod
INNER JOIN StoreProduct sp ON sp.StoreProductID = sod.StoreProductID
INNER JOIN SupplierProduct sup ON sup.ProductID = sod.ProductID
WHERE Quantity > 0
END
Thanks in advance.
EDIT** Below is also my Entity Relationship diagram. 'ProductID' lives in 'SupplierProduct'
Probably the field does not exist in one of the tables. Another point: group by was missing.
GROUP BY sp.StoreProductID, sup.ProductName
To do a join between two tables the joining column (in above case ProductID) should be available in both tables, otherwise sql will not be able to do the join.
According to the table structure SalesOrderDetails doesn't have a column ProductID which is why it's giving the error, you need to the join on cloumns that exist in the respective tables

How to deal with bad data in mysql?

I have three tables that I want to combine.
I have the following query to run:
DROP TABLE
IF EXISTS testgiver.smart_curmonth_downs;
CREATE TABLE testgiver.smart_curmonth_downs
SELECT
ldap_karen.uid,
ldap_karen.supemail,
ldap_karen.regionname,
smart_curmonth_downs_raw.username,
smart_curmonth_downs_raw.email,
smart_curmonth_downs_raw.publisher,
smart_curmonth_downs_raw.itemtitle,
smart_items.`Owner`
FROM
smart_curmonth_downs_raw
INNER JOIN ldap_karen ON smart_curmonth_downs_raw.username = ldap_karen.uid
INNER JOIN smart_items ON smart_curmonth_downs_raw.itemtitle = smart_items.Title
I want to know how to create the joins while maintaining a one to one relationship at all times with rows in table smart_curmonth_downs_raw.
For instance if there is not a uid in ldap_karen I have issues. And then the last issue I have found is that our CMS is allowing for duplicate itemtitle. So if I run my query I am getting a lot more rows because it is creating a row for each itemtitle. For example would there be a way to only catch the last itemtitle that is in smart_items. I would just really like to maintain the same number of rows - and I have no control over the integrity issues of the other tables.
The smart_curmonth_downs_raw table is the raw download information (download stats), the karen table adds unique user information, and the smart_items table adds unique items (download) info. They are all important. If a user made a download but is knocked off the karen table I would like to see NULLs for the user info and if there is more than one item in smart_items that has the same name then I would like to see just the item with the highest ID.
It sounds like relationship between smart_curmonth_downs_raw and ldap_karen is optional, which means you want to use a LEFT JOIN which all the rows in the first table, and, if the right table does not exists, use NULL as the right table's column values.
In terms of the last item in the smart_items table, you could use this query.
SELECT title, MAX(id) AS max_id
FROM smart_items
GROUP BY title;
Combining that query with the other logic, try this query as a solution.
SELECT COALESCE(ldap_karen.uid, 'Unknown') AS uid,
COALESCE(ldap_karen.supemail, 'Unknown') AS supemail,
COALESCE(ldap_karen.regionname, 'Unknown') AS regionname,
smart_curmonth_downs_raw.username,
smart_curmonth_downs_raw.email,
smart_curmonth_downs_raw.publisher,
smart_curmonth_downs_raw.itemtitle,
smart_items.`Owner`
FROM smart_curmonth_downs_raw
INNER JOIN (SELECT title, MAX(id) AS max_id
FROM smart_items
GROUP BY title) AS most_recent
ON smart_curmonth_downs_raw.itemtitle = most_recent.Title;
INNER JOIN smart_items
ON most_recent.max_id = smart_items.id
LEFT JOIN ldap_karen
ON smart_curmonth_downs_raw.username = ldap_karen.uid;

MySQL query: All data from one table and second table only if it exists

I have two tables in my database. The one has is a list (table b in the code example) of profiles. The second is a log table (table a in the code example) with records of when the profile was updated.
I want to execute a query that gives me every single profile on the site. Some profiles have not been updated so there is no record in the log table, I want them to be shown as well with a 0 timestamp.
The following code will return only the rows to which ids exist in both tables.
SELECT b.id, b.name, b.linkname, IFNULL(MAX(a.date),0) as lastupdate
FROM links a, list b
WHERE b.id=a.id
How can I get it to work, so I get a full list of profiles and the latest timestamp to which one exists and a 0 timestamp if it doesnt exist?
Thanks in advance.
Hope this isnt trivial, I've been searching a ton, but couldnt find an example.
Use LEFT JOIN to get data.. use GROUP BY to avoid repeated data.. because u have several entries in log table for a single profile..
SELECT b.id, b.name, b.linkname, IFNULL(MAX(a.date),0) as lastupdate
FROM list b
LEFT JOIN links a ON (a.id=b.id)
GROUP BY b.id;

How do you join on the same table, twice, in mysql?

I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
the other contains actual data, 2 of which columns require a TO and FROM domain names. So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.
Simple.
Now I need to actually display both domain names on the webpage. I know how to display one or the other, via the LEFT JOIN domains ON reviews.rev_dom_for = domains.dom_url query, and then you echo out the dom_url, which would echo out the domain name in the rev_dom_for column.
But how would I make it echo out the 2nd domain name, in the dom_rev_from column?
you'd use another join, something along these lines:
SELECT toD.dom_url AS ToURL,
fromD.dom_url AS FromUrl,
rvw.*
FROM reviews AS rvw
LEFT JOIN domain AS toD
ON toD.Dom_ID = rvw.rev_dom_for
LEFT JOIN domain AS fromD
ON fromD.Dom_ID = rvw.rev_dom_from
EDIT:
All you're doing is joining in the table multiple times. Look at the query in the post: it selects the values from the Reviews tables (aliased as rvw), that table provides you 2 references to the Domain table (a FOR and a FROM).
At this point it's a simple matter to left join the Domain table to the Reviews table. Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.
Then in the SELECT list, you will select the DOM_URL fields from both LEFT JOINS of the DOMAIN table, referencing them by the table alias for each joined in reference to the Domains table, and alias them as the ToURL and FromUrl.
For more info about aliasing in SQL, read here.
Given the following tables..
Domain Table
dom_id | dom_url
Review Table
rev_id | rev_dom_from | rev_dom_for
Try this sql... (It's pretty much the same thing that Stephen Wrighton wrote above)
The trick is that you are basically selecting from the domain table twice in the same query and joining the results.
Select d1.dom_url, d2.dom_id from
review r, domain d1, domain d2
where d1.dom_id = r.rev_dom_from
and d2.dom_id = r.rev_dom_for
If you are still stuck, please be more specific with exactly it is that you don't understand.
Read this and try, this will help you:
Table1
column11,column12,column13,column14
Table2
column21,column22,column23,column24
SELECT table1.column11,table1.column12,table2asnew1.column21,table2asnew2.column21
FROM table1 INNER JOIN table2 AS table2asnew1 ON table1.column11=table2asnew1.column21 INNER TABLE table2 as table2asnew2 ON table1.column12=table2asnew2.column22
table2asnew1 is an instance of table 2 which is matched by table1.column11=table2asnew1.column21
and
table2asnew2 is another instance of table 2 which is matched by table1.column12=table2asnew2.column22