Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two Sql query,
First query total is correct display Column1(Date), Column2(Total) on date search

if First Query Total is 100

Second query, when i add column2 in query Total not display correct
Column1(Date), Column2(User_Name),Column3(Total) on date search

Second Query Total is 200 after column add

My first Query total is correct, i want same total in second query after add column2(User_Name)

What I have tried:

'First Query
SQL
SELECT  Search.dbo.Invoice_Detail.Invoice_date"Date", Sum(Search.dbo.Invoice_Detail.Product_total)"Total" 
FROM Search.dbo.Invoice_Detail  WHERE Invoice_Detail.Invoice_date = '2018-04-29' 
Group by Invoice_Detail.Invoice_date



'Second Query
SQL
SELECT  Cast(Search.dbo.Invoice_Detail.Invoice_date as Date) Date, cast(Search.dbo.Invoice_Master.Login_User_name as NCHAR) User_Name, 
Sum(Search.dbo.Invoice_Detail.Product_total) Total 
FROM Search.dbo.Invoice_Master INNER JOIN Search.dbo.Invoice_Detail 
ON Invoice_Master.Invoice_date=Invoice_Detail.Invoice_date  
WHERE Invoice_Detail.Invoice_date = '2018-04-29' 
Group by Invoice_Detail.Invoice_date,Invoice_Master.Login_User_name
Posted
Updated 1-May-18 3:39am
v4

1 solution

At the moment you have joined the tables based on a date column
SQL
FROM Search.dbo.Invoice_Master INNER JOIN Search.dbo.Invoice_Detail 
ON Invoice_Master.Invoice_date=Invoice_Detail.Invoice_date  

I would guess that this is not the actual joining column between the tables. Instead probably multiple invoices can have the same date.

Investigate the schema and check what column in Invoice_Detail is the foreign key to Invoice_Master and use that when joining the tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900