This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 7 years ago.
below is mine table name: sample1
ID NAME ADDRESS
1 DAN NO.10,CHANGJIANG XI STREET,JIANXI DISTRECT ,LUOYANG CITY,HENAN ,CHINA
2 SAM BINALBAGAN NEGROS OCCIDENTAL PHILIPPINES
3 JOSE B-36 L-40 PH-1 ST. JOSEPH VILLAGE 7, MARINIG CABUYAO LAGUNA, 4025
i need to enter the rows to column, but here the challenges is, in sample 2 i have only 4 columns source rows obtained 6 or more while using split function, how can i insert/ append balance data in ADRS4 after inserting datas in columns.
output should be as follows:
ID NAME ADRS1 ADRS2 ADRS3 ADRS4
1 DAN NO.10 NGJIANG XI STREET JIANXI DISTRECT LUOYANG CITY,HENAN ,CHINA
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
Where you want to skip a value simply input a null value. So you query should look something like this:
INSERT INTO table_name (name,adrs1,adres2,...)
VALUES (John,Null,Xi Street,...);
Your SQL design is not that great to begin with IMO but that should work with your current setup. I would create a table that has multiple columns then insert as needed, like this:
CREATE TABLE [dbo].[Table_1](
[id] [int] NULL,
[name] [varchar](50) NULL,
[ADRS1] [varchar](50) NULL,
[ADRS2] [varchar](50) NULL,
[ADRS3] [varchar](50) NULL,
[ADRS4] [varchar](50) NULL
) ON [PRIMARY]
Related
This question already has answers here:
SCOPE_IDENTITY() for GUIDs?
(5 answers)
Closed 7 years ago.
Goal:
Retrieve the latest guid value in real time after you have inserted the the value in the table
Problem:
Don't know how to do it
Info:
*You can only add a new vale that is address and zip code
*Please take account to that it can be lots of data!
CREATE TABLE [AddressBook]
(
[testID] [uniqueidentifier] NOT NULL default newid(),
[address] [nvarchar](50) NULL,
[zipcode] [nvarchar](50) NULL
)
You can retrieve the inserted GUID through the Insert Stored Procedure like this sample:
CREATE PROCEDURE [dbo].[spUpdateBookmark]
#BookmarkGuid [uniqueidentifier] = null,
#otherfield nvarchar(256),
#NewBookmarkGuid [uniqueidentifier] OUTPUT
WITH EXECUTE AS CALLER
AS
BEGIN
SET #BookmarkGuid = NewID()
INSERT INTO Bookmarks
(BookmarkGuid,
otherfield)
Values
(#BookmarkGuid,
#otherfield)
SET #NewBookmarkGuid = #BookmarkGuid
END
Then when you call the procedure you add a parameter for output value.
QUESTION INFO
Detailed Question
The best way I can explain my question is to explain my desired outcome. I'm trying to take a certain set of offices, insert its data into the dbo.DeliveryLocation table, then take the output inserted.DeliveryLocationId and update the corresponding office's DeliveryLocationId field with that id.
Desired Outcome Example
Office Data Before
OfficeId | DeliveryLocationId
-----------------------------
1 | null
2 | null
3 | null
Run the SQL statement
Office Data After
OfficeId | DeliveryLocationId
-----------------------------
1 | 5
2 | 6
3 | 7
Delivery Location with the DeliveryLocationId of 5 was created with
the data of the Office with OfficeId of 1
Delivery Location with the DeliveryLocationId of 6 was created with
the data of the Office with OfficeId of 2
Delivery Location with the DeliveryLocationId of 7 was created with
the data of the Office with OfficeId of 3
The problem
Per my current SQL script below, you can see that I have the first part (inserting the Office data into the Delivery Location table) complete. The second part (updating the Office with the corresponding DeliveryLocationId of the created Delivery Location) is not complete, and I am unsure how to go about doing that.
My initial thoughts/ solutions
If there would be a way to store the correlated OfficeId and DeliveryLocationId, perhaps we could loop through them and update the offices in a second SQL statement rather than try to create one SQL statement that does everything.
REFERENCES
dbo.DeliveryLocation
[DeliveryLocationId] [int] IDENTITY(1,1) NOT NULL,
[LocationName] [nvarchar](max) NULL,
[ShortName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[DropoffInstruction] [nvarchar](max) NULL,
[PickupInstruction] [nvarchar](max) NULL,
[TaxRate] [decimal](18, 2) NOT NULL,
[Active] [bit] NOT NULL,
[DisableOffices] [bit] NOT NULL
dbo.Office
[OfficeId] [int] IDENTITY(1,1) NOT NULL,
[OfficeName] [nvarchar](max) NULL,
[ValidatedAddressId] [int] NOT NULL,
[ReferralSource] [nvarchar](max) NOT NULL,
[NumberOfEmployees] [int] NOT NULL,
[DeliveryLocationId] [int] NULL
Current SQL
insert into
dbo.DeliveryLocation
(LocationName, ShortName, ValidatedAddressId, Active, DisableOffices)
output
inserted.DeliveryLocationId
select
OfficeName, OfficeName, ValidatedAddressId, 0, 0
from
dbo.Office as o
where
OfficeId in
(
select distinct
OfficeId
from
dbo.[User] as u
where
u.DeliveryLocationId is null
and
u.OfficeId is not null
)
I'm not sure about doing this in an INSERT statement, but if you use a MERGE statement using Office (or a query based on Office) as the source, you'll be able to refer to source.OfficeId as well as inserted.DeliveryLocationId in the OUTPUT clause. You can simply skip the update and delete usage of the MERGE, and only use the ON NOT MATCHED clause.
When I'm doing things like this I put the output into a temp table, then carry out any further updates or inserts I need to do from there.
In case you've not used the MERGE statement before (or even for anyone who just hasn't used all of their capabilities), this is a really fantastic resource on how to use them, and how to use them well: http://www.made2mentor.com/2012/07/got-the-urge-to-merge/
You could do an update join after you insert into delivery location
update dbo.Office
set o.DeliveryLocationID = dl.DeliveryocationID
from Office o
JOIN DeliveryLocation dl on dl.LocationName = o.OfficeName
Assuming that the Office Names are unique. If they are not you may want to add OfficeID to the DeliveryLocations table, at least temporarily, and join on that.
I have created a table in there is data in the table,
I can insert, update ect.
CREATE TABLE [dbo].[cse_reports_month](
[report_id] [int] IDENTITY(1,1) NOT NULL,
[starburst_dept_name] [varchar](50) NULL,
....more collumns
[shooting_total_stars] [float] NULL,
) ON [PRIMARY]
but for some reason when I hover over the columns name, for example
select the top 1000 rows :
SELECT TOP 1000 [report_id]
,[starburst_dept_name]
,[starburst_dept_average]
...... more columns
,[rising_star_dept_name]
FROM [Intranet].[dbo].[cse_reports_month]
It says "invalid column name 'starburst_dept_name'"
It like it does not requinize it, but all my other tables are
good.
Wondering why it does not recognize the columns?
I have
Microsoft SQL Server Management Studio 10.0.2531.0
Hope you all enjoying holidays and fun time. Reply to this when you get a chance.
I'm using SQL Server 2005.
The issues is - there is a process in one of our teams which do things in sequence of events/actions. Mostly it is 4 actions/events for every sequence. These events are fixed. When they perform this DWH gets an entry for each event as a separate row.
Example
1) Call Customer
2) Sell Insurance
3) Send PDS
4) Send details to product team
I've it set in a way that all Actions and their definitions are in Dimension_Point_Code table. All events that come through from DWH are treated as Fact and stored in Fact_Point
The Point here refers to the Point in the sequence of the process.
So the table that store this info looks like below
Dimension table
CREATE TABLE [tbl_Dim_Point_Code]
(
[Point_Code_Key] [int] IDENTITY(101,1) NOT NULL,
[Point_Code] [varchar](8) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Point_Code_TouchPoint] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Fact Table
CREATE TABLE [tbl_FACT_Point]
(
[Point_Key] [bigint] IDENTITY(100,1) NOT NULL,
[Point_Code_Key] [int] NOT NULL,
[Customer_Number] [varchar](19) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Sale_Date] [datetime] NULL,
[Rep_ID] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
Data in Dimension
INSERT INTO [tbl_Dim_Point_Code]
([Point_Code]
,[Point_Code_TouchPoint])
Select 'CALC' as Point_Code,'Point1' as Point_Code_TouchPoint
UNION
Select 'SELL' as Point_Code,'Point2' as Point_Code_TouchPoint
UNION
Select 'SPDS' as Point_Code,'Point3' as Point_Code_TouchPoint
UNION
Select 'TPRD' as Point_Code,'Point4' as Point_Code_TouchPoint
Data in Fact
INSERT INTO [tbl_FACT_Point]
([Point_Code_Key]
,[Customer_Number]
,[Sale_Date]
,[Rep_ID])
Select 101,'C101501','24-Feb-2012','ABC'
UNION
Select 102,'C101501','24-Feb-2012','ABC'
UNION
Select 103,'C101501','26-Feb-2012','DEF'
UNION
Select 104,'C101501','27-Feb-2012','XYZ'
UNION
Select 101,'C101502','2-Feb-2012','GHI'
UNION
Select 102,'C101502','2-Feb-2012','GHI'
UNION
Select 104,'C101502','4-Feb-2012','XYZ'
UNION
Select 101,'C101503','14-Feb-2012','ABC'
UNION
Select 103,'C101503','20-Feb-2012','ABC'
UNION
Select 104,'C101503','22-Feb-2012','BBC'
UNION
Select 101,'C101501','24-Oct-2012','ABC'
UNION
Select 102,'C101501','24-Oct-2012','ABC'
UNION
Select 103,'C101501','26-Oct-2012','DEF'
UNION
Select 104,'C101501','27-Oct-2012','XYZ'
Points to note
1) As you can see Customer C101501 was called & sold the twice.
2) And the processing for the all involved happends everyday - like the team, the DWH and the SQL Server Process. Hence most of the times we will not know what is going to happen. We will only know that Event 1 has occured. then few days later event 2 and so on.
3) 101 & 104 are mandatory events. 102 and 103 may or may not occur. The team will contact Product team irrespective of sale or not.
Now - what we want is that the entries to be transposed into this table
CREATE TABLE [tbl_Process1_EVENT]
(
[HS_EVENT_KEY] [int] IDENTITY(101,1) NOT NULL,
[Customer_Number] [varchar](19) ,
[Import_Date] [datetime] NULL,
[Point1_Date_Called] [datetime] NULL,
[Point1_PointCode] [varchar](8) ,
[Point1_Rep_Id] [varchar](50) ,
[Point2_Date_Sold] [datetime] NULL,
[Point2_PointCode] [varchar](8) ,
[Point2_Rep_Id] [varchar](50) ,
[Point3_Date_PDSSent] [datetime] NULL,
[Point3_PointCode] [varchar](8) ,
[Point3_Rep_Id] [varchar](50) ,
[Point4_Date_ProdTeamXfer] [datetime] NULL,
[Point4_PointCode] [varchar](8) ,
[Point4_Rep_Id] [varchar](50)
) ON [PRIMARY]
What I'd want to happen is, an output like this.
Customer_Number Import_Date Point1_Date_Called Point1_PointCode Point1_Rep_Id Point2_Date_Sold Point2_PointCode Point2_Rep_Id Point3_Date_PDSSent Point3_PointCode Point3_Rep_Id Point4_Date_ProdTeamXfer Point4_PointCode Point4_Rep_Id
C101501 28/02/2012 24/02/2012 CALC ABC 24/02/2012 SELL ABC 26/02/2012 SPDS DEF 27/02/2012 TPRD XYZ
C101502 3/02/2012 2/02/2012 CALC GHI 2/02/2012 SELL GHI NULL NULL NULL 4/02/2012 TPRD ABC
C101503 23/02/2012 14/02/2012 CALC ABC NULL NULL NULL 20/02/2012 SPDS ABC 22/02/2012 TPRD BBC
C101501 28/10/2012 24/10/2012 CALC ABC 24/10/2012 SELL ABC 26/10/2012 SPDS DEF 27/10/2012 TPRD XYZ
Transposing the data into rows with new entry for C101501 if new sale is made on same customer.
Import_date is the date when the Row is Updated.
As i said processing happens everyday.
So if I can elaborate this table for line 1 of this output
On 24/02/2012 the Event CALC occurs - we know about this on 25/02/2012 Point 1 & Point 2 on 25/02 will be filled with data and Point 3 & Point 4 will be empty. Import_Date - will be 25/02/2012
On 26/02/2012 PDS is sent which is point 3. So the same row will be updated. Point 3 only will be updated. Import_date will be 27/02/2012
On 27/02/2012 Point 4 event occurs. Hence on 28/02/2012 the same row will be updated with Point 4 details.
That's all I can think about the issue.
Please any help any time will be greatly appreciated.
Njoy holidays and have a good time.
PS: I may have done something wrrong with the format of output. Please advise I can make it better.
I think you need to use the pivot command (aka cross tab). Its not easy to get your head around:
the following links should / could help:
http://geekswithblogs.net/lorint/archive/2006/08/04/87166.aspx
http://msdn.microsoft.com/en-us/library/ms177410%28v=sql.105%29.aspx
http://www.mssqltips.com/sqlservertip/1019/crosstab-queries-using-pivot-in-sql-server/
http://www.simple-talk.com/blogs/2007/09/14/pivots-with-dynamic-columns-in-sql-server-2005/
good lucj
I have the following table structures
CREATE TABLE [dbo].[WorkItem](
[WorkItemId] [int] IDENTITY(1,1) NOT NULL,
[WorkItemTypeId] [int] NOT NULL,
[ActionDate] [datetime] NOT NULL,
[WorkItemStatusId] [int] NOT NULL,
[ClosedDate] [datetime] NULL,
)
CREATE TABLE [dbo].[RequirementWorkItem](
[WorkItemId] [int] NOT NULL,
[RequirementId] [int] NOT NULL,
)
CREATE TABLE #RequirmentWorkItems
(
RequirementId int,
WorkItemTypeId int,
WorkItemStatusId int,
ActionDate datetime
)
I use the #RequirmentWorkItems table to create workitems for requirements. I then need to INSERT the workitems into the WorkItem table and use the identity values from the WorkItem table to create the cross-reference rows in the RequirementWorkItem table.
Is there a way to do this without cursoring thru each row? And I can't put the RequirementId into the WorkItem table because depending on the WorkItemTypeId the WorkItem could be linked to a Requirement or a Notice or an Event.
So there are really 3 xref tables for WorkItems. Or would it be better to put a RequirementId, NoticeId and EventId in the WorkItem table and 1 of the columns would have a value and other 2 would be null? Hopefully all this makes sense. Thanks.
You should read MERGE and OUTPUT – the swiss army knife of T-SQL for more information about this.
Today I stumbled upon a different use for it, returning values using an OUTPUT clause from a table used as the source of data for an insertion. In other words, if I’m inserting from [tableA] into [tableB] then I may want some values from [tableA] after the fact, particularly if [tableB] has an identity. Observe my first attempt using a straight insertion where I am trying to get a field from #source.[id] that is not used in the insertion: