SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 21st, 2009, 11:16 AM
eric5210 eric5210 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 3 eric5210 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 7 sec
Reputation Power: 0
Unhappy HELP!!!urgent...who can help me..pls~(sql query problem)

How to Write a select statement to return query Table A and to return in Table B format?

Table A

Name________Date________FieldType_______FieldValue
Tay Hui Boon__1/1/2005_______Title________Project Engineer
Tay Hui Boon__1/1/2005_____Department_____IT Department
Tay Hui Boon__1/1/2007_______Title_____Assist Project Manager
Ong Lay Keow__1/1/2006______Title_______Software Engineer
Ong Lay Keow__1/1/2006____Department_____IT Department

Table B

Date__________Name__________Title_________Departme nt
1/1/2007_____Tay Hui Boon____Assist Project Manager____IT Department
1/1/2006_____Ong Lay Keow_____Software Engineer______IT Department

Reply With Quote
  #2  
Old May 22nd, 2009, 12:00 AM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
Why would the order be important?
"SELECT [Date], [Name], Title, Department FROM tableA"

Are you using VB?
Date and Name are reserved words in VB and possibly in SQL, hence the brackets. Should rename your fields to something like DateHire and NameEmployee.

Reply With Quote
  #3  
Old May 22nd, 2009, 12:10 AM
eric5210 eric5210 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 3 eric5210 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 7 sec
Reputation Power: 0
Quote:
Originally Posted by June7
Why would the order be important?
"SELECT [Date], [Name], Title, Department FROM tableA"

Are you using VB?
Date and Name are reserved words in VB and possibly in SQL, hence the brackets. Should rename your fields to something like DateHire and NameEmployee.



i m using jsp. u mean i jz select statement like "SELECT [Date], [Name], Title, Department FROM tableA", then can return in table B format??Or i nid to use relias to rename the fields???

Reply With Quote
  #4  
Old May 22nd, 2009, 12:20 AM
mualsh mualsh is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 5 mualsh User rank is Private First Class (20 - 50 Reputation Level)mualsh User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 h 12 m 42 sec
Reputation Power: 0
Hopefully, this query works for you....


SELECT
A.Name
, A.Date
, (
SELECT TOP 1 FieldValue
FROM TabA t
WHERE t.FieldType = 'Title'
AND t.Name = A.Name
ORDER BY t.Date DESC
) AS Title
, (
SELECT TOP 1 FieldValue
FROM TabA d
WHERE d.FieldType = 'Department'
AND d.Name = A.Name
ORDER BY d.Date DESC
) As Department
FROM TabA A
WHERE A.Date = ( SELECT MAX(Date) FROM TabA WHERE Name = A.Name )
Comments on this post
June7 agrees: Nested Select statement, haven't tried one before, very nice.

Reply With Quote
  #5  
Old May 22nd, 2009, 12:21 AM
June7 June7 is offline
Contributing User
Click here for more information.
 
Join Date: Apr 2009
Location: The Great Land
Posts: 536 June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level)June7 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 13 h 58 m 7 sec
Reputation Power: 124
This is an SQL statement. I made some assumptions that you were familiar with SQL and opening a recordset with a SELECT statement and were using either VB or Access data tables. Would also have to establish connection to the tables and declare recordset variable.

Dim cn as ADODB.Connection
Set cn = CurrentProject.Connection 'or whatever connection is needed
Dim rs as ADODB.Recordset
Set rs = New ADODB.Connection

rs.Open "SELECT [Date], [Name], FieldValue As Title, FieldType As Department FROM tableA WHERE FieldType = 'IT Department' ORDER by [Date] DESC;", cn, adOpenStatic, adLockPessimistic

I don't know if jsp can recognize SQL. If it does, this may work.

I recommended changing the names in the actual table so you don't have to remember to use brackets in SQL statements.

Edit: Sorry, didn't notice the field alias situation, got stuck thinking you meant field order. Analyzing and composing online not always productive. Query lot more complicated than l initially thought. Believe Mualsh got it.

Reply With Quote
  #6  
Old May 22nd, 2009, 01:16 AM
eric5210 eric5210 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 3 eric5210 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 7 sec
Reputation Power: 0
thx all...i think i gt the solution for tis question le...thank you vy much...

Reply With Quote
  #7  
Old October 21st, 2009, 03:20 AM
sakthi.tnj sakthi.tnj is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 23 sakthi.tnj User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 48 m 35 sec
Reputation Power: 0
RE: Help, Urgent

Hi, I hope the below SQL query will be useful for you
Quote:
Select dept.name,date.date, dept.fieldvalue as Department, title.fieldvalue as Title
from
(select name,fieldvalue from
(select name,fieldvalue,row_number() over(partition by name order by date desc)rno from test2 where fieldtype = 'dept')tbl
where tbl.rno =1) dept
inner join
(select name, fieldvalue from
(select name, fieldvalue,row_number() over(partition by name order by date desc)rno from test2 where fieldtype = 'title')tbl
where tbl.rno =1) title
on dept.name = title.name
inner join
(select name,max(date)as date from test2 group by name)date on date.name = dept.name and date.name = title.name
order by date.date desc


Note: test2 is a Table Name

Thanks & Regards
Sakthimeenakshi.S

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > HELP!!!urgent...who can help me..pls~


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek