Anoop Bansal

Anoop Bansal

  • NA
  • 58
  • 5.8k

Subtract Qty from 2 tables in Sql

Nov 17 2017 8:25 AM
Hello all
I am having 2 tables
1. InventoryInwards
2. InventoryIssued
 
I want an output by subtracting the qty from both the above tables based on NameOfStores, ReferenceNo and Unit.
 
I wrote the below query, but it is showing wrong output, whereas running the same query on individual tables gives the correct result. 
 
Wrong Output:
 
  1. declare @storeid bigint  
  2. set @storeid=19  
  3. select  I.NameOfStores, I.ReferenceNo, SUM(Inwards.InwardQty) InwardQty,  SUM(ISNULL(Issued.IssuedQty,0)) IssuedQty, SUM((ISNULL(Inwards.InwardQty,0)-ISNULL(Issued.IssuedQty,0)))as Balance  
  4. from InventoryInwards as I   
  5. LEFT JOIN  
  6.         (select   nameofStores, ISNULL(Sum(ISNULL(Qty,0)),0) as InwardQty  
  7.         from InventoryInwards where StoreID=@storeid  
  8.         group By NameOfStores, ReferenceNo, Unit) as Inwards ON I.NameOfStores = Inwards.NameOfStores  
  9. left JOIN  
  10.         (select   nameofStores, ISNULL(Sum(ISNULL(Qty,0)),0) as IssuedQty  
  11.         from InventoryIssued where StoreID=@storeid  
  12.         group By NameOfStores, ReferenceNo, unit) as Issued on I.NameOfStores= Issued.NameOfStores  
  13.   
  14. where StoreID = @storeid  
  15. group by I.NameOfStores, I.ReferenceNo, I.Unit  
 
The above result shows samsung with referenceno 001 having 60 as its inwards qty, where as the actual qty is 10
 
  1. select   nameofStores, ReferenceNo, ISNULL(Sum(ISNULL(Qty,0)),0) as InwardQty  
  2.         from InventoryInwards where StoreID=19  
  3.         group By NameOfStores, ReferenceNo, Unit  
 
 
Please do let me know, where m i making mistakes
 
