Solved

SQL Query - Repeating a Row

Posted on 2014-09-23
3
123 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 48

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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