apologies if question is ambiguous. following is the scenario:
question master table:
id No
1 1
2 1
3 2
4 3
5 3
6 3
question part table:
qid statement
1 abc
2 xyz
3 a1235
4 abcde
5 asdf
this data now needs to be imported into a new structure
where only one qid should be present for each question no.
so in above example qid 1,2 should only be either 1 or 2 etc.
an trying to make an update query but it doesnt seem to do what exactly is needed.
the end result can be like so:
qid statement
1 abc
1 xyz
3 a1235
4 abcde
4 asdf
the query is as follows:
update questionpart qp
set Q_ID =
(
select max(nq.Q_ID) FROM newquestion nq
where nq.Q_No = (select nq2.Q_No FROM newquestion nq2 where nq2.Q_ID = qp.Q_ID )
)
any help greatly appreciated.
update questionpart qp
join newquestion nq on qp.Q_ID = nq.Q_ID
set qp.Q_ID = nq.NO;
I believe this is what you are trying to do. ( Set the question part Q_ID to be the new question/master question Q_NO value. If this is not what you are looking for please provide the table schemas.
Related
I got some issues while structuring my database and I would like to know how to solve the below question .
I have a table in database :
id
id_study
writer
1
1
writer1
2
1
writer2
3
2
writer3
Please help me how to combine above 2 table rows to produce output like below :
id
id_study
writer
1,2
1
writer1, writer2
3
2
writer3
SELECT GROUP_CONCAT(id) as id, id_study,GROUP_CONCAT(Writer) as Writer
FROM TABLE
GROUP BY id_study;
I have a rather complex SQL Server query (at least to me) to write on a demographics data set. I need to figure out how many respondents in the system mathc a specific demographic.
I have 2 main tables. I will list the relevant columns. Assume there are unique ID's on each row.
Table Respondents:
[RespondentID] [SystemEntryDate]
Table RespondentProfiles:
[QuestionID] [AnswerID]
The respondent ID on Respondents links to RespondentProfiles. For each question answered, a row is created. The question id corresponds to a specific question (say gender, ethnicity, state, and car ownership) and the answer id means something different depending on the question. Like 1 is male and 2 is female, or 1 might be white, 2 hispanic, 3 pacific islander, and so on.
I also have a table called Conditions. The conditions table looks like this:
[ConditionSetID] [QuestionID] [AnswerID]
The condition set id links to the conditions together into a collection of conditions. So i can pass a condition set id to the query, and it will return a count of how many respondents meet that criteria, as well as the min and max dates from that set.
My query will look something like this:
create procedure query
#ConditionSetID int
as
select count(distinct r.ID) as Respondents,
min(r.SystemEntryDate) as EarliestDate,
max(r.SystemEntryDate) as LatestDate
from Respondents r
join RespondentProfiles rp
on r.ID = rp.RespondentID
join Conditions c
on c.ConditionSetID = #ConditionSetID
and c.QuestionID = rp.QuestionID
where rp.QuestionID = c.QuestionID
and rp.Condition = c.AnswerID
As an example, I might have a respondent profiles table like this
[RespondentID] [QuestionID] [AnswerID]
10001 1 (gender) 1 (male)
10001 2 (ethnicity) 1 (white)
10001 3 (car) 23 (lexus)
10002 1 (gender) 2 (female)
10002 2 (ethnicity) 2 (black)
10002 3 (car) 24 (buick)
10003 1 (gender) 2 (female)
10003 2 (ethnicity) 1 (white)
10003 3 (car) 5 (honda)
10004 1 (gender) 1 (male)
10004 2 (ethnicity) 2 (black)
10004 3 (car) 24 (buick)
And if I pick a specific condition set, the rows id have might be like:
[QuestionID] [AnswerID]
1 (gender) 2 (female)
2 (ethnicity) 2 (black)
3 (car) 24 (buick)
This would be asking for all the black females who own a buick, which should give em a count of 1.
Or I could have:
[QuestionID] [AnswerID]
3 (car) 23 (lexus)
3 (car) 24 (buick)
This is asking for everyone who owns a buick or lexus, which would be 3 people.
And then as a final example:
[QuestionID] [AnswerID]
2 (ethnicity) 2 (black)
3 (car) 23 (lexus)
3 (car) 24 (buick)
This is asking for everyone who is black and owns a lexus or everyone who is black and owns a buick, which would be 2 people.
I know this isn't horribly complicated, but it is the most complex thing I've attempted yet, and any help would be greatly appreciated. I'm having a lot of trouble figuring out how to set up the where clause, and even general direction would be appreciated. There are also about 800,000 records in the respondentprofiles table, so it must be efficient.
The where clause I have set up isn't quite correct, because it will only get the records as if the different questions are being or'd together as opposed to and'ed. So it will return a row for that respondent even if only one answer matches, which is wrong. A particular respondent must meet all the conditions in the condition set to be selected.
Perhaps I need to select into a temp table question at a time or something? Or use some sort of grouping? I am just really confused on where to go with this. I hope I have provided enough information to adequately demonstrate my dilemma.
The examples below show how to get the respondent IDs of respondents who answered:
To question A, Yes
To question B, No
TO question C, Yes
Assuming you are actually using SQL server (you tagged both mysql and sql server in your question), you can use:
select id
from RespondentProfiles
where QuestionID = 'a'
and AnswerID = 'yes'
intersect
select id
from RespondentProfiles
where QuestionID = 'b'
and AnswerID = 'no'
intersect
select id
from RespondentProfiles
where QuestionID = 'c'
and AnswerID = 'yes'
Or if you are using MySQL you can use:
select id
from RespondentProfiles x
where QuestionID = 'a'
and AnswerID = 'yes'
join (select id
from RespondentProfiles
where QuestionID = 'b'
and AnswerID = 'no') y
on x.id = y.id
join (select id
from RespondentProfiles
where QuestionID = 'c'
and AnswerID = 'yes') z
on y.id = z.id
Just to add to my answer what I put in the comments - there is no need for your conditions table. You do not need to have such a table in order to query for respondents who answers 2+ questions a certain way. You can use inline views and/or subqueries to accomplish that. (or in the case of sql server, the intersect set operator)
id - in_id - nat_id
1 1 1
2 1 3
3 3 3
4 2 1
Is it possible to select with mysql only the values in the above table which are the same across the 3 columns, ie return 1 and 3?
Or is this kind of filter only possible post query with php?
Thanks,
John
This simple query should work for you:
SELECT id
FROM your_table
WHERE id = in_id
AND nat_id = in_id
;
Check example at SQLFiddle: SQLFiddle Example
table 1 and table 2
In table 1:
Question ID Question Name
1 a
2 b
3 c
4 d
5 e
6 f
7 g
In table 2, Parent question ID has relation with child question id which are derived from table 1:
parent Question ID child Question ID
1 2
1 5
1 4
1 3
6 7
Now i need the answer to get result like this:
Question ID Question Name
1 a
2 b
5 e
4 d
3 c
6 f
7 g
Please provide the mysql query for it. Thanks in advance
I'm not sure I understand your question entirely, but from what I can glean, I'd do something to this effect:
SELECT
table2.child_question_id,
table1.question_name
FROM
table2,
table1
WHERE
table2.child_question_id = table1.question_id
At a basic glance it looks to get the data how you need it!
-Edit, I'm sure that I'm missing something from your question, but stuck my solution up anyway, hopefully it'll be of some use!
The original question is here.. MySQL self-referencing ID and selects
I would like to pose the question in a way with all the relation to a specific case removed.
I have the example table..
id1 id2
1 5
5 1
2 3
3 2
What SQL command would return..
id1 id2
1 5
2 3
Essentially removing the "duplicate rows".
Q1 and Q2 are the alias' I've created for your table, so we can reference the id's as if they were on different tables.
DELETE Q1 FROM table Q1
JOIN table Q2
ON Q1.id1 = Q2.id2
AND Q2.id1 = Q1.id2
WHERE Q1.id1 > Q1.id2