Table Structure with sample data
  1. CREATE TABLE [dbo].[InventoryInwards](  
  2.     [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  3.     [InwardID] [bigintNULL,  
  4.     [Date] [datetime] NULL,  
  5.     [NameOfStores] [nvarchar](maxNULL,  
  6.     [ReferenceNo] [nvarchar](maxNULL,  
  7.     [Qty] [decimal](18, 2) NULL,  
  8.     [Unit] [nvarchar](maxNULL,  
  9.     [Rate] [decimal](18, 2) NULL,  
  10.     [Nature] [nvarchar](maxNULL,  
  11.     [CreatedDate] [datetime] NULL,  
  12.     [CreatedBy] [nvarchar](maxNULL,  
  13.     [StoreID] [bigintNULL,  
  14.  CONSTRAINT [PK_InventoryInwards] PRIMARY KEY CLUSTERED   
  15. (  
  16.     [ID] ASC  
  17. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  18. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  19.   
  20. GO  

  21. SET ANSI_NULLS ON  
  22. GO  
  23. SET QUOTED_IDENTIFIER ON  
  24. GO  
  25. CREATE TABLE [dbo].[InventoryIssued](  
  26.     [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  27.     [IssuedID] [bigintNULL,  
  28.     [Date] [datetime] NULL,  
  29.     [NameOfStores] [nvarchar](maxNULL,  
  30.     [ReferenceNo] [nvarchar](maxNULL,  
  31.     [Qty] [decimal](18, 2) NULL,  
  32.     [Unit] [nvarchar](maxNULL,  
  33.     [Rate] [decimal](18, 2) NULL,  
  34.     [Nature] [nvarchar](maxNULL,  
  35.     [IssuedTo] [nvarchar](maxNULL,  
  36.     [IssuedBy] [nvarchar](maxNULL,  
  37.     [Remarks] [nvarchar](maxNULL,  
  38.     [CreatedDate] [datetime] NULL,  
  39.     [CreatedBy] [nvarchar](maxNULL,  
  40.     [StoreID] [bigintNULL,  
  41.  CONSTRAINT [PK_InventoryIssued] PRIMARY KEY CLUSTERED   
  42. (  
  43.     [ID] ASC  
  44. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  45. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  46.   
  47. GO  
  48. SET IDENTITY_INSERT [dbo].[InventoryInwards] ON   
  49.   
  50. GO  
  51. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (1, 0, CAST(0x0000A823016B30CD AS DateTime), N'pipe', N'sdf'CAST(12.00 AS Decimal(18, 2)), N'12'CAST(12.00 AS Decimal(18, 2)), N'sf'CAST(0x0000A82300000000 AS DateTime), N'', 1)  
  52. GO  
  53. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (2, 0, CAST(0x0000A823016B32BB AS DateTime), N'mobile', N'sdf'CAST(12.00 AS Decimal(18, 2)), N'12'CAST(14.00 AS Decimal(18, 2)), N'sf'CAST(0x0000A823016B32BB AS DateTime), N'', 1)  
  54. GO  
  55. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (3, 0, CAST(0x0000A823016B33C9 AS DateTime), N'mobile', N'sdf'CAST(12.00 AS Decimal(18, 2)), N'12'CAST(16.00 AS Decimal(18, 2)), N'sf'CAST(0x0000A823016B33C9 AS DateTime), N'', 1)  
  56. GO  
  57. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (7, 0, CAST(0x0000A823016B38B2 AS DateTime), N'gold', N'sdf'CAST(12.00 AS Decimal(18, 2)), N'12'CAST(22.00 AS Decimal(18, 2)), N'sf'CAST(0x0000A823016B38B2 AS DateTime), N'', 1)  
  58. GO  
  59. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (8, 0, CAST(0x0000A823016B3A0C AS DateTime), N'silver', N'sdf'CAST(12.00 AS Decimal(18, 2)), N'12'CAST(26.00 AS Decimal(18, 2)), N'sf'CAST(0x0000A823016B3A0C AS DateTime), N'', 1)  
  60. GO  
  61. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (9, 0, CAST(0x0000A823016B7A25 AS DateTime), N'pen', N'sf'CAST(22.00 AS Decimal(18, 2)), N'232'CAST(112.00 AS Decimal(18, 2)), N''CAST(0x0000A823016B7A25 AS DateTime), N'', 1)  
  62. GO  
  63. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (10, 0, CAST(0x0000A82400000000 AS DateTime), N'asd', N'001'CAST(10.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'allotment'CAST(0x0000A82500EA44E4 AS DateTime), N'', 1)  
  64. GO  
  65. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (11, 0, CAST(0x0000A8260000AE20 AS DateTime), N'gold', N'020'CAST(10.00 AS Decimal(18, 2)), N'nos'CAST(120.00 AS Decimal(18, 2)), N'procurement'CAST(0x0000A8260000F098 AS DateTime), N'', 1)  
  66. GO  
  67. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (12, 0, CAST(0x0000A8290155BDA2 AS DateTime), N'pipe', N'001'CAST(120.00 AS Decimal(18, 2)), N'12'CAST(120.00 AS Decimal(18, 2)), N'procurement'CAST(0x0000A8290155CF98 AS DateTime), N'', 1)  
  68. GO  
  69. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (13, 0, CAST(0x0000A82B00283BF5 AS DateTime), N'asd', N'001'CAST(10.00 AS Decimal(18, 2)), N'12'CAST(120.00 AS Decimal(18, 2)), N''CAST(0x0000A82B00284854 AS DateTime), N'', 1)  
  70. GO  
  71. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (14, 0, CAST(0x0000A82B01836DC1 AS DateTime), N'name1', N'ref1'CAST(100.00 AS Decimal(18, 2)), N'nos'CAST(100.00 AS Decimal(18, 2)), N'purchase'CAST(0x0000A82B01838544 AS DateTime), N'', 19)  
  72. GO  
  73. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (15, 0, CAST(0x0000A82B01836DC1 AS DateTime), N'mobile', N'ref1'CAST(50.00 AS Decimal(18, 2)), N'pcs'CAST(120.00 AS Decimal(18, 2)), N'purchase'CAST(0x0000A82B01839C40 AS DateTime), N'', 19)  
  74. GO  
  75. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (18, 0, CAST(0x0000A82E002DE858 AS DateTime), N'samsung', N'001'CAST(10.00 AS Decimal(18, 2)), N'nos'CAST(100.00 AS Decimal(18, 2)), N'purchase'CAST(0x0000A82E002DF8A8 AS DateTime), N'', 19)  
  76. GO  
  77. INSERT [dbo].[InventoryInwards] ([ID], [InwardID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [CreatedDate], [CreatedBy], [StoreID]) VALUES (19, 0, CAST(0x0000A82E002E1FF1 AS DateTime), N'samsung', N'002'CAST(50.00 AS Decimal(18, 2)), N'nos'CAST(120.00 AS Decimal(18, 2)), N'purchase'CAST(0x0000A82E002E2FBA AS DateTime), N'', 19)  
  78. GO  
  79. SET IDENTITY_INSERT [dbo].[InventoryInwards] OFF  
  80. GO  
  81. SET IDENTITY_INSERT [dbo].[InventoryIssued] ON   
  82.   
  83. GO  
  84. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (1, 0, CAST(0x0000A8250101C827 AS DateTime), N'pipe', N'110'CAST(10.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'allotment', N'ashutosh', N'imran', N'asdasdad'CAST(0x0000A82501038436 AS DateTime), N'', 1)  
  85. GO  
  86. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (2, 0, CAST(0x0000A8250101C827 AS DateTime), N'mobile', N'5'CAST(5.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'allotment', N'ashutosh', N'imran', N'asdasdad'CAST(0x0000A8250103D29D AS DateTime), N'', 1)  
  87. GO  
  88. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (3, 0, CAST(0x0000A82600168DE7 AS DateTime), N'asd', N'002'CAST(10.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'allotment', N'ranjan', N'amit', N'test remarks'CAST(0x0000A8260016AA2B AS DateTime), N'', 1)  
  89. GO  
  90. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (4, 0, CAST(0x0000A82600168DE7 AS DateTime), N'pipe', N'002'CAST(100.00 AS Decimal(18, 2)), N'12'CAST(12.00 AS Decimal(18, 2)), N'allotment', N'ranjan', N'amit', N'test remarks'CAST(0x0000A8260016B376 AS DateTime), N'', 1)  
  91. GO  
  92. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (5, 0, CAST(0x0000A82600168DE7 AS DateTime), N'silver', N'001'CAST(30.00 AS Decimal(18, 2)), N'12'CAST(26.00 AS Decimal(18, 2)), N'allotment', N'ranjan', N'amit', N'test remarks'CAST(0x0000A8260016BD89 AS DateTime), N'', 1)  
  93. GO  
  94. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (6, 0, CAST(0x0000A82B002726E8 AS DateTime), N'asd', N''CAST(10.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'', N'', N'', N''CAST(0x0000A82B00272D1C AS DateTime), N'', 1)  
  95. GO  
  96. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (7, 0, CAST(0x0000A82B0027A130 AS DateTime), N'asd', N''CAST(14.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'', N'', N'', N''CAST(0x0000A82B0027A9A4 AS DateTime), N'', 1)  
  97. GO  
  98. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (8, 0, CAST(0x0000A82B00284F19 AS DateTime), N'asd', N'220'CAST(15.00 AS Decimal(18, 2)), N'meter'CAST(100.00 AS Decimal(18, 2)), N'', N'kumar', N'mahesh', N''CAST(0x0000A82B00286AAB AS DateTime), N'', 1)  
  99. GO  
  100. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (9, 0, CAST(0x0000A82B00284F19 AS DateTime), N'gold', N'15'CAST(15.00 AS Decimal(18, 2)), N'12'CAST(22.00 AS Decimal(18, 2)), N'', N'kumar', N'mahesh', N''CAST(0x0000A82B0028715E AS DateTime), N'', 1)  
  101. GO  
  102. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (10, 0, CAST(0x0000A82C00012C18 AS DateTime), N'mobile', N'new ref'CAST(20.00 AS Decimal(18, 2)), N'pcs'CAST(120.00 AS Decimal(18, 2)), N'', N'me', N'me', N''CAST(0x0000A82C00014645 AS DateTime), N'', 19)  
  103. GO  
  104. INSERT [dbo].[InventoryIssued] ([ID], [IssuedID], [Date], [NameOfStores], [ReferenceNo], [Qty], [Unit], [Rate], [Nature], [IssuedTo], [IssuedBy], [Remarks], [CreatedDate], [CreatedBy], [StoreID]) VALUES (11, 0, CAST(0x0000A82C0001839C AS DateTime), N'name1', N''CAST(10.00 AS Decimal(18, 2)), N'nos'CAST(100.00 AS Decimal(18, 2)), N'sold', N'', N'', N''CAST(0x0000A82C000194C4 AS DateTime), N'', 19)  
  105. GO  
  106. SET IDENTITY_INSERT [dbo].[InventoryIssued] OFF  
  107. GO  
 
 
 

Answers (2)