Solved

SAL  Syntax

Posted on 2012-04-10
3
374 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 167 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 167 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 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 166 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 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