Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

sql query

Posted on 2007-11-15
6
Medium Priority
?
185 Views
Last Modified: 2010-03-19
CategoryNumber      EmployeeID      CategoryName
10101013                             1      Chargers
1010101310                             1      AC Chargers A/M
1010101311                             1      AC Charger OEM
1010101312                             1      Car Chargers A/M
1010101313                       1      Car Chargers OEM

i have records in a table like above the table strucutre is
categorynumber text
employeeid number
categorynumber text

i want to make report like

main category    subcategory    

how can i write a query for that.

0
Comment
Question by:romeiovasu
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Marcel Hopman
ID: 20292650
Assuming the numbering CategoryNumber is consistant then you could do something like :

SELECT	(
	SELECT	CategoryName
	FROM	TableA
	WHERE	CategoryNumber = LEFT(CategoryNumber,7)
	) AS [Main Category]
	CategoryName AS subcategory
FROM	TableA
WHERE	LEN(CategoryNumber) > 7		

Open in new window

0
 

Author Comment

by:romeiovasu
ID: 20292657
some of them have only 4 in length what should i do for that.
0
 
LVL 10

Expert Comment

by:pai_prasad
ID: 20292934
how do u define MainCategory as?

I want to make report like
main category    subcategory    
>> yeah ..but can u give sample data for us to understand the problem??

Thanks
Prasad
http://support.microsoft.com/kb/q555375
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Marcel Hopman
ID: 20292975
Are there any other "markings" for recognising main Categories?
0
 

Author Comment

by:romeiovasu
ID: 20293199
no that is the only one.
0
 
LVL 6

Accepted Solution

by:
Marcel Hopman earned 2000 total points
ID: 20293326

The solution is "ugly" but if there are no other ways to recognise the categories you can try this:
(The beter solution wil be to split up the CategoryNumber in the database: a Category and a subcategory field)

SELECT	(
	SELECT	CategoryName
	FROM	TableA
	WHERE	CategoryNumber = LEFT(CategoryNumber,7)
	) AS [Main Category]
	CategoryName AS subcategory
FROM	TableA
WHERE	LEN(CategoryNumber) > 7		
 
UNION
 
SELECT	(
	SELECT	CategoryName
	FROM	TableA
	WHERE	CategoryNumber = LEFT(CategoryNumber,4)
	) AS [Main Category]
	CategoryName AS subcategory
FROM	TableA
WHERE	LEN(CategoryNumber) > 4 
AND 	LEN(CategoryNumber) < 7	 

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question