Click here to Skip to main content
15,887,444 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Why is it that Metro style apps do not include "Imports System.Data.SqlClient"? Pin
Afzaal Ahmad Zeeshan26-Jul-15 2:30
professionalAfzaal Ahmad Zeeshan26-Jul-15 2:30 
QuestionHow to find which DIV is selected and get selected DIV element values in jquery Pin
sr15924-Jul-15 0:37
sr15924-Jul-15 0:37 
AnswerRe: How to find which DIV is selected and get selected DIV element values in jquery Pin
Pete O'Hanlon24-Jul-15 1:03
mvePete O'Hanlon24-Jul-15 1:03 
Questioncheck data prior to insert Pin
Member 1185289422-Jul-15 8:04
Member 1185289422-Jul-15 8:04 
AnswerRe: check data prior to insert Pin
Michael_Davies22-Jul-15 8:41
Michael_Davies22-Jul-15 8:41 
AnswerRe: check data prior to insert Pin
Daniel Pfeffer22-Jul-15 9:23
professionalDaniel Pfeffer22-Jul-15 9:23 
QuestionJoining Multiple Tables SQL Query Problem Pin
ratchoy01021-Jul-15 22:16
ratchoy01021-Jul-15 22:16 
AnswerRe: Joining Multiple Tables SQL Query Problem Pin
Richard Deeming22-Jul-15 1:04
mveRichard Deeming22-Jul-15 1:04 
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query:
C#
using (var connection = new SqlConnection("..."))
using (var command = new SqlCommand("... PO_Date Between @FromDate And @ToDate ...", connection))
{
    command.Parameters.AddWithValue("@FromDate", dtfrom.Value);
    command.Parameters.AddWithValue("@ToDate", dtto.Value);
    ...
}


I'm guessing that the problem is that you could have multiple sales and multiple purchases for the same item within the same date range. When you join the tables on the item, every purchase within the date range gets matched up to every sale for the same item in that date range. As a result, the purchase quantities will be multiplied by the number of sales, and the sales quantities will be multiplied by the number of purchases.

Also, since you're using an INNER JOIN, your query won't pick up any items with a purchase but no sale within the date range, or with a sale but no purchase within the date range.

You didn't mention which DBMS you're using. Assuming Microsoft SQL Server 2005 or higher, something like this should work:
SQL
WITH ctePurchases As
(
    SELECT
        Item,
        Sum(Quantity_Received) As Quantity_Received
    FROM
        Purchase
    WHERE
        PO_Date Between @FromDate And @ToDate
    GROUP BY
        Item
),
cteSales As
(
    SELECT
        Item_Description,
        Sum(Quantity) As Quantity
    FROM
        Sales
    WHERE
        Sales_Date Between @FromDate And @ToDate
    GROUP BY
        Item_Description
)
SELECT
    IsNull(P.Item, S.Item_Description) As Item,
    IsNull(P.Quantity_Received, 0) As Purchases,
    IsNull(S.Quantity, 0) As Sold
FROM
    ctePurchases As P
    FULL OUTER JOIN cteSales As S
    ON S.Item_Description = P.Item
;




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Joining Multiple Tables SQL Query Problem Pin
ratchoy01022-Jul-15 4:07
ratchoy01022-Jul-15 4:07 
GeneralRe: Joining Multiple Tables SQL Query Problem Pin
Dave Kreskowiak22-Jul-15 4:55
mveDave Kreskowiak22-Jul-15 4:55 
GeneralRe: Joining Multiple Tables SQL Query Problem Pin
Richard Deeming22-Jul-15 7:38
mveRichard Deeming22-Jul-15 7:38 
QuestionIs it possible to add Custom Suggestions to google.maps.places.Autocomplete Pin
sr15915-Jul-15 23:59
sr15915-Jul-15 23:59 
AnswerRe: Is it possible to add Custom Suggestions to google.maps.places.Autocomplete Pin
Dave Kreskowiak17-Jul-15 3:58
mveDave Kreskowiak17-Jul-15 3:58 
QuestionAsp.net front and back end confusion! Pin
George Tourtsinakis10-Jul-15 5:06
George Tourtsinakis10-Jul-15 5:06 
AnswerRe: Asp.net front and back end confusion! Pin
Pete O'Hanlon10-Jul-15 5:28
mvePete O'Hanlon10-Jul-15 5:28 
GeneralRe: Asp.net front and back end confusion! Pin
George Tourtsinakis12-Jul-15 3:59
George Tourtsinakis12-Jul-15 3:59 
GeneralRe: Asp.net front and back end confusion! Pin
Dave Kreskowiak12-Jul-15 4:29
mveDave Kreskowiak12-Jul-15 4:29 
GeneralRe: Asp.net front and back end confusion! Pin
George Tourtsinakis18-Jul-15 23:16
George Tourtsinakis18-Jul-15 23:16 
QuestionClone child gridview in nested gridview Pin
Member 118269799-Jul-15 21:20
Member 118269799-Jul-15 21:20 
AnswerRe: Clone child gridview in nested gridview Pin
User 418025428-Jul-15 8:54
User 418025428-Jul-15 8:54 
QuestionHow to get Memory usage size of each services running on Application Server using C#.net Pin
ven7538-Jul-15 20:18
ven7538-Jul-15 20:18 
AnswerRe: How to get Memory usage size of each services running on Application Server Pin
Michael_Davies8-Jul-15 20:24
Michael_Davies8-Jul-15 20:24 
GeneralRe: How to get Memory usage size of each services running on Application Server using C#.net Pin
ven7538-Jul-15 20:28
ven7538-Jul-15 20:28 
GeneralRe: How to get Memory usage size of each services running on Application Server using C#.net Pin
Michael_Davies8-Jul-15 21:08
Michael_Davies8-Jul-15 21:08 
GeneralRe: How to get Memory usage size of each services running on Application Server using C#.net Pin
ven7538-Jul-15 23:16
ven7538-Jul-15 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.