Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Newbie Question: How to implement example code?

Posted on 2008-10-16
7
Medium Priority
?
179 Views
Last Modified: 2010-04-21
Hi All,
I'm brand new to SQL but can usually pick up programming pretty easy so this is frustrating. When I see the example code for this solution, I'm not sure how to interpret it. Excuse my ignorance, please!!

When this code says to select "r", is that a dummy table name or is the user meant to insert the name of the table with the data in it? And is create_date something you leave in there, or do you define it elsewhere, or insert your own date? Is rev_id the id you want to group by?

Just simple, quick descriptions of what these terms are referring to would help me so much and would be greatly appreciated.
select r.* from review as r
inner join 
(
select rev_id,max(create_date) as max_dat
from review
group by rev_id
) as rev_max  on r.rev_id = rev_max.rev_id and r.create_date = rev_max.create_date

Open in new window

0
Comment
Question by:chaimberbell
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:natloz
ID: 22731660
select r.* from review as r

This statemet "from review as r" has aliased "r" as the new table name which can than be used in the query....
0
 
LVL 7

Accepted Solution

by:
natloz earned 500 total points
ID: 22731689
"r" now represents the table "review"

you could go...

select a.value, b.value
from tblApple a
inner join tblBanana b on pkFruitID = fkFruitID
where a.value = 'green' and b.value = 'yellow'

a = tblApple
b = tblBanana
0
 

Author Comment

by:chaimberbell
ID: 22731779
Ah, thanks. That part makes more sense now.

What about the create_date part?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 7

Expert Comment

by:natloz
ID: 22731825
create_date looks like it is simply a field in "review" that is being passed into the MAX function to gets the maximum value found.
0
 
LVL 7

Expert Comment

by:natloz
ID: 22731864
Depending on what you are trying to combine will determine what you want to group by.
If there is only one create_date per review and you are looking for the max of create_date, I would probably group by create_date and remove rev_id from the query, since any field in a query that has an aggregate function (such as MAX) has to be in the group by statement.
0
 

Author Closing Comment

by:chaimberbell
ID: 31506766
Thanks!
0
 

Author Comment

by:chaimberbell
ID: 22731911
I missed that those were the names of the fields. Thanks for clarifying! I'm gonna give it a try now :-).
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

824 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