Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello,

i'm developing a windows application where it will convert textfile to sql compact database.this converted data from textfile will be stored in table 1.
table 1 have 2 colums,itemcode and qty:
itemcode qty
111111 3
111112 2
111113 4

and i have table 2 with 4 colums ,location,itemcode,snnumber,qty;
location itemcode snnumber qty
LG , 111111 566655 1
LG , 111111 565656 1
LG , 111111 565656 1

means in table 2,i have qty with all fixed to 1.lets say got 3 same items,i will store as 3 different items.but in table 1,it will show the quantity as 3.

so,now i want to compare these two tables,so that itemcode in table 1 exists in table2,and vice versa..and i want a produce a report in csv or excel file, the variance,and the quantity.
is this possible?if yes,how?can anyone guide me..
Posted

Not really understand your question. It looks like you can fill table 1 with info from table 2 using a SQL group by construct like:

select itemcode, sum(quantity)
from table2
group by itemcode

what is exaclty your question?
 
Share this answer
 
If I understand your question, you will need a query that utilizes sub-queries and inner join to generate the appropriate resulting table. I think it would be something similar to this:

SQL
SELECT 
    tbl1.itemcode, 
    tbl1.T1Quantity, 
    tbl2.T2Quantity, 
    (tbl1.T1Quantity - tbl2.T2Quantity) AS Variance 
FROM 
    ( SELECT 
          itemcode, 
          qty AS T1Quantity 
      FROM 
          table1
    ) AS tbl1 INNER JOIN 
    ( SELECT 
          itemcode, 
          SUM(qty) AS T2Quantity 
      FROM 
          table2 
      GROUP BY 
          itemcode
    ) AS tbl2 ON tbl1.itemcode = tbl2.itemcode


[EDIT: version without subqueries in FROM clause]

SQL
SELECT
    Table1.itemcode, 
    Table1.qty AS T1Quantity, 
    SUM(Table2.qty) AS T2Quantity, 
    Table1.qty - SUM(Table2.qty) AS Variance
FROM
    Table1 INNER JOIN
    Table2 ON Table1.itemcode = Table2.itemcode
GROUP BY 
    Table1.itemcode, 
    Table1.qty
 
Share this answer
 
v2
Comments
pwtc222 25-Mar-11 6:36am    
it seems like i'm getting error when i execute your code :
The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = SUM ]

is it that the code doesnt support sql compact version.
JOAT-MON 25-Mar-11 6:55am    
What version of SQL CE are you using?
pwtc222 26-Mar-11 12:37pm    
sql compact 3.5
JOAT-MON 27-Mar-11 22:53pm    
- Did you see my [EDIT] in the answer above? Does changing the select to get rid of subqueries work?
- The SUM() function should be supported in that version.

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