Solved

sql query

Posted on 2007-11-15
6
176 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
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.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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