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


Query Help


Query Help

Author
Message
JackTimber
JackTimber
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 61
Hi All Expert,

I need help with the below.Could somebody pls help me with this.


/*
I want to traverse through the loop and find the event before HardDrink.
If no hardDrink then blank.
*/

CREATE TABLE #TEMP (UserName varchar(20),Drink varchar(20),Value int,CreatedDate date)

INSERT INTO #TEMP VALUES ('ABC','Coke',100,'2014-01-12')
INSERT INTO #TEMP VALUES ('ABC','Fanta',50,'2014-01-13')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',50,'2014-01-14')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',75,'2014-01-18')
INSERT INTO #TEMP VALUES ('ABC','Fanta',36,'2014-01-20')
INSERT INTO #TEMP VALUES ('ABC','XXX',45,'2014-01-28')
INSERT INTO #TEMP VALUES ('ABC','XXX',75,'2014-02-04')
INSERT INTO #TEMP VALUES ('ABC','XXX',96,'2014-02-06')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',12,'2014-02-10')
INSERT INTO #TEMP VALUES ('ABC','Fanta',93,'2014-02-22')

INSERT INTO #TEMP VALUES ('ABC','XXX',101,'2014-02-22')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',150,'2014-02-23')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',510,'2014-02-24')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',175,'2014-02-25')
INSERT INTO #TEMP VALUES ('ABC','Fanta',136,'2014-02-26')
INSERT INTO #TEMP VALUES ('ABC','XXX',145,'2014-02-27')
INSERT INTO #TEMP VALUES ('ABC','XXX',175,'2014-02-28')
INSERT INTO #TEMP VALUES ('ABC','XXX',196,'2014-03-01')
INSERT INTO #TEMP VALUES ('ABC','XXX',112,'2014-03-02')
INSERT INTO #TEMP VALUES ('ABC','XXX',193,'2014-03-03')

SELECT * FROM #TEMP

/*
So my output would be like
UserName Drink Value
ABC Pepsi 50
ABC xxx 96
ABC Pepsi 510


*/


Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16562 Visits: 17010
JackTimber (8/28/2014)
Hi All Expert,

I need help with the below.Could somebody pls help me with this.


/*
I want to traverse through the loop and find the event before HardDrink.
If no hardDrink then blank.
*/

CREATE TABLE #TEMP (UserName varchar(20),Drink varchar(20),Value int,CreatedDate date)

INSERT INTO #TEMP VALUES ('ABC','Coke',100,'2014-01-12')
INSERT INTO #TEMP VALUES ('ABC','Fanta',50,'2014-01-13')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',50,'2014-01-14')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',75,'2014-01-18')
INSERT INTO #TEMP VALUES ('ABC','Fanta',36,'2014-01-20')
INSERT INTO #TEMP VALUES ('ABC','XXX',45,'2014-01-28')
INSERT INTO #TEMP VALUES ('ABC','XXX',75,'2014-02-04')
INSERT INTO #TEMP VALUES ('ABC','XXX',96,'2014-02-06')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',12,'2014-02-10')
INSERT INTO #TEMP VALUES ('ABC','Fanta',93,'2014-02-22')

INSERT INTO #TEMP VALUES ('ABC','XXX',101,'2014-02-22')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',150,'2014-02-23')
INSERT INTO #TEMP VALUES ('ABC','Pepsi',510,'2014-02-24')
INSERT INTO #TEMP VALUES ('ABC','HardDrink',175,'2014-02-25')
INSERT INTO #TEMP VALUES ('ABC','Fanta',136,'2014-02-26')
INSERT INTO #TEMP VALUES ('ABC','XXX',145,'2014-02-27')
INSERT INTO #TEMP VALUES ('ABC','XXX',175,'2014-02-28')
INSERT INTO #TEMP VALUES ('ABC','XXX',196,'2014-03-01')
INSERT INTO #TEMP VALUES ('ABC','XXX',112,'2014-03-02')
INSERT INTO #TEMP VALUES ('ABC','XXX',193,'2014-03-03')

