?
Solved

SQL -Do not repeat QTD, MTD & YTD values

Posted on 2010-11-15
2
Medium Priority
?
913 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
[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
2 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

800 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