Solved

sql query

Posted on 2007-11-15
6
175 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Backup & Restore 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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now