SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to find result for this table


how to find result for this table

Author
Message
vanapandi
vanapandi
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 45
PaymentHistoryID paymentID Amount receivedType Reason
1 11 5000.00 Dr ASD
2 12 4000.00 Cr BG
3 11 8000.00 Cr BG
4 11 3000.00 Dr SD
5 11 3000.00 Cr ASD
6 13 50000.00 Cr ASD
7 13 50000.00 Cr ASD
8 13 1000.00 Cr ASD
9 13 500.00 Cr ASD
This is my table………..



pls help me at vanapandi@gmail.com





Payment ID Amount(dr-cr) Reason
11 5000-3000=2000 ASD
11 0-8000=-8000 BG
12 0-4000=-4000 BG
I need result like below table…how to write query for getting this table.
demonfox
demonfox
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1513 Visits: 1192
Please provide the ddl ..

follow this link for best practices to post :
http://www.sqlservercentral.com/articles/Best+Practices/61537/

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2038 Visits: 1599
Here you go:

CREATE TABLE Payment
(
PaymentHistoryID INT,
paymentID INT,
Amount DECIMAL(10, 2),
receivedType CHAR(2),
Reason VARCHAR(10)
);

INSERT INTO Payment
VALUES
(1,11, 5000.00,'Dr','ASD'),
(2,12, 4000.00,'Cr', 'BG'),
(3,11, 8000.00,'Cr', 'BG'),
(4,11, 3000.00,'Dr', 'SD'),
(5,11, 3000.00,'Cr','ASD'),
(6,13,50000.00,'Cr','ASD'),
(7,13,50000.00,'Cr','ASD'),
(8,13, 1000.00,'Cr','ASD'),
(9,13, 500.00,'Cr','ASD')

SELECT paymentID,
Sum(CASE receivedType
WHEN 'Dr' THEN Amount
ELSE -1 * Amount
END) Amount,
Reason
FROM Payment
GROUP BY paymentID,
Reason




demonfox (2/4/2013)
Please provide the ddl ..

follow this link for best practices to post :
http://www.sqlservercentral.com/articles/Best+Practices/61537/



Always provide DDL along with test data and expected result, as mentioned by demonfox...

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


demonfox
demonfox
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1513 Visits: 1192
Lokesh Vij (2/4/2013)
Here you go:

CREATE TABLE Payment
(
PaymentHistoryID INT,
paymentID INT,
Amount DECIMAL(10, 2),
receivedType CHAR(2),
Reason VARCHAR(10)
);

INSERT INTO Payment
VALUES
(1,11, 5000.00,'Dr','ASD'),
(2,12, 4000.00,'Cr', 'BG'),
(3,11, 8000.00,'Cr', 'BG'),
(4,11, 3000.00,'Dr', 'SD'),
(5,11, 3000.00,'Cr','ASD'),
(6,13,50000.00,'Cr','ASD'),
(7,13,50000.00,'Cr','ASD'),
(8,13, 1000.00,'Cr','ASD'),
(9,13, 500.00,'Cr','ASD')

SELECT paymentID,
Sum(CASE receivedType
WHEN 'Dr' THEN Amount
ELSE -1 * Amount
END) Amount,
Reason
FROM Payment
GROUP BY paymentID,
Reason




demonfox (2/4/2013)
Please provide the ddl ..

follow this link for best practices to post :
http://www.sqlservercentral.com/articles/Best+Practices/61537/



Always provide DDL along with test data and expected result, as mentioned by demonfox...


And the best part ; Float hasa higher precedence over int , so multiplying Amount with -1 doesn't convert the data type of amount ...

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2038 Visits: 1599
demonfox (2/4/2013)

And the best part ; Float hasa higher precedence over int , so multiplying Amount with -1 doesn't convert the data type of amount ...


Hey demonfox..spot on with regards to today's Qotd :-)

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


vanapandi
vanapandi
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 45
thank you sir,,,i got result...i want to improve my knowledge in sql server..can you suggest me..

i am also finally got result by using sub query...whether it is wrong way



select distinct
cr=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Cr' and Reason=ph.Reason),'0'),
Dr=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Dr' and Reason=ph.Reason),'0'),
Amount=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Cr' and Reason=ph.Reason),'0')-
isnull((select sum(receivedAmout) from paymentHistory where receivedType='Dr' and Reason=ph.Reason),'0'),

Reason from paymentHistory ph
group by Reason
demonfox
demonfox
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1513 Visits: 1192
vanapandi (2/5/2013)
thank you sir,,,i got result...i want to improve my knowledge in sql server..can you suggest me..

i am also finally got result by using sub query...whether it is wrong way



select distinct
cr=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Cr' and Reason=ph.Reason),'0'),
Dr=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Dr' and Reason=ph.Reason),'0'),
Amount=isnull((select sum(receivedAmout) from paymentHistory where receivedType='Cr' and Reason=ph.Reason),'0')-
isnull((select sum(receivedAmout) from paymentHistory where receivedType='Dr' and Reason=ph.Reason),'0'),

Reason from paymentHistory ph
group by Reason

I don't think that query executes , does it ?

<< For improving the knowledge , you can start with reading msdn articles and experiment a lot ; that would give you some ideas..>>

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
vanapandi
vanapandi
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 45
CREATE TABLE Payment
(
PaymentHistoryID INT,
paymentID INT,
Amount DECIMAL(10, 2),
receivedType CHAR(2),
Reason VARCHAR(10)
);

INSERT INTO Payment
VALUES
(1,11, 5000.00,'Dr','ASD'),
(2,12, 4000.00,'Cr', 'BG'),
(3,11, 8000.00,'Cr', 'BG'),
(4,11, 3000.00,'Dr', 'SD'),
(5,11, 3000.00,'Cr','ASD'),
(6,13,50000.00,'Cr','ASD'),
(7,13,50000.00,'Cr','ASD'),
(8,13, 1000.00,'Cr','ASD'),
(9,13, 500.00,'Cr','ASD')



Now I got another problem..i need result like that..

paymentID SD ASD BG
-----------------------------------
11 -3000 -2000 8000
12 0 0 4000
is it possible ...
Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2038 Visits: 1599
Try to do this yourself. Here is the link for your reference:
http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


Lokesh Vij
Lokesh Vij
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2038 Visits: 1599
This is how it can be done, using PIVOT function

SELECT paymentID, [ASD], [BG],[SD]
FROM (SELECT paymentID,
CASE receivedType WHEN 'Dr' THEN Amount ELSE -1 * Amount END Amount, Reason
FROM Payment) AS SourceTable
PIVOT ( Sum(Amount)
FOR Reason IN ([ASD], [BG], [SD]) ) AS PivotTable;



~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter


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