Solved

sql query

Posted on 2007-11-15
6
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 500 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to simplify my SQL statement? 14 61
Query to Add Late Tolerance 10 86
Update a summary table with values from detail records 6 42
What Is an Error? 2 59
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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