Solved

SQL -Do not repeat QTD, MTD & YTD values

Posted on 2010-11-15
2
902 Views
Last Modified: 2012-05-10
I have the following data:
Store      Dept            Amt            MTD            QTD            YTD
127               0            1.00            913.50      2157.18      7203.18
127               0            6.00            913.50      2157.18      7203.18
127               0            7.00            913.50      2157.18      7203.18
127               2            5.00            134.00      431.00       1298.00
129               0            13.00          155.00      661.00       3343.00
355               2            3.00            706.00      231.00       798.00
355               2            2.00            706.00      231.00       798.00

My client wants the QTD, MTD & YTD to show only on the 1st line
WHERE Store and Dept are the same as the record above it
The desired results shown below:
Store      Dept            Amt            MTD            QTD            YTD
127            0            1.00            913.50      2157.18      7203.18
127            0            6.00            0                  0                  0
127            0            7.00            0                  0                  0
127            2            5.00            134.00      431.00       1298.00
129            0            13.00          155.00      661.00       3343.00
355            2            3.00            706.00      231.00       798.00
355            2            2.00            0                  0                  0

Is there a simple sql statement that will query the top table and return the results above?
0
Comment
Question by:n2dweb
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34140329
select Store, Dept, Amt,
case when RN=1 then MTD else 0 end MTD,
case when RN=1 then QTD else 0 end QTD,
case when RN=1 then YTD else 0 end YTD
from
(
      select *, rn=ROW_NUMBER() over (
            partition by Store, Dept
            order by Dept, Amt) -- or whatever order you like
      from tbl
) X
0
 
LVL 1

Author Comment

by:n2dweb
ID: 34140478
Thanks That Worked!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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