combine two tables with different columns in sql sever


I have a requirement like..

One table has 12 columns for all month  with different year.

 Lets say i need values from sep-2008 to Mar-2009.

I need to select the values from sep-2008 to dec-2008 as a single table and jan-2009 to Mar-2009 values as a single table.

Now i need to combine these two table as a single table.

Please help how to combine two tables with different columns.



10/31/2008 10:41:07 AM 29906 articles. 0 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 13

 The question is unclear (put samples of exact data structure, test data and desired output). I think you need to look at UNPIVOT operator in SQL Server 2005. Look at the Help for this operator in BOL.

Beware of bugs in the above code; I have only proved it correct, not tried it.
(Donald Knuth)
10/31/2008 10:52:36 AM

 combine them by using union operator.

suppose table1 has a column col1  and table2 has a column col2 then create dummy columns for them and then combine.

select col1, null as col2  from table1


select null as col1, col2 from table2

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
10/31/2008 10:53:03 AM

Similar Artilces:

How to Combine two column in one table using SQL statement ?
Could you write the simple SQL statement from 'Combine two column in one table '? I try to use 'Union' which combine two column in two table . thx You can use the concatention operator (+): SELECT column1 + column2 AS myColumn FROM myTable TerriTerri MortonEngagement Manager, NeudesicHow to ask a question If the columns are numeric then "+" will sum the column values. If you do not desire this, you should use str() function Check the below statement, select ltrim(str(column1)) + ltrim(str(column2)) from mytable http://www.eral...

what's difference between Accesss DataBase and Sql Sever about Sql String(or Command),Sql StoreProcedure ?
i am being dirty to Access DataBase from Sql Sever Database for my new work.But the sql string of Access sounds like a little different,can i use  the sql string of SqlSever for Access database,too?another question is: Does Access DataBase support Sql StoreProcedure??? when i worked in Access2000 ,did not see any about StoreProcedure,but i download a file named AccessHelper.cs  , some function of it looks like support StoreProcedure,Does it?  i want to fly ... Hi dkzeng ,Your first question     The string of both sqlsever and acce...

SQL Server Table's column Referencing two parent tables.
Hi everybody,  I know that it is possible for one column to reference two different parent tables by defining two foreign keys on a same column, I have done it and SQL Server does give any error. But I want to know that is it advisable to define multiple foreign key on a column referncing two differnt parent table's primary key in differnt case... ? Means If Case 1 then It should reference to column1 or table1 otherwise it should reference to table2? How is it practicle...?    Please, 'Mark as Answer' if this post has helped you! - Bihag Thakerhttp://bihagt...

Combine different column (from different tables) into 1 table
Hi, I have below 3 query: Select Name, Count(C1) as A from Table1 Select Name, Count(C2) as B from Table2 Select Name, Count(C3) as C from Table3  Where Name from 3 tables will be the same on every returned row. Now, I have anotehr table named tblCombine which has columns (Name, A, B, C). I need to put all above data into tblCombine as below: Amy    5      7       10 Jo       14     27      5 Bob&n...

I imported a SQL Table into SQL DataBase, But I can not update this table even with SQL Server management Studio
I imported a SQL Table into SQL DataBase, But I can not update this table even with SQL Server management Studio When I change any data on mentioned table above, Red exclamation sign appears left of the record . How can I correct this problem?  Thanks. Try running the UPDATE via Query Analyzer...and see if you get an error.***********************Dinakar NethiLife is short. Enjoy it.***********************...

How to combine two columns into one in an sql
I have an stored procedure that returns 3 columns. Month, Date, and Total Number of Calls. Here is the stored Proc: SELECT DATEPART(mm, CALLSTARTTIME) ,  DATEPART(dd, CALLSTARTTIME), COUNT(*) FROM CALL_LOG_MASTER WHERE (COMMERCIALS = '1') AND (PINCODE IS NOT NULL) GROUP BY DATEPART(mm, CALLSTARTTIME), DATEPART(dd, CALLSTARTTIME) ORDER BY DATEPART(mm, CALLSTARTTIME), DATEPART(dd, CALLSTARTTIME) It returns a table: MONTH   DATE   TOTAL NUMBER OF CALLS=======   =====   ===========1      ...

Strange difference between SQL from .Net and SQL within stored proc
Hi, I am trying to fill a DataGrid with random records from my database. It thought I had found a way to randomly select records using SQL. So I built a stored procedure in Access and sure enough, it returns a random set of records. Whe I tried to call this from my app, it kept returning the same set of records. So I copied and pasted the SQL into the app (code below) but it made no difference. Could anyone tell me why this wouldn't work? <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %><%@ Import Namespace="System.Data" %><%@ Import Namespace="...

Merge two sql statements with derived table Sql Server 2005
I have two select statements that I need to merge into one record. I have a personcontact table that contains contact information and insurance information. (bad db design). below is what I have created so far based on what I have learned from Google searches but I get errors and I am not sure why   ThanksMsg 156, Level 15, State 1, Line 18 Incorrect syntax near the keyword 'Left'. Msg 156, Level 15, State 1, Line 29 Incorrect syntax near the keyword 'as'. select Top 1 pc.firstname + ' ' + pc.lastname as ContactName , A.Address1, A.Address2, A.cit...

