Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T SQL statement issue


T SQL statement issue

Author
Message
chandrakant_gaurav
chandrakant_gaurav
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 168
Guys,
I have a requirement as follows:
CREATE TABLE #temp
(CallDate DAtetime,
Talktime INT,
ID INT,
others Varchar(10))
INSERT INTO #temp
SELECT '2013-01-01', 10,1,'ABC'
UNION
SELECT '2013-01-01', 10,1,'---'
UNION
SELECT '2013-01-01', 20,2,'XYZ'
UNION
SELECT '2013-01-01', 20,2,'--'
UNION
SELECT '2013-01-01', 30,3,'asd'
UNION
SELECT '2013-01-01', 30,3,'---'
UNION
SELECT '2013-01-02', 40,1,'ABCD'
UNION
SELECT '2013-01-02', 40,1,'---_'
UNION
SELECT '2013-01-02', 30,2,'XYZQ'
UNION
SELECT '2013-01-02', 30,2,'--1'
UNION
SELECT '2013-01-02', 20,3,'asdf'
UNION
SELECT '2013-01-02', 20,3,'---'

Using a query like this:
--------------
select calldate,
   SUM(talktime) as Overall,
   case when ID = 1 then SUM(talktime) END AS A_Talktime,
   case when ID = 2 then SUM(talktime) END AS B_Talktime,
   case when ID = 3 then SUM(talktime) END AS C_Talktime
FROM #temp
GROUP BY calldate, ID
--------------------
I need an output like below:

Calldate------------Overall------A_Talktime-----B_Talktime------C_Talktime
_________________________________________________________________
2013-01-01----------120-----------20-------------40--------------60
2013-01-02----------180-----------80-------------60--------------40


Let me know if any other info is required.

Thanks
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19012
So nearly there...

SELECT 
   calldate,
   Overall      = SUM(talktime),
   A_Talktime   = SUM(case when ID = 1 then talktime END),
   B_Talktime   = SUM(case when ID = 2 then talktime END),
   C_Talktime   = SUM(case when ID = 3 then talktime END)
FROM #temp
GROUP BY calldate



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
chandrakant_gaurav
chandrakant_gaurav
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 Visits: 168
Its embarassing Blush . Thank you!!
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 8957 Visits: 19012
chandrakant_gaurav (2/21/2013)
Its embarassing Blush . Thank you!!


Heh - don't be embarrassed, you almost got there. Thanks loads for posting a sample script.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44972 Visits: 39864
chandrakant_gaurav (2/21/2013)
Its embarassing Blush . Thank you!!


If you'd like to learn more about this "CROSSTAB" technique (which is also known as a PIVOT), please see the following articles...

http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search