﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / how to find result for this table / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Mon, 20 May 2013 09:37:34 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>sir , i want to use '0' instead of null.here i couldn't use isNull().why?</description><pubDate>Sun, 10 Feb 2013 21:43:58 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Here i got some kind of error..all columns filled with Null...why?.I couldn't find what..please help me sir,,,/****** Object: Table [dbo].[paymentHistory] Script Date: 02/06/2013 13:56:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[paymentHistory]([PaymentHistoryID] [int] IDENTITY(1,1) NOT NULL,[paymentID] [int] NULL,[receivedAmout] [money] NULL,[receivedDate] [datetime] NULL,[receivedType] [varchar](30) NULL,[BankName] [varchar](100) NULL,[Reason] [varchar](100) NULL,[cheque_DD_no] [varchar](100) NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOSET IDENTITY_INSERT [dbo].[paymentHistory] ONINSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (1, 11, 5000.0000, CAST(0x0000A15900000000 AS DateTime), N'Dr', N'axis', N'ASD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (2, 12, 4000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N' BG', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (3, 11, 8000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N' BG', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (4, 11, 3000.0000, CAST(0x0000A15900000000 AS DateTime), N'Dr', N'axis', N' SD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (5, 11, 3000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N'ASD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (6, 13, 50000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'axis valasai', N'ASD', N'123456')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (7, 13, 50000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'axis valasai', N'ASD', N'123456')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (8, 13, 1000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'', N'ASD', N'')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (9, 13, 500.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'', N'ASD', N'')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (10, 14, 50000.0000, CAST(0x0000A15D00000000 AS DateTime), N'Cr', N'axis', N' FDR', N'54567')SET IDENTITY_INSERT [dbo].[paymentHistory] OFF---My query---------declare @col_Str varchar(max) ,@SQl_Query varchar(max);WITH col as (Select Distinct Reason from paymentHistory )select @col_Str=isnull(@col_Str+',','')+reason from col;set @SQl_Query ='select paymentid,'+@col_Str+'from (select paymentID, CASE receivedType when ''Dr'' then receivedAmout else -1* receivedAmout End receivedAmout ,Reasonfrom paymentHistory)as sourcetablePIVOT( sum(receivedAmout) for reason IN ('+@col_Str+') ) as Pivota 'exec (@SQl_Query);</description><pubDate>Wed, 06 Feb 2013 21:35:37 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Here  i got some kind of error..all columns filled with Null...why?.I couldn't find what..please help me sir,,,/****** Object:  Table [dbo].[paymentHistory]    Script Date: 02/06/2013 13:56:10 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[paymentHistory](	[PaymentHistoryID] [int] IDENTITY(1,1) NOT NULL,	[paymentID] [int] NULL,	[receivedAmout] [money] NULL,	[receivedDate] [datetime] NULL,	[receivedType] [varchar](30) NULL,	[BankName] [varchar](100) NULL,	[Reason] [varchar](100) NULL,	[cheque_DD_no] [varchar](100) NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOSET IDENTITY_INSERT [dbo].[paymentHistory] ONINSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (1, 11, 5000.0000, CAST(0x0000A15900000000 AS DateTime), N'Dr', N'axis', N'ASD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (2, 12, 4000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N' BG', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (3, 11, 8000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N' BG', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (4, 11, 3000.0000, CAST(0x0000A15900000000 AS DateTime), N'Dr', N'axis', N' SD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (5, 11, 3000.0000, CAST(0x0000A15900000000 AS DateTime), N'Cr', N'axis', N'ASD', N'12345')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (6, 13, 50000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'axis valasai', N'ASD', N'123456')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (7, 13, 50000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'axis valasai', N'ASD', N'123456')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (8, 13, 1000.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'', N'ASD', N'')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (9, 13, 500.0000, CAST(0x0000A15B00000000 AS DateTime), N'Cr', N'', N'ASD', N'')INSERT [dbo].[paymentHistory] ([PaymentHistoryID], [paymentID], [receivedAmout], [receivedDate], [receivedType], [BankName], [Reason], [cheque_DD_no]) VALUES (10, 14, 50000.0000, CAST(0x0000A15D00000000 AS DateTime), N'Cr', N'axis', N' FDR', N'54567')SET IDENTITY_INSERT [dbo].[paymentHistory] OFF---My query---------declare @col_Str varchar(max) ,		@SQl_Query varchar(max);									WITH col as (Select Distinct Reason from paymentHistory )		select @col_Str=isnull(@col_Str+',','')+reason from col;set @SQl_Query ='select paymentid,'+@col_Str+'						from (select paymentID, CASE receivedType when ''Dr'' then receivedAmout else -1* receivedAmout End receivedAmout ,Reason						from paymentHistory)as sourcetable						PIVOT( sum(receivedAmout)  for reason IN ('+@col_Str+') ) as Pivota '										exec (@SQl_Query);											</description><pubDate>Wed, 06 Feb 2013 01:35:30 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Select statement cannot be used directly to create headers. Here is how it can be done by creating a list of columns and then creating a PIVOT string which include those columns[code="sql"]DECLARE @ColStr VARCHAR(500),        @SqlStr VARCHAR(MAX); -- Creating Column StringWITH col_str AS(SELECT DISTINCT reason FROM Payment)SELECT @ColStr=ISNULL(@ColStr+',','')+Reason FROM col_str;-- Creating pivot querySET @SqlStr ='SELECT paymentID, '+@ColStr+'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 ('+@ColStr+') ) AS PivotTable '-- Execute QueryEXEC (@SqlStr);[/code]</description><pubDate>Tue, 05 Feb 2013 18:02:50 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>can i create dynamic headers....:-)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; //Here  can i put SELECT statement for Dynamic Headers..</description><pubDate>Tue, 05 Feb 2013 07:43:03 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>This is how it can be done, using PIVOT function[code="sql"]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; [/code]</description><pubDate>Tue, 05 Feb 2013 06:47:24 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Try to do this yourself. Here is the link for your reference:[url]http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx[/url]</description><pubDate>Tue, 05 Feb 2013 02:29:57 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>CREATE TABLE Payment  (     PaymentHistoryID INT,     paymentID        INT,     Amount           DECIMAL(10, 2),     receivedType     CHAR(2),     Reason           VARCHAR(10)  ); INSERT INTO PaymentVALUES (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	   800012		        0	   0	   4000is it possible ...</description><pubDate>Tue, 05 Feb 2013 02:05:50 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>[quote][b]vanapandi (2/5/2013)[/b][hr]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[/quote]I don't think that query executes , does it ?&amp;lt;&amp;lt; For improving the knowledge , you can start with reading msdn articles and experiment a lot ; that would give you some ideas..&amp;gt;&amp;gt;</description><pubDate>Tue, 05 Feb 2013 01:17:49 GMT</pubDate><dc:creator>demonfox</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>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</description><pubDate>Tue, 05 Feb 2013 01:09:33 GMT</pubDate><dc:creator>vanapandi</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>[quote][b]demonfox (2/4/2013)[/b][hr]And the best part ; Float hasa higher precedence over int , so multiplying Amount with -1 doesn't convert the data type of amount ...[/quote]Hey demonfox..spot on with regards to today's Qotd :-)</description><pubDate>Tue, 05 Feb 2013 00:28:22 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>[quote][b]Lokesh Vij (2/4/2013)[/b][hr]Here you go:[code="sql"]CREATE TABLE Payment  (     PaymentHistoryID INT,     paymentID        INT,     Amount           DECIMAL(10, 2),     receivedType     CHAR(2),     Reason           VARCHAR(10)  ); INSERT INTO PaymentVALUES (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,       ReasonFROM   PaymentGROUP  BY paymentID,          Reason [/code][quote][b]demonfox (2/4/2013)[/b][hr]Please provide the ddl ..follow this link for best practices to post :http://www.sqlservercentral.com/articles/Best+Practices/61537/[/quote]Always provide DDL along with test data and expected result, as mentioned by demonfox...[/quote]And the best part ; Float hasa higher precedence over int , so multiplying Amount with -1 doesn't convert the data type of amount ...</description><pubDate>Mon, 04 Feb 2013 23:16:32 GMT</pubDate><dc:creator>demonfox</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Here you go:[code="sql"]CREATE TABLE Payment  (     PaymentHistoryID INT,     paymentID        INT,     Amount           DECIMAL(10, 2),     receivedType     CHAR(2),     Reason           VARCHAR(10)  ); INSERT INTO PaymentVALUES (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,       ReasonFROM   PaymentGROUP  BY paymentID,          Reason [/code][quote][b]demonfox (2/4/2013)[/b][hr]Please provide the ddl ..follow this link for best practices to post :http://www.sqlservercentral.com/articles/Best+Practices/61537/[/quote]Always provide DDL along with test data and expected result, as mentioned by demonfox...</description><pubDate>Mon, 04 Feb 2013 23:00:16 GMT</pubDate><dc:creator>Lokesh Vij</dc:creator></item><item><title>RE: how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>Please provide the ddl ..follow this link for best practices to post :http://www.sqlservercentral.com/articles/Best+Practices/61537/</description><pubDate>Mon, 04 Feb 2013 22:41:29 GMT</pubDate><dc:creator>demonfox</dc:creator></item><item><title>how to find result for this table</title><link>http://www.sqlservercentral.com/Forums/Topic1415570-391-1.aspx</link><description>PaymentHistoryID	paymentID	Amount	receivedType	Reason1	11	5000.00	Dr	ASD2	12	4000.00	Cr	 BG3	11	8000.00	Cr	 BG4	11	3000.00	Dr	 SD5	11	3000.00	Cr	ASD6	13	50000.00	Cr	ASD7	13	50000.00	Cr	ASD8	13	1000.00	Cr	ASD9	13	500.00	Cr	ASDThis is my table………..pls help me at vanapandi@gmail.comPayment ID	Amount(dr-cr)	Reason11	5000-3000=2000	ASD11	0-8000=-8000	BG12	0-4000=-4000	BG I need result like below table…how to write query for getting this table.</description><pubDate>Mon, 04 Feb 2013 22:26:50 GMT</pubDate><dc:creator>vanapandi</dc:creator></item></channel></rss>