Solved

record selection

Posted on 2008-10-23
5
187 Views
Last Modified: 2012-05-05
is there a way to extract multiple records into 1 record
i am trying the listed statement
select saletype, classDesc, selectyear,
case when yearnumber = 1 then prem_amt end as Year1,
case when yearnumber = 2 then prem_amt end as Year2,    
case when yearnumber = 3 then prem_amt end as Year3,
case when yearnumber = 4 then prem_amt end as Year4
from qClassDescSummaryReport
group by saletype, classDesc, selectyear

it gives me two error messages
Column 'qClassDescSummaryReport.prem_amt' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

Column 'qClassDescSummaryReport.YearNumber' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

is there a way to use pseudo names  for these missing columns




0
Comment
Question by:ldcray
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 22790557
You're not attempting any aggregation.... if you're trying to do sums, try something like this.
select saletype, classDesc, selectyear, sum(year1), sum(year2), sum(year3), sum(year4)

from 

(select saletype, classDesc, selectyear,

case when yearnumber = 1 then prem_amt else 0 end as Year1,

case when yearnumber = 2 then prem_amt else 0 end as Year2,    

case when yearnumber = 3 then prem_amt else 0 end as Year3,

case when yearnumber = 4 then prem_amt else 0 end as Year4

from qClassDescSummaryReport

) a

group by saletype, classDesc, selectyear

Open in new window

0
 
LVL 23

Assisted Solution

by:adathelad
adathelad earned 250 total points
ID: 22790651
You should even be able to do it in one select, without the nested query:

select saletype, classDesc, selectyear,
sum(case when yearnumber = 1 then prem_amt else 0 end) as Year1,
sum(case when yearnumber = 2 then prem_amt else 0 end) as Year2,    
sum(case when yearnumber = 3 then prem_amt else 0 end) as Year3,
sum(case when yearnumber = 4 then prem_amt else 0 end) as Year4
from qClassDescSummaryReport
group by saletype, classDesc, selectyear
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22790707
Absolutely.  That is another completely valid way to do that.  I have found that it's sometimes easier to express in different ways so thanks.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now