Solved

SQL Query - Repeating a Row

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Set Value of Multiple Fields in One Query 10 26
query question 12 32
Check ALL SP in database make sure there are no errors 17 45
Stored Proc - Rewrite 42 60
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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