Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Combine columns of two tables

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

Posted - 2009-12-15 : 20:10:47
Please show us a data example of what it is currently displaying and what you want it to show.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

sazd1
Starting Member

6 Posts

Posted - 2009-12-15 : 20:53:34
Table DailyTransaction
Description Quantity Amount
Coca Cola 2 25
Sprite 3 15
Beaf 4 50

Table DailyExpenses
Description Quantity Amount
Mineral Water 12 26
Rice 3 215
Beaf 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 Amount
Mineral Water 12 26 Coca Cola 2 25
Rice 3 215 Sprite 3 15
Beaf 4 150 Beaf 4 50


Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-15 : 22:16:34
You need to use a JOIN instead of a UNION, but I don't see how you'd relate the two tables together. How do you know that Mineral Water goes with Coca Cola?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

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.
Go to Top of Page

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 canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-16 : 05:34:19
Hi

CREATE 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 IncomeTable
SELECT 'A' UNION ALL
SELECT 'B' UNION ALL
SELECT 'C' UNION ALL
SELECT 'D' UNION ALL
SELECT 'E' UNION ALL
SELECT 'F'


INSERT INTO ExpenditureTable
SELECT 1,'A' UNION ALL
SELECT 2,'B' UNION ALL
SELECT 4,'D'


SELECT *
FROM IncomeTable A
INNER JOIN
ExpenditureTable B
ON A.ID = B.ID -- Like this


--DROP TABLE IncomeTable
--DROP TABLE ExpenditureTable

So you need to join both the tables using common field.


-------------------------
R...
Go to Top of Page

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,Column6

Please advise.
Go to Top of Page

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 on
t1.ItemId =t2.ItemId

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

rajdaksha
Aged Yak Warrior

595 Posts

Posted - 2009-12-16 : 05:52:00
Hi sazd1

Did you understand my previous OP

Try this

SELECT A.Column1,A.Column2,A.Column3,B.Column4,B.Column5,B.Column6
FROM IncomeTable A
INNER JOIN
ExpenditureTable B
ON A.ItemId = B.ItemId


-------------------------
R...
Go to Top of Page

sazd1
Starting Member

6 Posts

Posted - 2009-12-16 : 05:59:11
Thanks Rajdaksha
I will try your suggestions and will let you know the results.
Go to Top of Page

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 TAmount

0 0 0 cocacola 2 6

0 0 0 Sprite 3 12

0 0 0 Pepsi 4 15

Rent 1 1500 0 0 0

Beaf 10 50 0 0 0

Conveyance 1 10 0 0 0

How I can amend this query to avoid these zeros and to get the result as under:

EDescription EQuantity EAmount TDescription TQuantity TAmount

Rent 1 1500 cocacola 2 6

Beaf 10 50 Sprite 3 12

Conveyance 1 10 Pepsi 4 15

Thanks
Go to Top of Page
   

- Advertisement -