Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SAL  Syntax

Posted on 2012-04-10
3
Medium Priority
?
375 Views
Last Modified: 2012-04-12
TABLE 1 example data

field1  field2_year
ABC      2012
DFG      2012
HIJ      2012
ABC      2013
DFG      2013
HIJ      2013



        SELECT
            field1
             ,SUM(HL) as HL
             ,SUM(UT) as UL
               ,field2_year
      FROM TABLE1
      GROUP BY field1,field2_year


I have example data in table 1 above.

i need to write a query that will return the following:
ABC      2012
DFG      2012
HIJ      2012
the trick is that i will not know what the value is in field2_year,
so a simple query like select field1,field2_year where field2_year = 2012 will not work.

i tried using the MAX function but it didnt work as i was hoping it would.

Any suggestions?
0
Comment
Question by:MoreThanDoubled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 668 total points
ID: 37829802
S ELECT   field1, SUM(hl) AS hl, SUM(ul) AS ul, field2_year
    FROM (SELECT field1,
                 hl,
                 ul,
                 field2_year,
                 RANK() OVER (PARTITION BY field1 ORDER BY field2_year) r
            FROM table1) x
   WHERE r = 1
GROUP BY field1, field2_year
0
 
LVL 15

Assisted Solution

by:Deepak Chauhan
Deepak Chauhan earned 668 total points
ID: 37829964
Hi use it hope so it will give u expected result

select field1 ,  field2_year
from (select row_number() over (partition by field1 order by field2_year) as row,
field1 ,  field2_year from table1)  a where row =1
0
 
LVL 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 664 total points
ID: 37832688
SELECT
            field1
             ,SUM(HL) as HL
             ,SUM(UT) as UL
               ,field2_year
      FROM TABLE1
      WHERE
            field2_year = (SELECT MAX(field2_year) FROM table1)  --<<-- limit rows to MAX(year) in table
      GROUP BY field1,field2_year
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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