Solved

SAL  Syntax

Posted on 2012-04-10
3
370 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
3 Comments
 
LVL 73

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:deepakChauhan
deepakChauhan 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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