?
Solved

Subqueries with Aggregates and Constants

Posted on 2009-05-19
5
Medium Priority
?
316 Views
Last Modified: 2012-05-07
Problem:  Need to transform data from horizontal to vertical.  IE

Part           Date           Defect1     Defect2
123         5/1/2009           5               6

To

Part           Date            Defect       DefectTotal
123         5/1/2009       Defect1             5
123         5/1/2009       Defect2             6

I am pretty sure this needs to be done with a sub query and I am not sure how to create it.  

I have attached a snip of code.  Can you tell me where to enter the subquery to Keep updated the Fields Defect and DefectTotal?

Thanks
Select 'ColdSpots' As Defect,Sum(ColdSpot) as DefectTotal, 
	From SCRAP
Where Dt between '5/1/2009' and '5/20/2009'

Open in new window

0
Comment
Question by:Jlucht
[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
  • 2
5 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24422634
how many columns total do you have?  ie Defect1, Defect2, etc.
0
 
LVL 3

Author Comment

by:Jlucht
ID: 24422694
15 defects in total
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 24422778
easy way to do it in 2000 (2005 would be different)
declare @date1 datetime, @date2 datetime
select @date1 = '5/1/2009', @date2 = '5/20/2009'

select part, date, defect = 'Defect1' , defecttotal = Defect1
From SCRAP
Where Dt between @date1 and @date2
UNION ALL
select part, date, defect = 'Defect2' , defecttotal = Defect2
From SCRAP
Where Dt between @date1 and @date2

....et.al.
0
 
LVL 3

Author Comment

by:Jlucht
ID: 24424446
That is the code that I need to do what I need.  One more question.  How would I insert that record set into a table.


Below is the code that I used to do it.  I had to change a couple of things.  

Thanks ALOT!!!

Joe



declare @date1 datetime, @date2 datetime
select @date1 = '5/1/2009', @date2 = '5/20/2009'
 
select Distinct partnum, defect = 'Holes' , defecttotal = Sum(Hole)
From SCRAP
Where Dt between @date1 and @date2
Group By Scrap.PartNum
UNION ALL
select Distinct partNum, defect = 'ColdSpot' , defecttotal = Sum(ColdSpot)
From SCRAP
Where Dt between @date1 and @date2
Group By Scrap.PartNum

Open in new window

0
 
LVL 3

Author Closing Comment

by:Jlucht
ID: 31583029
Thanks for everything
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

801 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