I have a table with three columns:
Survey QuestionName SurveyQuestion SurveyAnswer
1859 Satisfied? 1 Yes
1859 Comments? 2 Happy with outcome, thanks
1859 Phone Number? 3 Not Answered
2000 Satisfied? 1 No
2000 Comments? 2 Rep was a d1ck.
2000 Phone Number? 3 5166569999
What I want to end up with is a table that looks like this (Note: SurveyQuestion #3 is not needed)
Survey SurveyAnswer1 SurveyAnswer2
1859 Yes Happy with outcome, thanks
2000 No Rep was a d1ck.
I just can't think of how to do this... I've tried several ways including LEAD/LAG, but can't get it to work... I think I just need someone to point me in the right direction 😞
That's four columns ;)
You can use the old aggregate-as-crosstab chestnut:
select Survey,
SurveyAnswer1 = max(iif(SurveyQuestion = 1, SurveyAnswer, null)),
SurveyAnswer2 = max(iif(SurveyQuestion = 2, SurveyAnswer, null))
from YourTable
where SurveyQuestion != 3
group by Survey
Related
Let's say I have the following table in Postgres:
fruit
fruit_id owner_id fruit_price notes
-------------------------------------------
1 5 15
2 5 30
3 5 20
4 8 10
5 8 80
I am looking for a way to update the cheapest fruit owned by someone.
That is, I am looking for an operation that would allow me to set the notes column for the cheapest fruit owned by an individual. So this should only ever update one row (updating multiple rows is fine if there are several ties for the smallest value).
For example (psuedocode):
UPDATE fruit SET notes = 'wow cheap' WHERE owner_id = 5 AND fruit_price IS cheapest;
And this would update the first row in the above example data, because fruit_id of 1 is the cheapest fruit owned by user 5.
One possible way is simply to use a correlated subquery:
update fruit set
notes = 'some notes'
where owner_id = 5
and fruit_price = (
select min(fruit_price) from fruit f2
where f2.owner_id = fruit.owner_id
);
I got a self referencing table that's only 1 deep: comments and replies. A reply is just a comment with a parent id:
Comments (simplified):
- comment_id
- parentCommentId
Users have to scroll through the comments and replies and typically 10 new rows are being fetched each time, I'm trying out an recursive query for this:
WITH RECURSIVE included_childs(comment_id, parent_comment_id) AS
(SELECT comment_id, parent_comment_id
FROM comments
UNION
SELECT c.comment_id, c.parent_comment_id
FROM included_childs ch, comments c
WHERE c.comment_id = ch.parent_comment_id
)
SELECT *
FROM included_childs
limit 10
obviously because of limit 10 not all the childs are being included this way and conversations will be cut off. What I actually want is a limit on the parents and have all childs included, regardless of how many total rows.
update
This is the actual query, now with limit in the first branch:
WITH RECURSIVE included_childs(comment_id, from_user_id, fk_topic_id, comment_text, parent_comment_id, created) AS
((SELECT comment_id, from_user_id, fk_topic_id, comment_text, parent_comment_id, created
FROM vw_comments WHERE fk_topic_id = 2
and parent_comment_id is null
limit 1)
union all
SELECT c.comment_id, c.from_user_id, c.fk_topic_id, c.comment_text, c.parent_comment_id, c.created
FROM included_childs ch, vw_comments c
WHERE c.comment_id = ch.parent_comment_id
)
SELECT *
FROM included_childs
still, this doesn't give me the expected results, I get 1 comment as a result with no replies.
update 2
silly mistake on the where clause:
WHERE c.comment_id = ch.parent_comment_id
should've been
WHERE ch.comment_id = c.parent_comment_id
it's working now.
I think the first branch in the UNION in the recursive CTE should be something like:
SELECT comment_id, parent_comment_id
FROM comments
WHERE parent_comment_id IS NULL
LIMIT 10
Then you'd get all replies for these 10 "root" comments.
I'd expect some sort of ORDER BY in there, unless you don't care for the order.
UNION ALL would be fester than UNION, and there cannot be cycles, right?
I would like to add the values based on a column:
For example: The input table looks like this:
USERS Order_date Number_of_orders
alice 01-01-2014 2
alice 19-01-2014 5
alice 20-05-2014 8
bob 03-01-2014 1
bob 08-04-2014 9
The output should be like:
USERS Order_date Number_of_orders(NEW)
alice 01-01-2014 2
alice 19-01-2014 7
alice 20-05-2014 15
bob 03-01-2014 1
bob 08-04-2014 10
Number_of_orders(NEW) is the sum of total orders in the same day + the total number of previous orders of that user.
Please let me know how to do this with SPSS modeler.
I know this is fairly late but what I would do is:
1) create a conditional derive node if NAME /= #OFFSET(NAME,1) or #NULL(NAME) then Number_of_orders else null
2) create filler node if #NULL(Derive325) then #OFFSET(Derive325,1)+Number_of_orders
Just FYI, if I were to look for this, I would use the word cumulative, not aggregate
my data
column1 column2
1-Sep-11 31-Aug-12
1-May-12 30-Apr-14
1-Mar-09 28-Feb-14
1-Apr-13 31-Mar-14
1-Apr-10 31-Mar-13
i want how many years difference between column1 and column2
out put like
column1 column2
1-Sep-11 31-Aug-12 1
1-May-12 30-Apr-14 2
1-Mar-09 28-Feb-14 5
1-Apr-13 31-Mar-14 1
1-Apr-10 31-Mar-13 3
please let me know
Please try:
=YEAR(B1)-YEAR(A1)
select to_date(Column1,'DD-MON-YYYY')- to_date(Column2,'DD-MON-YYYY') from Table;
Excel solution:
If you only want the difference between the years, i.e. coming from 31-Dec-13 to 1-Jan-14 should result in 1 year, use pnut's formula =YEAR(B1)-YEAR(A1).
If you're interested in the real underlying duration, i.e. 1-Apr-13 to 31-Mar-14 would be 0 years, use this formula: =INT((B1-A1)/365)!
Take the following update statement.
UPDATE TABLE_1
SET Units2 = ABS(Units1)
,Dollars2=ABS(Dallars1)
,Units1 =0
,Dollars1 =0
WHERE Units1 < 0
AND Dollars2 = 0
Here are my questions,
1) Is this legal? It parses and it "seems" to work (on the test table), but will it always work or am I just picking the right records to review.
2) is there a better way to do this.
Thanks,
It is legal, and as long as you are wanting to essentially keep the old values of Units1 and Dollars1 in Units2 and Dollars2 that should work
Here's a test:
CREATE TABLE #Table_1
(
Units1 INT,
Dollars1 MONEY,
Units2 INT,
Dollars2 MONEY
)
GO
INSERT INTO #Table_1 (Units1, Dollars1, Units2, Dollars2)
VALUES (-1,12.00,3,0.00)
GO
UPDATE #TABLE_1
SET Units2 = ABS(Units1)
,Dollars2=ABS(Dollars1)
,Units1 =0
,Dollars1 =0
WHERE Units1 < 0
AND Dollars2 = 0
GO
SELECT *
FROM #Table_1
Outputs:
Units1 | Dollars1 | Units2| Dollars2
0 | 0.00 | 1 | 12.00
Assuming that you're talking about the Dollars1 column, I think it should be fine. Reads should use the current value, writes are committed after the calculations are done.
If you're questioning it now, though I would suggest breaking it into two statements. You're the author and it's not clear to you. Take a little pity on the guy that has to maintain it, and make it clear now.
Your query is correct and is pretty much the way to do it.