Click here to Skip to main content
15,892,005 members
Home / Discussions / Database
   

Database

 
AnswerRe: design a database Pin
smcnulty20005-Mar-12 22:03
smcnulty20005-Mar-12 22:03 
QuestionFinding the Max value per group. Pin
Clark Kent12317-Feb-12 5:47
professionalClark Kent12317-Feb-12 5:47 
AnswerRe: Finding the Max value per group. Pin
scottgp17-Feb-12 8:19
professionalscottgp17-Feb-12 8:19 
GeneralRe: Finding the Max value per group. Pin
Corporal Agarn17-Feb-12 8:36
professionalCorporal Agarn17-Feb-12 8:36 
GeneralRe: Finding the Max value per group. Pin
Clark Kent12321-Feb-12 2:58
professionalClark Kent12321-Feb-12 2:58 
AnswerRe: Finding the Max value per group. Pin
Eddy Vluggen17-Feb-12 8:47
professionalEddy Vluggen17-Feb-12 8:47 
QuestionRe: Finding the Max value per group. Pin
Clark Kent12321-Feb-12 2:55
professionalClark Kent12321-Feb-12 2:55 
AnswerRe: Finding the Max value per group. Pin
Eddy Vluggen21-Feb-12 7:35
professionalEddy Vluggen21-Feb-12 7:35 
Clark Kent123 wrote:
What I need to do is organize a table that has multiple listings of a Project ID and return only 1 of the latest (date related) per Project ID.

The last sql-statement is almost a literal translation of that request; it fetches a unique list of all projectid's (distinct), and then the maximum year for that projectid. Lastly, it fetches the maximum month for that projectid in the max(year).
SQL
SET NOCOUNT ON;
BEGIN TRANSACTION 
SELECT 100005000 as ProjectID, 
       2008 as ReportYear,
       05 as ReportMonth
       INTO vPSR
UNION SELECT 100005000,   2008,	6
UNION SELECT 100005000,   2008,	7
UNION SELECT 100006600,   2008,	8
UNION SELECT 100006600,   2008,	9
UNION SELECT 100006600,   2008,	10
UNION SELECT 100006600,   2008,	11
UNION SELECT 100006600,   2008,	12
UNION SELECT 100006600,   2009,	1
UNION SELECT 100006800,   2008,	8
UNION SELECT 100006800,   2008,	9
UNION SELECT 100006800,   2009,	1
UNION SELECT 100006800,   2009,	2
UNION SELECT 100006800,   2009,	3
UNION SELECT 100006800,   2009,	4
 
SELECT DISTINCT ProjectID
     , (SELECT MAX(ReportYear) FROM vPSR WHERE ProjectID = A.ProjectID) AS MaxReportYear
     , (SELECT TOP 1 ReportMonth FROM vPSR WHERE ProjectID = A.ProjectID AND ReportYear = (
			SELECT MAX(ReportYear) FROM vPSR WHERE ProjectID = A.ProjectID) ORDER BY ReportMonth DESC)
       AS MaxReportMonth
  FROM vPSR AS A 
ROLLBACK

ProjectID   MaxReportYear MaxReportMonth
----------- ------------- --------------
100005000   2008          7
100006600   2009          1
100006800   2009          4

Clark Kent123 wrote:
What if I were to instead create a temporary table and create an extra column with "ReportDate" and add the Year and Month together. (I know someone has also mentioned this in a reply to my first post). So, it would be something to the affect of (ReportYear * 100) + ReportMonth. Then I could write a query with the above sample data and return back per ProjectID the latests info. What do you think? Further, can you please link me to a "How-To" article on creating temp tables in SQL Server 2008?

There's two ways of declaring a temp table in Sql Server, and there's a CodeProject article[^] that compares them Smile | :)

Clark Kent123 wrote:
I don't understand how f.ProjectID can equal vPSR.ProjectID. This seems very circular to me. This is a technique that I don't use often. If you have any info/advice about this way of writing a query can you link me?

It's a subquery; we introduced the table under a new name, being called "F" "A". Let's simply walk through the query; it first fetches all projectid's from vPSR (which we'll call "table A"). For each record, it tries to display the values in the columns of our select-statement, and our second column is a new query. So, it executes that query.
SQL
SELECT MAX(ReportYear) FROM vPSR WHERE ProjectID = A.ProjectID

Now, the table in the query was called "A", so for the first record this subquery would read as below;
SQL
SELECT MAX(ReportYear) FROM vPSR WHERE ProjectID = 100005000

Then it returns that value instead that results from that query.

The technique is called "correlation", which sounds fancy, but really merely means that you're referring to a field in the outer query from a subquery. You could substitute the "A" in the example with any string you'd like, and Sql Server will treat it as if it were a new table with the same contents under that name.
Bastard Programmer from Hell Suspicious | :suss:

GeneralRe: Finding the Max value per group. Pin
Clark Kent12322-Feb-12 2:21
professionalClark Kent12322-Feb-12 2:21 
GeneralRe: Finding the Max value per group. Pin
Eddy Vluggen23-Feb-12 0:05
professionalEddy Vluggen23-Feb-12 0:05 
QuestionSSRS 2005 verses OUTLOOK 2007 Pin
SQL Ed17-Feb-12 1:00
SQL Ed17-Feb-12 1:00 
AnswerRe: SSRS 2005 verses OUTLOOK 2007 Pin
cjb1105-Apr-12 1:24
cjb1105-Apr-12 1:24 
Questionoracle 11g alias to query Pin
hadad14-Feb-12 22:28
hadad14-Feb-12 22:28 
AnswerRe: oracle 11g alias to query Pin
Jörgen Andersson15-Feb-12 1:37
professionalJörgen Andersson15-Feb-12 1:37 
AnswerRe: oracle 11g alias to query Pin
Chris Meech15-Feb-12 3:00
Chris Meech15-Feb-12 3:00 
AnswerRe: oracle 11g alias to query PinPopular
David Skelly15-Feb-12 6:24
David Skelly15-Feb-12 6:24 
JokeRe: oracle 11g alias to query Pin
Bernhard Hiller15-Feb-12 21:04
Bernhard Hiller15-Feb-12 21:04 
GeneralRe: oracle 11g alias to query Pin
David Skelly15-Feb-12 22:05
David Skelly15-Feb-12 22:05 
Questionconcept of database Pin
messages13-Feb-12 22:56
messages13-Feb-12 22:56 
AnswerRe: concept of database Pin
Simon_Whale14-Feb-12 0:16
Simon_Whale14-Feb-12 0:16 
AnswerRe: concept of database Pin
uspatel17-Feb-12 23:54
professionaluspatel17-Feb-12 23:54 
NewsRe: concept of database Pin
Eddy Vluggen18-Feb-12 2:52
professionalEddy Vluggen18-Feb-12 2:52 
GeneralRe: concept of database Pin
messages19-Feb-12 5:09
messages19-Feb-12 5:09 
Questionhow do i design a database system for an app? Pin
BupeChombaDerrick13-Feb-12 13:06
BupeChombaDerrick13-Feb-12 13:06 
AnswerRe: how do i design a database system for an app? Pin
Mycroft Holmes13-Feb-12 13:35
professionalMycroft Holmes13-Feb-12 13:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.