Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query - Repeating a Row

Posted on 2014-09-23
3
Medium Priority
?
155 Views
Last Modified: 2014-10-06
I have a table called "ACCOUNT_TEMP" that has some data in there.  

Here's what my raw table looks like.
Raw Data
From the output example, the column F is what I'm after.  I am able to get all of it with my query, but I need to roll up all the dollar amount in Column E into account "15-200" with the data center.
Output
As you can see, from the above example, the items highlighted, I need to repeat the account "15-200".  This is to roll up all the rows with a value greater than zero in the "Sales Amt" column with the respective "Data Center".  Data Center "K00200" needs to always be first and data center "K00380" always needs to be last.  How would I go about doing this?

Here's what I was able to get with my query, but can't seem to get the highlighted yellow portion and need help.
Query
My sql query.
SELECT	A.ACCOUNT_ID, A.DESCRIPTION, A.TOTAL_BALANCE, A.DATA_CENTER, A.SALES_AMT,

		CASE	WHEN	A.ACCOUNT_ID = '10-200' AND A.DATA_CENTER = 'K00300' 
				THEN	(A.TOTAL_BALANCE - B.SALES_AMT) 
				ELSE	(A.TOTAL_BALANCE + A.SALES_AMT)
		END	NEW_TOTAL
		
FROM	ACCOUNT_TEMP A

		LEFT OUTER JOIN
		(
		SELECT	A.ACCOUNT_ID, SUM(A.SALES_AMT) SALES_AMT
		FROM	ACCOUNT_TEMP A
		GROUP BY A.ACCOUNT_ID
		) B ON B.ACCOUNT_ID = A.ACCOUNT_ID

Open in new window

0
Comment
Question by:holemania
  • 2
3 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 40340629
Is this SQL Server 2005? (or, what version of SQL Server is this for?)

why do we have to start at the table ACCOUNT_TEMP and not earlier?
what is the code that produces ACCOUNT_TEMP?

Is there some hierarchy data available to the query?

can you provide some relevant account_temp data in a reusable format please
0
 

Accepted Solution

by:
holemania earned 0 total points
ID: 40355835
There's no hierarchy other than what I posted.  I was able to get what I needed.  Had to create temp table and worked with the temp table.
0
 

Author Closing Comment

by:holemania
ID: 40363271
I was able to resolve the issue myself.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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