I'm getting a query output as follows from a table
RowId QuestionGroupId QuestionId Answer
1 2 1 Single
2 2 2 With Kids
3 2 3 Not At All
4 3 1 Single
5 3 2 With Kids
6 3 3 Occasionally Smoke
But the result I would Like to get is as something follows
RowId QuestionGroupId ValueSet
1 2 [{QuestionId:1,Answer:Single},QuestionId:2,Answer:WithKids},QuestionId:3,Answer:Nt at all}]
2 3 [{QuestionId:1,Answer:Single},QuestionId:2,Answer:WithKids},QuestionId:3,Answer:Occasionally Smoke}]
So how to I convert the rows of record to JSON using SQL JSON ability.
Using FOR JSON AUTO given me all in one row of record.Your help is appreciated.
I don't really get what you're doing with the RowId column, but you should be able to do something like this with a correlated subquery to get what you're after
;with src
(
RowId,
QuestionGroupId,
QuestionId,
Answer
) as
(
select 1, 2, 1, 'Single'
union all select 2, 2, 2, 'With Kids'
union all select 3, 2, 3, 'Not At All'
union all select 4, 3, 1, 'Single'
union all select 4, 3, 2, 'With Kids'
union all select 4, 3, 3, 'Occasionally Smoke'
)
select
QuestionGroupId,
(
select i.QuestionId, i.Answer
from src i
where o.QuestionGroupId = i.QuestionGroupId
order by QuestionId
for json auto
) as ValueSet
from src o
group by QuestionGroupId
Related
I want to copy the third and the forth column but change the second column. What should I do? I want to know how to write the sql query. Thank you.
for example:
table1:
1, 1, aaa, bbb
2, 1, ads, bff
3, 1, awq, bcc
and I want table1 finally to be:
1, 1, aaa, bbb
2, 1, ads, bff
3, 1, awq, bcc
4, 2, aaa, bbb
5, 2, ads, bff
6, 2, awq, bcc
(the first column is id)
This should work:
insert into table1 (column2,column3,column4)
select 2,column3, column4
from table1 where column2 = 1
As you suggest:
select (#cnt:=#cnt + 1) as RowNumber,a.c1,a.c2,a.c3 from
(select 1 as id, 1 as c1, 'aaa' as c2, 'bbb'as c3 union all
select 2, 1, 'ads', 'bff' union all
select 3, 1, 'awq', 'bcc'
union all
select 1 as id, 1 as c1, 'aaa' as c2, 'bbb'as c3 union all
select 2, 1, 'ads', 'bff' union all
select 3, 1, 'awq', 'bcc') as a
cross JOIN
(select #cnt:=0) as tmp
RESULT:
1 1 aaa bbb
2 1 ads bff
3 1 awq bcc
4 1 aaa bbb
5 1 ads bff
6 1 awq bcc
Need MySql query that should return result by ordering values in particular field.
From below my result set should contain order like parent_id (1,4,6) should come first parent_id(2,3,7) come next and other should come last.
d data parent_id
----------------------
1 a1 1
2 abc 3
3 abcd 4
4 xyz 2
5 zxyy 6
2 abc 8
3 abcd 9
4 xyz 2
5 zxyy 15
Use a CASE expression in your ORDER BY clause:
SELECT d, data, parent_id
FROM yourTable
ORDER BY CASE WHEN parent_id IN (1, 4, 6) THEN 1
WHEN parent_id IN (2, 3, 7) THEN 2
ELSE 3 END,
parent_id
Follow the link below for a running demo:
SQLFiddle
Use below mentioned query
SELECT d, data, parent_id FROM table_name ORDER BY FIELD(parent_id,1,4,6,2,3,7);
Can someone suggest me a query to get combined count from 2 columns. My specific requirement is as following:
A B Permission
--------------------------
1 2 accept
2 3 accept
3 4 accept
1 6 accept
1 4 accept
2 1 accept
3 1 accept
4 1 pending
I want the count of 1 whether it belong to A or B and the permission is 'accept'. For the above example I need the output as 5
You can do this by denormalizing first the data using UNION ALL and then use COUNT to achieve the desired result:
WITH SampleData(A, B, Permission) AS(
SELECT 1, 2, 'accept' UNION ALL
SELECT 2, 3, 'accept' UNION ALL
SELECT 3, 4, 'accept' UNION ALL
SELECT 1, 6, 'accept' UNION ALL
SELECT 1, 4, 'accept' UNION ALL
SELECT 2, 1, 'accept' UNION ALL
SELECT 3, 1, 'accept' UNION ALL
SELECT 4, 1, 'pending'
)
SELECT
t.ColValue,
ValueCount = COUNT(*)
FROM (
SELECT
Col = 'A', ColValue = A, Permission
FROM SampleData
UNION ALL
SELECT
Col = 'B', ColValue = B, Permission
FROM SampleData
) t
WHERE Permission = 'accept'
GROUP BY t.ColValue
RESULT
ColValue ValueCount
----------- -----------
1 5
2 3
3 3
4 2
6 1
SELECT COUNT(*) FROM table WHERE (A=1 OR B=1) AND Permission LIKE 'accept'
You mean this?
As you said A or B, I think when you have 1 in A And in B it means 1 count; So I suggest this edited query?
SELECT M, COUNT(*) As C
FROM (
SELECT A as M, Permission
FROM yourTable
UNION ALL
SELECT B, Permission
FROM yourTable
WHERE A <> B) Dt
WHERE Permission = 'accept'
GROUP BY M
I got the answer and changed it to work for postgre :
WITH SampleData(auid, buid, permission) AS(
SELECT 1, 2, 'accept' UNION ALL
SELECT 2, 3, 'accept' UNION ALL
SELECT 3, 4, 'accept' UNION ALL
SELECT 1, 6, 'accept' UNION ALL
SELECT 1, 4, 'accept' UNION ALL
SELECT 2, 1, 'accept' UNION ALL
SELECT 3, 1, 'accept' UNION ALL
SELECT 4, 1, 'pending')
SELECT t.col, COUNT(*)as count
FROM (
SELECT auid as col, permission FROM SampleData
UNION ALL
SELECT buid as col, permission FROM SampleData
) t
WHERE permission = 'accept'
GROUP BY t.col
Result
col count
----------- -----------
1 5
2 3
3 3
4 2
6 1
In a table I have records as follows:
ID, ID1, ID2
1, 2, 3
2, 2, 4
3, 2, 5
4, 3, 3
4, 3, 4
4, 4, 3
4, 4, 4
4, 4, 5
I want to be able to find all ID1 values which exist in the table which have ALL of the ID2 values 3, 4 AND 5
So in this case I would want some SQL to pull out only ID1 = 2 and ID1 = 4, but not ID1 = 3 because there exist only ID2=3 and ID2=4 for ID1=3... so it's missing a row for ID2=5 and hence I do not want it included in my result set.
Is there an efficient way to do this?
TY!
You will want to use the following which selects all rows that have an id2 with a value of 3, 4 or 5 and then applies a group by with a having clause to make sure that you return 3 distinct id2 values:
select id1
from yourtable
where id2 in (3, 4, 5)
group by id1
having count(distinct id2) = 3
See SQL Fiddle with Demo.
This type of query is known as relational division.
I have a single table with a self reference InReplyTo with some data like this:
PostID InReplyTo Depth
------ --------- -----
1 null 0
2 1 1
3 1 1
4 2 2
5 3 2
6 4 3
7 1 1
8 5 3
9 2 2
I want to write a query that will return this data in it's threaded form so that the post with ID=2 and all it's descendants will output before PostID=3 and so on for unlimited depth
PostID InReplyTo Depth
------ --------- -----
1 null 0
2 1 1
4 2 2
6 4 3
9 2 2
3 1 1
5 3 2
8 5 3
7 1 1
Is there a simple way to achieve this? I am able to modify the DB structure at this stage so would the new hierarchy datatype be the easiest way to go? Or perhaps a recursive CTE?
-- Test table
declare #T table (PostID int, InReplyTo int, Depth int)
insert into #T values (1, null, 0), (2, 1, 1), (3, 1, 1), (4, 2, 2),
(5, 3, 2), (6, 4, 3), (7, 1, 1), (8, 5, 3),(9, 2, 2)
-- The post to get the hierarchy from
declare #PostID int = 1
-- Recursive cte that builds a string to use in order by
;with cte as
(
select T.PostID,
T.InReplyTo,
T.Depth,
right('0000000000'+cast(T.PostID as varchar(max)), 10)+'/' as Sort
from #T as T
where T.PostID = #PostID
union all
select T.PostID,
T.InReplyTo,
T.Depth,
C.Sort+right('0000000000'+cast(T.PostID as varchar(max)), 10)+'/'
from #T as T
inner join cte as C
on T.InReplyTo = C.PostID
)
select PostID,
InReplyTo,
Depth,
Sort
from cte
order by Sort
Result:
PostID InReplyTo Depth Sort
----------- ----------- ----------- --------------------------------------------
1 NULL 0 0000000001/
2 1 1 0000000001/0000000002/
4 2 2 0000000001/0000000002/0000000004/
6 4 3 0000000001/0000000002/0000000004/0000000006/
9 2 2 0000000001/0000000002/0000000009/
3 1 1 0000000001/0000000003/
5 3 2 0000000001/0000000003/0000000005/
8 5 3 0000000001/0000000003/0000000005/0000000008/
7 1 1 0000000001/0000000007/
What you are looking for is indeed a recursive query.
A matching example to your case can be found here