473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How do i phrase this simple query

375 256MB
Hi,

I need help in phrasing this simple query.
I have two tables
1. Category table, it consists of categoryid and productid
2. product table, it consists of categoryid, productid, productname, etc.

Now I need to display 3 categories with 4 products in each category.
Its not necessary that all categories will have 4 products, so first i want to find if there are 4 products in that particular category , if yes then display it.


Further I want to input the number of categories and products from user end.
eg. if the user asks 1 category and 2 products of the same category i should be able to display that
or he may ask 5 categorys and all products.

Can anyone help please
Regards
cmrhema
Jun 21 '09 #1
3 1647
ck9663
2,878 Expert 2GB
What do you have so far?


--- CK
Jun 21 '09 #2
cmrhema
375 256MB
Ok I am providing you with sample data

Category Id CategoryName
1 Gifts
2 Sports
3 Caskets
4 Cards



Product Id CategoryId ProductName
3 1 Bag
4 1 Camera 3D
5 1 MousePad
10 1 LampShade
11 1 NamePlate
12 1 Box
6 2 Apron
7 2 Baby
8 2 Cup
103 3 Casket Spray with Fabric Drape
104 3 Full Cover Hanging Casket Spray
105 3 Green Casket Spray with Apple
106 3 Infant Casket Spray with Daisies
107 3 Interior Casket Lid Arrangement

The user will input 3,2(ie, 3 categories with 2 products each), the output should be
3 1 Bag
4 1 Camera 3D
6 2 Apron
7 2 Baby
103 3 Casket Spray with Fabric Drape
104 3 Full Cover Hanging Casket Spray


If he gives 2,4(ie. 2 categories with 3 products each) the output should be as follows, it should not take the category number 2 as it has only 2 products

3 1 Bag
4 1 Camera 3D
5 1 MousePad
10 1 LampShade
103 3 Casket Spray with Fabric Drape
104 3 Full Cover Hanging Casket Spray
105 3 Green Casket Spray with Apple
106 3 Infant Casket Spray with Daisies

I tried this way, now only one restriction, I want to pass the value "3", in "select distinct top 3" as a parameter, thro stored procedure,


select * from es_product where es_product.categoryid in
(select distinct top 3 id from es_category)order by es_product.categoryid



in the stored procedure when I write

declare @count as int

set @count=3

select * from es_product where es_product.categoryid in
(select distinct top @count id from es_category)order by es_product.categoryid

it does not accept
what the variable count
What should I do?

Regards
cmrhema
Jun 22 '09 #3
code green
1,726 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. select distinct top 
  2. CAST(@count id AS VARCHAR(4) )
  3. from es_category)order by es_product.categoryid
or use ROWCOUNT
Jun 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: ash | last post by:
I want search phrase in html file and mark (like Google when I click on "cached") it (phrase). Does somebody know any class, that can help me? Maybe somebody know how could I make this? thanks
0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
2
by: Robert W. | last post by:
In my current work I noticed that I have several circumstances where I need to create little if/else constructs to handle the phrasing of a message. This typically involves a ternary situation like...
1
by: Michal Hlavac | last post by:
hello, I have table with text attribute and fulltext index... is it possible to do query with result: word or phrase, count for example: SELECT word, my_count(word) FROM table WHERE...
2
by: cptuser | last post by:
Hi, I'm a novice and I have the following code, but I can't get it to work. All i want to be able to do is a simple form validation for a single field, so that if the user enters a particular...
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
1
by: chromis | last post by:
Hi, I've been trying to create a carousel class which takes an array of phrases and then creates a textfield for each one positioning it vertically based on the order it was added. The next stage...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.