Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
2.78/5 (3 votes)
See more:
Hi all

I have products table like this:

productID,producteName,category

now, I need to get last 4 products inserted into products table for every category
help me please

regards
Posted
Comments
joshrduncan2012 25-Mar-13 15:46pm    
Are you familiar with the T-SQL language?

Try this:
SQL
DECLARE @ct INT

SELECT @ct = COUNT(DISTINCT category)
FROM @products

SET @ct = @ct * 4

SELECT T.*
FROM (
	SELECT productID, producteName, category, ROW_NUMBER() OVER (PARTITION BY category ORDER BY productID DESC) AS Number
	FROM products
	) AS T
WHERE T.Number < 5


[EDIT 2]
Now, it must work!

I tested it on below example:
SQL
DECLARE @products TABLE (productID INT IDENTITY(1,1), producteName VARCHAR(30), category VARCHAR(30))

INSERT INTO @products (producteName, category)
VALUES('Audi','cars')
INSERT INTO @products (producteName, category)
VALUES('Volkswagen','cars')
INSERT INTO @products (producteName, category)
VALUES('Citroen','cars')
INSERT INTO @products (producteName, category)
VALUES('Peugeot','cars')
INSERT INTO @products (producteName, category)
VALUES('Honda','cars')
INSERT INTO @products (producteName, category)
VALUES('Toyota','cars')
INSERT INTO @products (producteName, category)
VALUES('Yamaha','motor scooter')
INSERT INTO @products (producteName, category)
VALUES('Romet','motor scooter')
INSERT INTO @products (producteName, category)
VALUES('Zipp','motor scooter')
INSERT INTO @products (producteName, category)
VALUES('Kymco','motor scooter')
INSERT INTO @products (producteName, category)
VALUES('Router','motor scooter')
INSERT INTO @products (producteName, category)
VALUES('Zumico','motor scooter')
INSERT INTO @products(producteName, category)
VALUES('Fiat','caravan')
INSERT INTO @products (producteName, category)
VALUES('Skoda','caravan')

SELECT T.*
FROM (
	SELECT productID, producteName, category, ROW_NUMBER() OVER (PARTITION BY category ORDER BY productID DESC) AS Number
	FROM @products
	) AS T
WHERE T.Number < 5


Result:
productIDproducteNamecategoryNumber
14Skodacaravan1
13Fiatcaravan2
6Toyotacars1
5Hondacars2
4Peugeotcars3
3Citroencars4
12Zumicomotor scooter1
11Routermotor scooter2
10Kymcomotor scooter3
9Zippmotor scooter4

[/EDIT]

[EDIT 3]
Read my comments and follow the links.
This artcile[^] is about how to emulate MS SQL ROW_NUMBER() function in MySQL queries, but there are some restrictions.
Quote:
Can we have this result in MySQL? Here are two ways to get it. First, with user variables:
SQL
SET @iprev=0, @jprev=0;
SELECT i, j, row_number
FROM (
  SELECT j, @jprev := if(@iprev = i, @jprev+1, 1) AS row_number, @iprev := i AS i
  FROM test
  ORDER BY i,j
) AS tmp;


The second method uses a join and aggregation, but is correct only if there are no duplicate values of j

SQL
SELECT a.i, a.j, count(*) as row_number
FROM test a
JOIN test b ON a.i=b.i AND a.j >= b.j
GROUP BY a.i, a.j ;  


More interesting example queries, you'll find here: http://www.artfulsoftware.com/infotree/queries.php[^]
[/EDIT]
 
Share this answer
 
v5
Comments
ocean99 26-Mar-13 2:40am    
I tried this but doescn't work !!!
the result is error with syntax
Maciej Los 26-Mar-13 3:26am    
Check updated solution ;)
gvprabu 26-Mar-13 3:35am    
cars category is coming more than 4 times :-)
Maciej Los 26-Mar-13 3:42am    
I see it now ;) And i know the reason. That's because the record count for caravan category is smaller than 4 ;)
I need to work out on it.

Thanks gvprabu ;)
Manfred Rudolf Bihy 26-Mar-13 4:36am    
Hi Maciej, great example you created there for OP. Gets my 5!
I've tried a solution based on your excellent example and would like you to try it out, because I don't have SQL Server 2008 at home yet. Any feed back if works or if there is something wrong with it would be appreciated.

Cheers and thank you!
Based on Maciej Los] excellent example in Solution 2[^] I'd do it by generating a list of all categories first and then work it from there:

SQL
SELECT t.category, DISTINCT p1.category
FROM @products AS p1 RIGHT JOIN (
    SELECT TOP(4) p2.productID, p2.category
    FROM @products AS p1
    ORDER BY p2.productID DESC
) AS t ON p1.category = t.category
ORDER BY p1.category ASC


Sorry that I can't try this as I don't have an instance of SQL Server 2008 Handy (because of the table type used in Maciej's example).

Regards,

— Manfred
 
Share this answer
 
Comments
gvprabu 26-Mar-13 4:50am    
Are u checked the output of ur Query... Pls check
Maciej Los 26-Mar-13 4:51am    
Nice solution ;)
+5!
gvprabu 26-Mar-13 4:53am    
Hi Maciej Los,
the Solution is not correct
Hi,

Check the Solution.... with Solution 1 Sample Data
SQL
DECLARE @ct INT
SET @ct = 4
 
SELECT p.productID, p.producteName, p.category
FROM @products AS p 
INNER JOIN (SELECT productID, 
               ROW_NUMBER() OVER(PARTITION BY category ORDER BY productID DESC) 'RowNumber'
 	    FROM @products) T ON T.productID=p.productID AND T.RowNumber<= @ct
 
Share this answer
 
v2
Comments
Maciej Los 26-Mar-13 4:52am    
Good job!
+5
check this link ...
tips in last lines .
Tip

or maybe you should try this :

SQL
SELECT   top 4 *
FROM         Orders order by RequiredDate desc


this query will select first 4 rows from your table that ordered by RequiredDate (RequiredDate is date of registered row and last row has maximum ) descendly !
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900