Solved

SAL  Syntax

Posted on 2012-04-10
3
373 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to trim a value in SQL 2 52
Data conversion error in SSIS 3 56
T-SQL: Only Wanting One Record 8 61
SqlServer amend PK column 5 14
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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