How to combine two SQL Datasource and put it in GridView?
Hi, I'm trying to combine two datasources together. The reason I have to combine them is because, one database is used by a software that I can't manipulate, the other database is exact one I created for an internal custom software. They both have different connectionstrings. I have the VB code below. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)Dim PCConnectionString As String = "Data Source=Source1;Initial Catalog=Center;Persist Security Info=True"Dim PCcon As SqlConnection = New SqlConnection(PCConnectionString)Dim TCConnectio...

Totaling Different Table Columns within SQL
Hi, I have 3 tables which are part of my query and I need to total one column from each of them, but cannot get it to work. I had this working within Access using the nz function (below), but when trying to use this within VS to produce my code did not work, saying NZ was not a valid function. nz(HW.HGF)+nz(HD.HGF)+nz(HL.HGF) AS FWhere HW,HD,HL are the table names and HGF is the same column name in each which I want to total.Any help would be grately appreciated.Thanks   Have you tried this one in your query:(HW.HGF + HD.HGF + HL.HGF) AS FLimno Hi, I think I have it w...

Why these two SQL statement return differently? using ROUND() function in Transact-SQL
Hello, DECLARE @x DECIMAL SET @x = 65.554 SELECT ROUND(@x, 1) --this returns 66 SELECT ROUND(65.554, 1) --this returns 65.600 can someone explain to me why is like that?  Thank you   Always specify the size for the parameters. For example: Decimal(10,3). ***********************Dinakar NethiLife is short. Enjoy it.*********************** DECLARE @x DECIMAL(10,3) SET @x = 65.554 SELECT ROUND(@x,1) SELECT ROUND(65.554,1) both return the same answer. ndinakar & Motley Thank you two very much.  I got it.  :)...

difference between T-Sql and Sql,
Hi guys, I am very new to Sybase, can some one tell me the differences between T-Sql and Sql. Thanks in advance, Munish. I believe you are referring to: SQL--The standard SQL syntax used universally for most DBMS's T-SQL--Transact-SQL, Sybase's flavor of SQL the is used for writing stored proc's, etc. I supported variable declarations, functions and data-type conversion. -Dan ...

How to get data from two different tables for two different columns of a gridview based on datakey
Hi All,            I have a Gridview with two different columns- "Faculty" coming from tblFaculty and "Teachers" coming from tblFacultyTeacherLink. I want to get data for "Teachers" by comparing FcaultyId in tblFacultyTeacherLink and then getting Teachers name. How to achieve this without using nested gridviews?  My .aspx Page is as-   <asp:GridView ID="gviewFaculty" DataKeyNames="FacultyId" AutoGenerateColumns="false" runat="server" Width="100%&q...

Insert into Access from two different sql database tables
Hi there, I am looking for a strategy on how to insert records into an MS Access table based on records from two different tables in different sql servers. Currently the solution uses a query stored in MS Access and implements linked tables to get data from the two sql tables. It works in Access if I manually enter the passwords to the SQL databases. When I tried to automate this from an page the sql connections failed. I edited the linked tables in Access so they store the password and that worked. Now if I manually run the query in Access it doesn't prompt for ...

Web resources about - combine two tables with different columns in sql sever -

U.S. Sends $1.7 Billion to Iran
John Kerry has announced that the U.S. has made a $1.7 billion payment directly to Iran. Here's the secretary of state's statement: The United ...

Foreigners among 23 killed as al-Qaeda militants storm Burkina Faso hotel
The Islamic extremists stormed the hotel and a nearby cafe Friday night.

The NFL playoffs continue and you can watch games on Roku
The NFL playoff games continue this weekend with some big contests including some of the hot teams. Those games require viewers to have cable ...

‘Saturday Night Live’ Returns With Jabs At GOP Contenders’ Latest Debate
Adam Driver hosted the season return of Saturday Night Live , which opened with a parody of the latest GOP presidential hopefuls’ debate, as ...

Pro-China party likely to lose power in Taiwan election
Tsai supporters appeared confident that ties with China would weather a change in government.

Noah Chamberlin Search Update: Police Send Home Volunteers, Bring In Law Enforcement From Across State ...
The search for Noah Chamberlin entered its third day on Sunday, but police in Tennessee remained confident that they could find the 2-year-old ...

Netflix Unveils Premiere Dates For ‘Orange Is The New Black,’ ‘The Get Down,’ ‘Flaked’ And Others
Netflix today announced premiere dates for a roster of upcoming series – including Flaked , The Ranch and Lost & Found Music Studios , among ...

Celine Dion's brother dies of cancer only days after her husband
Two days after Céline Dion's husband René Angélil died, it has emerged that her brother, Daniel Dion, also lost his battle with cancer. In a ...

Judd Apatow Netflix Comedy Series Releases New ‘Love’ Trailer
Netflix this morning unveiled its new trailer for Love , created, written and executive produced by Judd Apatow , Paul Rust and Lesley Arfin. ...

Resources last updated: 1/17/2016 5:29:02 PM