SQL Clone
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 Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 174
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
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39174 Visits: 19991
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 Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 174
Its embarassing Blush . Thank you!!
ChrisM@Work
ChrisM@Work
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39174 Visits: 19991
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 Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203204 Visits: 41947
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

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