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


Retrieve Data with condition


Retrieve Data with condition

Author
Message
sidharthgs6
sidharthgs6
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 14
Hi,

I have one Table

Userid Monthly Quaterly HalfYearly Yearly
----------------------------------------------------
1 30.00 0.00 0.00 0.00
2 0.00 300.00 0.00 0.00


I want display the data in gridview based on above table.


User PaymentTerm Amount
--------------------------------
Raja Monthly 30.00
Ram Quaterly 300.00


what is the query for above
bleroy
bleroy
SSC-Enthusiastic
SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)SSC-Enthusiastic (172 reputation)

Group: General Forum Members
Points: 172 Visits: 739
What about


SELECT USERID, 'Monthly' as [PERIOD], SUM([MONTHLY] As [PERIOD_TOTAL]
UNION
SELECT USERID, 'Quaterly' as [PERIOD], SUM([Quaterly] As [PERIOD_TOTAL]
UNION
SELECT USERID, 'HealfYearly' as [PERIOD], SUM([HalfYearly] As [PERIOD_TOTAL]
UNION
SELECT USERID, 'Yearly' as [PERIOD], SUM([Yearly] As [PERIOD_TOTAL]



HTH,

B
deepkt
deepkt
SSC Veteran
SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)

Group: General Forum Members
Points: 235 Visits: 358
Try this, unpivot logic:

CREATE TABLE #Amt(Id INT, Monthly Float,Quaterly Float,HealfYearly Float,Yearly Float)

INSERT INTO #Amt (Id ,Monthly ,Quaterly ,HealfYearly ,Yearly )
VALUES (1,100,200,400,800),(2,200,400,600,900)

--SELECT * FROM #Amt
SELECT ID, PaymentTerm, Amount
FROM
(
SELECT Id ,Monthly ,Quaterly ,HealfYearly ,Yearly FROM #Amt

) p
UNPIVOT
(Amount FOR PaymentTerm IN
(
Monthly,
Quaterly,
HealfYearly,
Yearly
)
)AS unpvt

DROP TABLE #Amt
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