Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


T-SQL Help Needed


T-SQL Help Needed

Author
Message
a4apple
a4apple
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 406
CREATE TABLE Issue
(
id int not null identity(1,1) primary key,
name varchar(10)
)

CREATE TABLE Jrn
(
id int not null identity(1,1) Primary Key,
issue_id int,
created_dt datetime
)

ALTER TABLE jrn
ADD Foreign Key (issue_id) References Issue(id)


CREATE TABLE details
(
id int not null identity(1,1) primary key,
jrn_id int,
prop_key varchar(10),
old varchar(10),
new varchar(10)
)

ALTER TABLE details
ADD Foreign Key (jrn_id) References jrn(id)

Create table Status
(
id int not null identity(1,1) primary key,
name varchar(20)
)

Create table users
(
id int not null identity(1,1) primary key,
name varchar(20)
)


INSERT INTO issue(name) values('Issue1')
INSERT INTO issue(name) values('Issue2')
INSERT INTO issue(name) values('Issue3')


Insert into jrn(issue_id, created_dt) values(1, '04/01/2012')
Insert into jrn(issue_id, created_dt) values(1, '04/22/2012')
Insert into jrn(issue_id, created_dt) values(1, '04/28/2012')
Insert into jrn(issue_id, created_dt) values(2, '04/01/2012')
Insert into jrn(issue_id, created_dt) values(2, '04/23/2012')

insert into status(name) values('New')
insert into status(name) values('Assign')
insert into status(name) values('Inprogress')
insert into status(name) values('Complete')

Insert into users(name) values('Alex')
Insert into users(name) values('Sophia')

Insert into details(jrn_id, prop_key, old, new) values(1, 'status_id', 1, 2)
Insert into details(jrn_id, prop_key, old, new) values(2, 'user_id', NULL, 2)
Insert into details(jrn_id, prop_key, old, new) values(2, 'status_id', 2, 3)
Insert into details(jrn_id, prop_key, old, new) values(3, 'status_id', 3, 4)
Insert into details(jrn_id, prop_key, old, new) values(4, 'status_id', 1, 2)
Insert into details(jrn_id, prop_key, old, new) values(5, 'status_id', 2, 3)

This is my table structure and I have data like this.

MY Final Result Should look like this

SELECT 1 as IssueID, 'Complete' as 'Status', '2012-04-01' as Created_date,'2012-04-01' as Assigned_Date, '2012-04-22' as InprogressDate, '2012-04-28' as 'Completed Date', 'Sophia' as [User]
UNION
SELECT 2, 'Inprogress', '2012-04-01', '2012-04-01', '2012-04-23', NULL, NULL


Thanks in advance

Good Luck Smile .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45022 Visits: 39887
The test data setup you provided is great. Could you also describe what you want done with it? The result set doesn' give many hints as to what you're trying to do. Thanks

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Jeff Moden (9/10/2012)
The test data setup you provided is great. Could you also describe what you want done with it? The result set doesn' give many hints as to what you're trying to do. Thanks


Jeff, I think this post is the same as this one... http://www.sqlservercentral.com/Forums/Topic1354775-392-1.aspx

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
a4apple
a4apple
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 406
Jeff Moden (9/10/2012)
The test data setup you provided is great. Could you also describe what you want done with it? The result set doesn' give many hints as to what you're trying to do. Thanks


Hi Jeff,

By using my data in these tables, I want to create a final destination table that would look like the Select Statement I have used at the end.

Basically it should capture the changes in data based on the details table but the dates come from created_dt field from Jrn Table based on status change. which means Accepted Date column should be populated only if the details table has an entry for that change otherwise NULL should be populated.

I hope you got my question.

Good Luck Smile .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
CELKO (9/10/2012)
We do not use IDENTITY for a key in RDBMS. Why do you think that the count of physical insertion attempts to one disk on one machine is an attribute of an entity in a valid data model? You ave a magical Kabbalah number called “id” that can be an issue, a squid, Lady Gaga, etc. That is magic and not RDBMS.


Most of experts here do use IDENTITY for a primary keys in SQL Server database where it's appropriate.
Mr. Celko doesn't, but it entirely his personal problem. It does look like he is afraid of Lady G and have some issues with Kabbalah magics Hehe



Why do the data elements change names from table to table? Status is not an entity; it is an attribute property, such as “martial_status” “employment_status”, etc. An SQL programmer would have a column something lie this ...


It depends. Sometime it's good enough to have it as attribute. Sometimes you want to denormalize it into own entity.
I use the following approach when making a decision for status:
For simple cases like 2-4 different, pretty constant statuses (usually having more technical meaning than business, but still required some text description) I use simple table attribute with CHECK. Examples:
Record status (technical) : Open, Closed
Progress status: Not-started, Started, Completed, Failed
For cases which require more flexibility, eg. the probability of status description changed or new state added, I will have an entity:
Marital status: Single, Married, Divorced, Widowed (new statuses added recently: Partnership, Friendship, Life-enemies)
Job status: Employed, Not-employed, Self-Employed. - Here business may want to add some more statuses any time eg. Split Employed to Employed Full-time and Employed Part-time, add Temporary Employed, Never want to be Employed, Not-employable.Hehe

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
a4apple
a4apple
SSC-Enthusiastic
SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)SSC-Enthusiastic (170 reputation)

Group: General Forum Members
Points: 170 Visits: 406
Hi, I need to get status names as columns and their respective dates as rows. I know its pivoting, but asking you guys for more insight.

Thanks

Good Luck Smile .. Visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
a4apple (9/11/2012)
Hi, I need to get status names as columns and their respective dates as rows. I know its pivoting, but asking you guys for more insight.

Thanks


I think you need to do a little more than that, namely fully understand the data. For example, your expected output includes a column for 'Created_date', yet you do not provide a status of 'created'. Are you going to make an inference on this value based on it's existing in a table somewhere? Also, what happens if a user_id changes? Anyhow, this will give you your desired output, with the exception of the created_date, as I'll leave that for you to decide how you want to handle. I can think of about 10 ways this will fail depending on your real data, but it should be at least enough to get you started.

WITH Pvt AS
(
SELECT
issue_id,[created] ,[assign],[Inprogress],[Complete]
FROM
(
SELECT
a.issue_id,
a.created_dt,
f.name AS status_new
FROM jrn a INNER JOIN details b
ON a.id = b.jrn_id
CROSS APPLY
(
SELECT name FROM status f
WHERE id = b.new AND b.prop_key = 'status_id'
) f
) t1
PIVOT
(
MIN(created_dt) FOR status_new IN ([created],[assign],[Inprogress],[Complete])
) AS pvt
)
SELECT
a.issue_id,
c.name AS [Status],
a.created AS Created_dt,
a.assign AS Assign_dt,
a.inprogress AS Inprogress_dt,
a.Complete AS Complete_dt,
b.[user]
FROM PVT a LEFT OUTER JOIN
(
SELECT
a.issue_id,
c.name AS [USER]
FROM jrn a INNER JOIN details b
ON b.jrn_id = a.id INNER JOIN users c
ON b.new = c.id
WHERE b.prop_key = 'user_id'
) b
ON a.issue_id = b.issue_id LEFT OUTER JOIN
(
SELECT
a.ISSUE_ID,
b.name
FROM
(
SELECT
a.issue_id,
MAX(b.new) AS status_id
FROM jrn a INNER JOIN details b
ON b.jrn_id = a.id
GROUP BY a.issue_id
) a INNER JOIN status b
ON a.status_id = b.id
) c
ON a.issue_id = c.issue_id



Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
Also...have...to...(fighting it)...agree...with...Celko. The 'id' column in every table was a bit annoying to work with. If you have any ability to provide some input with respect to column names, you might want to give more descriptive names for the id columns.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18115
Have you tried this article? It might help you with what you're trying to accomplish.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search