SELECT * FROM #TEMP

/*
So my output would be like
UserName Drink Value
ABC Pepsi 50
ABC xxx 96
ABC Pepsi 510


*/



This looks amazingly similar to your recent thread. http://www.sqlservercentral.com/Forums/Topic1605879-392-1.aspx

The part that concerns me is that if you are unable to figure out the minor difference from the solution there and this one it is obvious you don't understand the solution to your other query. You need to read and understand that query. Once you understand it, the change to this will be really simple.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
JackTimber
JackTimber
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 61
I tried figuring out that , but Sad
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3448 Visits: 33029
JackTimber (8/28/2014)
I tried figuring out that , but Sad



reading the previous thread...it seems that you have 20 million rows....
what solution did you finally decide upon...pls post that here ....and as Sean says it may be easy for us to guide you on this current question

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

JackTimber
JackTimber
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 61
Actaully i was not able to come up with a solution for that problem, so the assignment was given to other guy. I had shown him this thread. He said that , he used ChrisM solution. It work prefectly fine for himwith some changes.
There were changes because i had not posted the correct info at first.
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3448 Visits: 33029
JackTimber (8/28/2014)
Actaully i was not able to come up with a solution for that problem, so the assignment was given to other guy. I had shown him this thread. He said that , he used ChrisM solution. It work prefectly fine for himwith some changes.
There were changes because i had not posted the correct info at first.


...ok so have you posted the correct info this time ??? :-)

________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

JackTimber
JackTimber
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 61
Naa re, its a different asignment. I am working by keeping ChrisM solution as a base from previous example, and lets see.

If i come up with a solution would definitely post the result . :-)
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1161 Visits: 9735
JackTimber (8/28/2014)
Naa re, its a different asignment. I am working by keeping ChrisM solution as a base from previous example, and lets see.

If i come up with a solution would definitely post the result . :-)


If you can post up a representative sample data set, your current query, and the results you are expecting, there are plenty of folks ready to help you.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3448 Visits: 33029
JackTimber (8/28/2014)
Naa re, its a different asignment. I am working by keeping ChrisM solution as a base from previous example, and lets see.

If i come up with a solution would definitely post the result . :-)


ok..I am not really sure where you are with understanding the previous thread solutions and what you have posted as a "new" question in this thread.

for what it is worth...and with no relevance to your previous post, and based entirely on the sample data you have posted in this thread....here is a possible solution that provides what you have requested...whether this delivers effectively ...only you can tell...and then tell us if you need more help.





WITH CTE_SORT as (
SELECT
UserName
, Drink
, Value
, CreatedDate
, ROW_NUMBER() OVER (ORDER BY createddate) AS rn
FROM #TEMP)
,
CTE_HD as (
SELECT
rn - 1 AS sn
FROM cte_sort
WHERE (Drink = 'harddrink'))

SELECT
cs.UserName
, cs.Drink
, cs.Value
FROM cte_sort cs INNER JOIN
cte_hd ch ON cs.rn = ch.sn;





________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day

Shadab Shah
Shadab Shah
Old Hand
Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)Old Hand (307 reputation)

Group: General Forum Members
Points: 307 Visits: 798
I tried a little bit on solution and got it right w00t

So here is the code



SELECT t.UserName, t.Drink, t.CreatedDate, x.Value
FROM #TEMP t
OUTER APPLY (

SELECT TOP 1 Value
FROM
(
--SELECT * FROM #TEMP
SELECT TOP 2 Value , CreatedDate
FROM #TEMP TI
WHERE TI.UserName = T.UserName
AND TI.CreatedDate < T.CreatedDate
--AND TI.Drink IN ('HardDrink')
ORDER BY TI.CreatedDate DESC
) T
ORDER BY CreatedDate DESC
) x
WHERE t.Drink IN ('HardDrink')


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