| Author |
Topic |
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-15 : 19:58:24
|
| I am working in vb express 2008.I want to combine two tables to show all the columns of two tables in one table. I tried following query:"Select de.Description, SUM(de.Quantity) AS Quantity, SUM(de.Amount) AS Amount " & _ "FROM DailyExpenses de " & _ "WHERE de.EDate Between @START and @END " & _ "GROUP BY de.Description " & _ "UNION ALL " & _ "Select dt.Description, SUM(dt.Quantity) AS Quantity, SUM(dt.Amount) AS Amount " & _ "FROM DailyTransaction dt " & _ "WHERE dt.DTDate Between @START and @END " & _ "GROUP BY dt.Description "this query is working but it is adding the records of two tables ; first it is showing the records of DailyExpenses and then displaying the records of DailyTransaction, horizontally.My requirement is:Table DailyTransaction has three columns and Table DailyExpenses also has three columns. The query should just show six columns and data of DailyTransaction should be in first three columns and the data of DailyExpenses should be in the next three columns.I hope i have been able to clear my requirement.Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-15 : 20:53:34
|
| Table DailyTransactionDescription Quantity AmountCoca Cola 2 25Sprite 3 15Beaf 4 50 Table DailyExpensesDescription Quantity AmountMineral Water 12 26Rice 3 215Beaf 4 150 And i want to combine two tables to display the following result into a Listview through a SQL query:Description Quantity AmountDescription Quantity AmountMineral Water 12 26 Coca Cola 2 25Rice 3 215 Sprite 3 15Beaf 4 150 Beaf 4 50 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-16 : 05:15:33
|
| Infact my intention to combine two tables is that I have two tables DailyTransaction(IncomeTable) and DailyExpenses(ExpenditureTable). I want to display income and expenditure in a single table and at the end of table i will calculate the NetIncome or NetLoss.so I just want to display incometable columns and expendituretable columns in a single table to create a report for Netincome or Netloss.Thanks. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-16 : 05:28:39
|
quote: Originally posted by sazd1 Infact my intention to combine two tables is that I have two tables DailyTransaction(IncomeTable) and DailyExpenses(ExpenditureTable). I want to display income and expenditure in a single table and at the end of table i will calculate the NetIncome or NetLoss.so I just want to display incometable columns and expendituretable columns in a single table to create a report for Netincome or Netloss.Thanks.
Thats OK,Whats is the relation between two tables??Any common Column?Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-16 : 05:34:19
|
HiCREATE TABLE IncomeTable(ID INT IDENTITY(1,1) PRIMARY KEY,JOIN1 VARCHAR(10))CREATE TABLE ExpenditureTable(ID INT REFERENCES JOIN1(ID),JOIN2 VARCHAR(10))INSERT INTO IncomeTableSELECT 'A' UNION ALLSELECT 'B' UNION ALLSELECT 'C' UNION ALLSELECT 'D' UNION ALLSELECT 'E' UNION ALLSELECT 'F' INSERT INTO ExpenditureTableSELECT 1,'A' UNION ALLSELECT 2,'B' UNION ALLSELECT 4,'D' SELECT *FROM IncomeTable A INNER JOIN ExpenditureTable BON A.ID = B.ID -- Like this--DROP TABLE IncomeTable--DROP TABLE ExpenditureTableSo you need to join both the tables using common field. -------------------------R... |
 |
|
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-16 : 05:47:57
|
| I have a common field ItemId in both the tables.I would like to explain my requirement again:Table1(Column1,Column2,Column3)Table2(Column4,Column5,Column6)My intention is to create a report displaying all the records of two tables with total of six columns in followint format:Column1,Column2,Column3,Column4,Column5,Column6Please advise. |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-12-16 : 05:50:39
|
| Then you must include the column ItemId in both table.Select t1.column1, t1.column2, t1.column3,t2.column4, t2.column5, t2.column6 from Table1 t1 inner join Table2 t2 ont1.ItemId =t2.ItemIdSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
rajdaksha
Aged Yak Warrior
595 Posts |
Posted - 2009-12-16 : 05:52:00
|
Hi sazd1Did you understand my previous OPTry thisSELECT A.Column1,A.Column2,A.Column3,B.Column4,B.Column5,B.Column6FROM IncomeTable A INNER JOIN ExpenditureTable BON A.ItemId = B.ItemId -------------------------R... |
 |
|
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-16 : 05:59:11
|
| Thanks RajdakshaI will try your suggestions and will let you know the results. |
 |
|
|
sazd1
Starting Member
6 Posts |
Posted - 2009-12-18 : 20:24:23
|
I am using MsAccess as my database. i appreciate your response and guidance for me. Join is not giving the results and it is missing many records and duplicating many records as well.I tried following query and it worked for me.Dim cmdText As String = "Select EDescription, SUM(EQuantity) AS EQuantity, " & _ "SUM(EAmount) AS EAmount, TDescription,SUM(TQuantity) AS TQuantity, SUM(TAmount) AS TAmount " & _ "From " & _ "(" & _ "SELECT de.Description AS EDescription, de.Quantity AS EQuantity, de.Amount AS EAmount," & _ "0 AS TDescription, 0 AS TQuantity,0 AS TAmount FROM DailyExpenses de " & _ "WHERE de.EDate Between @START and @END " & _ "UNION ALL " & _ "SELECT 0 AS EDescription, 0 AS EQuantity, 0 AS EAmount, " & _ "dt.Description AS TDescription,dt.Quantity AS TQuantity,dt.Amount AS TAmount FROM DailyTransaction dt " & _ "WHERE dt.DTDate Between @START and @END " & _ ") a " & _ "GROUP BY EDescription,TDescription"this query has one problem for me that it is showing data as under:EDescription EQuantity EAmount TDescription TQuantity TAmount0 0 0 cocacola 2 60 0 0 Sprite 3 120 0 0 Pepsi 4 15Rent 1 1500 0 0 0Beaf 10 50 0 0 0Conveyance 1 10 0 0 0How I can amend this query to avoid these zeros and to get the result as under:EDescription EQuantity EAmount TDescription TQuantity TAmountRent 1 1500 cocacola 2 6Beaf 10 50 Sprite 3 12Conveyance 1 10 Pepsi 4 15Thanks |
 |
|
|
|