Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Query Help Expand / Collapse
Author
Message
Posted Thursday, August 28, 2014 9:51 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 8:53 PM
Points: 17, Visits: 53
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


*/

Post #1608329
Posted Thursday, August 28, 2014 10:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 13,082, Visits: 12,546
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)
Post #1608336
Posted Thursday, August 28, 2014 10:25 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 8:53 PM
Points: 17, Visits: 53
I tried figuring out that , but

Post #1608343
Posted Thursday, August 28, 2014 10:30 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 1,917, Visits: 19,593
JackTimber (8/28/2014)
I tried figuring out that , but



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
Post #1608344
Posted Thursday, August 28, 2014 11:18 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 8:53 PM
Points: 17, Visits: 53
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.
Post #1608353
Posted Thursday, August 28, 2014 11:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 1,917, Visits: 19,593
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
Post #1608355
Posted Thursday, August 28, 2014 11:42 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, September 19, 2014 8:53 PM
Points: 17, Visits: 53
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 .
Post #1608359
Posted Thursday, August 28, 2014 11:48 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 6:59 AM
Points: 1,033, Visits: 6,790
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
Post #1608362
Posted Thursday, August 28, 2014 12:20 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:33 PM
Points: 1,917, Visits: 19,593
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
Post #1608378
Posted Thursday, August 28, 2014 12:23 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, October 13, 2014 4:08 AM
Points: 268, Visits: 670
I tried a little bit on solution and got it right

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')

Post #1608380
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse