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
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 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]
);
There are my tables:
http://i.imgur.com/dzwokhh.png
I want to write a query that return all info order by categoryId and Name.
For example: I want to return from right table id = 2,15,18 (CategoryId=1)
because in the left table they belong to Java (Id=1)
This should help to solve the problem:
select *
from mytable1
join mytable2 on mytable1.ID=mytable2.CategoryID
order by mytable1.ID ,Name
I've got three tables, two are "data" tables, one is a join (or lookup) table.
Place Table
PlaceId
Name
etc...
Categories Table
CatId
Name
etc...
PlaceCats Table
PlaceId
CatId
(with appropriate relationships defined between each Id field)
What I want to do is pull the categories that contain less than 5 Places... for some reason I just can wrap my mind around the T-SQL to make that happen.
SELECT *
FROM Categories
WHERE CatId IN
(
SELECT CatId
FROM PlaceCats
GROUP BY CatId
HAVING COUNT(*) < 5
)
To get the raw data:
select CatID, count(*)
from PlaceCats
group by CatID
having count(*) < 5
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 */