TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The table Order contains a primary key of OrderID and a foreign key of CustomerID.
Something like
Select yourcustomerfields, yourorderfields
From Customer
Left join Orders on Customer.OrderID = Orders.OrderID
I came up with this solutions.
Select CustomerName
from Customer
Where pk_CustomerID IN
(
Select fk_CustomerID from Orders
INNER JOIN Customer
on Customer.pk_CustomerID=Orders.fk_CustomerID)
/* NOT IN instead of IN will give the other customers who doesn't have an Order */
Related
so I am stuck on this problem.
I want to generate random(but unique) data in my table.
I have table of products with id. And table warehouse - where I want to insert/update product_id. But it has to be unique (so there will be just one row for one product_id)
I tried different approaches, but none of them worked. Can you please help me somehow?
UPDATE warehouse
SET product_id = (SELECT id from product where product_id = product_id order by id limit 1);
with data as (
select s.i,
s.id as product_id
from (generate_series(1, 1) as seq(i)
cross join lateral (select product.id, seq.i from product order by random() ) as s)
)
insert into warehouse(product_id)
select product_id from data;
I need to do a sub-query from a table to find all employees working in the same department that is part of the same city, but I'm not getting it.
I have the following tables:
Table departments
DEPARTMENTS
department_id
department_name
location_id
Table locations
LOCATIONS
location_id
street_address
postal_code
city
state_province
country_id
Table employees
EMPLOYEES
employee_id
first_name
last_name
email
phone_number
hire_date
job_id
department_id
My code right now is something like that :
SELECT
firt_name,
department_id,
job_id
FROM employees
WHERE state_province = (SELECT state_province FROM locations
WHERE state_province = 'Sao Paulo');
The problem is that while I want to select state_province from the table locations, I can't select the name, department id and job id from the table employees. How can I select both tables while doing the sub-query ?
Anyway, sorry if I did something wrong in the code, I am new to sub-queries.
You could try doing a join between the two tables instead:
SELECT
e.firt_name,
e.department_id,
e.job_id,
l.* -- replace with columns you really want
FROM employees e
INNER JOIN locations l
ON e.state_province = l.state_province
WHERE
e.state_province = 'Sao Paulo';
I don't know which columns you want to select from locations, but it doesn't really make sense to do a join just for state_province alone, as the employees table already has this column. So I just included location.* as a placeholder which you can replace with the columns you actually want.
Edit:
A join is the way to go here IMO, but if you absolutely need to use a subquery, then you can move your current subquery from the WHERE clause to the SELECT clause:
SELECT
firt_name,
department_id,
job_id,
(SELECT l.state_province FROM locations l
WHERE e.state_province = l.state_province) state_province
FROM employees e;
Note that this will only work if there is one matching province. For this and performance reasons, my join query is probably what you would want to use in practice.
I think in your case, sub-query may not be necessary.
A join table can do the trick.
SELECT e.first_name, e.department_id, e.job_id, l.state_province
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
LEFT JOIN locations l ON d.location_id = l.location_id
WHERE l.state_province = 'Sao Paulo';
I have two tables. Table1 has company data (Company ID, Company Name ...), so single record for each company.
Table2 has information about departments in that company (Department ID, Department Name, Company ID, Company Name ... ). So, second table might have n number of records where same company id is used.
Problem is one of our trigger failed to work properly, and no one noticed till now. So, when Company Name was updated in Table1, it never reflected in Table2.
To correct this, I have to do something like the below query:
Update Table2
Set
[Company Name] = (select [Company Name]
from Table1
where Table2.Company ID = Table1.Company ID)
Group By Table2.Company ID
Basically, I am trying to update all records in Table2 to use the same name as Table1, for each record in Table1.
I am a bit confused about how to create the inner select clause.
P.S. Sorry, it might be a bit confusing. Kindly do let me know how to reword it the best.
Don't need to use group by ...
UPDATE T2
SET [Company Nane] = T1.[Company Nane]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.[Company ID] = T2.[Company ID]
As other have mentioned: Remove GROUP BY and your query works fine.
GROUP BY is used to produce a result row in a query that is an aggregate of other rows. E.g. one record per company from your department table with the most busy department per company. You cannot update such a result record, for that record does not exist in the table. You can only update table records.
So remove GROUP BY from your query and you have it straight-forward.
Update DepartmentTable
Set [Company Name] =
(
select [Company Name]
from CompanyTable
where CompanyTable.[Company ID] = DepartmentTable.[Company ID]
);
I have the following query that finds customers related to an order. I have a legacy ID on the customer so I have to check old id (legacy) and customer id hence the or statement
SELECT
c.Title,
c.Name
FROM productOrder po
INNER JOIN Employee e ON po.BookedBy = e.ID
CROSS APPLY (
SELECT TOP 1 *
FROM Customer c
WHERE(po.CustID = c.OldID OR po.CustID = c.CustID)
) c
GROUP BY
c.CustomerId, c.Title, c.FirstName, c.LastName
if I remove the OR statement it runs fine for both situations. There is an index on customer id and legacy.
For table customer, you need to create separate indexes on columns oldid and custid. If you already have clustered index on custid, then add index on oldid as well:
CREATE INDEX customer_oldid_idx ON customer(oldid);
Without this index, search for oldid in this clause:
WHERE (po.CustID = c.OldID OR po.CustID = c.CustID)
will have to use full table scan, and that will be super slow.
I am having 3 tables Book Master, Category Master, Subcategory Master. In my Book Master table, column CategoryName have value from Category Master's ID column. And SubcategoryName column in Book Master have ID columns value from Subcategory Master.
Table structure of the 3 tables:
Book Master
Oid uniqueidentifier,
BookName nvarchar(100),
CategoryName uniqueidentifier,
SubCategoryName uniqueidentifier
Category Master
Oid uniqueidentifier,
CategoryName nvarchar(100)
Subcategory Master
Oid uniqueidentifier,
CategoryName uniqueidentifier,
SubCategoryName nvarchar(100)
I am working on one windows application,in that I have to show top 5 books from each category of book.Now I am having 10 records in table and I have used the following query to display the books on form.
select BM.BookName ,BM.BookImage,CM.CategoryName,SCM.SubCategoryName
from BookMaster BM,CategoryMaster CM,SubCategoryMaster SCM
where BM.CategoryName=CM.Oid and BM.SubCategoryName=SCM.Oid
order by CM.CategoryName
this query gives me all 10 records. But, this query is fine when records in table are less. But when records goes on increasing, It will be difficult to show all records. For this reason, I want to show Top 5 books from each category.
I have tried it with group by clause
select top 5 BM.BookName ,BM.BookImage,CM.CategoryName,SCM.SubCategoryName
from BookMaster BM,CategoryMaster CM,SubCategoryMaster SCM
where BM.CategoryName=CM.Oid and BM.SubCategoryName=SCM.Oid
group by CM.CategoryName
but it gives me error saying,
Column 'BookMaster.BookName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Suggest me any solution for this.
Thanks in advance
Write a function that takes CategoryName and returns top 5 books for that category as a table
Then use T SQL Apply to print your desired result
More more details http://msdn.microsoft.com/en-us/library/ms175156%28v=sql.105%29.aspx
Try this:
WITH CategoryCTE AS (
SELECT
BookName,
ROW_NUMBER() OVER (
PARTITION BY CategoryName
ORDER BY CategoryName DESC
) AS CTE_Order
FROM BookMaster
)
SELECT bm.BookName, cm.CategoryName, scm.SubCategoryName
FROM
CategoryCTE
INNER JOIN BookMaster bm ON CategoryCTE.BookName = bm.BookName
INNER JOIN CategoryMaster cm ON bm.CategoryName = cm.Oid
INNER JOIN SubCategoryMaster scm ON bm.SubCategoryName = scm.Oid
WHERE CategoryCTE.CTE_Order < 6
GROUP BY cm.CategoryName, scm.SubCategoryName, bm.BookName
Please look over this link for solution
http://sqlzoo.net/howto/source/z.dir/err979/sqlserver
While using group by clause in query you have to specify the selected list of columns in select query in group clause, Otherwise its showing error what you get