Retrieve data from Table - sql

I have a table as follows,
Id Form_Id Form_Layout_Txt
-------------- ----------- -----------------
2 1 aa
3 1 bb
5 2 dddd
6 2 eeeee
7 3 fffff
8 3 gggg
i need to retreive the rows which has same Form_id as follows,
2 1 aa
3 1 bb
How to write a query for the above data?
Thanks

Here is what you need at most basic level, unless there's a hidden immunity to find out more :)
SELECT * FROM YOUR TABLE
WHERE FORM_ID = 1

Select *
from nameoftable
where Form_Id = 1;

Related

How to merge 2 tables together?

I have 2 a table with this structure:
[Database1].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 book1
2 book2
3 book2
4 tv1
[Database2].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 nick1
2 nick2
3 book2
4 tv2
5 tv3
6 tv1
I have 2 tables with this structure where the UserID column is linked to the Users_Detail
[Database1].[dbo].[Sys_User_Ghost]
ID UserID Value
-------------------------------
1 3 Value1
2 3 Value2
3 3 Value3
4 4 Value4
[Database2].[dbo].[Sys_User_Ghost]
ID UserID NickName
-------------------------------
1 3 Value12
2 2 Value13
3 4 Value14
4 4 Value15
5 6 Value16
6 6 Value16
I want to convert this table to:
[Database1].[dbo].[Users_Detail]
UserID NickName
-------------------------------
1 book1
2 book2
3 book2
4 tv1
5 tv3
6 nick1
7 nick2
8 book2
9 tv2
10 tv1
[Database1].[dbo].[Sys_User_Ghost]
ID UserID Value
-------------------------------
1 3 Value1
2 3 Value2
3 3 Value3
4 4 Value4
5 8 Value12
6 7 Value13
7 9 Value14
8 9 Value15
9 10 Value16
10 10 Value16
I want the duplicate userid columns to be able to change to another userid without duplicates and add.
I have a Sys_User_Ghost table with a userid column associated with the userid at Users_Detail. I want it to sync with the duplicate userids changed in the question above.
For User_Details table use the following query:
INSERT INTO [Database1].[dbo].[Users_Detail] (UserID, NickName)
SELECT UserID, NickName
FROM [Database2].[dbo].[Users_Detail]
For Sys_User_Ghost table use this query:
INSERT INTO [Database1].[dbo].[Sys_User_Ghost] (ID, UserID, Value)
SELECT ID, UserID, Value
FROM [Database2].[dbo].[Sys_User_Ghost]
Using these queries from database2 tables data insert into database1 tables, you can check using following query
SELECT * FROM [Database1].[dbo].[Users_Detail]
SELECT * FROM [Database1].[dbo].[Sys_User_Ghost]
You can do it in below approach,
For Users_Detail Table,
select * from [Database1].[dbo].[Users_Detail]
union all
select * from [Database2].[dbo].[Users_Detail]
For Sys_User_Ghost table,
select * from [Database1].[dbo].[Sys_User_Ghost]
union all
select * from [Database2].[dbo].[Sys_User_Ghost]
Note that if you dont want to take duplicates then use union instead of unionall.
See here details about sql union and union all .
https://www.sqlshack.com/sql-union-vs-union-all-in-sql-server/

Finding Distinct values across multiple columns and rows in SQL

I have data like this
Id code1 code2 code3 code4 code5 code6
1 2 3 4 5 6 7
1 4 5 2 3 7 6
1 7 6 5 2 3 4
1 5 7 6 4 3 2
1 7 5 6 3 2 4
I need to identify the distinct codes from this set of 6 codes across 5 rows and 6 columns and display them in any order of 6 rows with ID and code
OUTPUT
ID Code
1 7
1 6
1 2
1 3
1 5
1 4
enter image description here
One method is to use union :
select id, code
from (select id, code1 as code
from table t
union
select id, code2
from table t
. . .
select id, code6
from table t
) t;

BigQuery: Flattening all repeated fields in nested schema

I am having so much trouble with querying from Big Query's nested schema.
I have the following fields.
I want to flatten the table and get something like this.
user | question_id | user_choices
123 | 1 | 1
123 | 1 | 2
123 | 1 | 3
123 | 1 | 4
From other resources, I got to a point where I can query from one of the records in the repeated columns. Such as the following:
SELECT user, dat.question_id FROM tablename, UNNEST(data) dat
It gives me this result.
But when I do this, I get another repeated columns again.
SELECT user, dat.question_id, dat.user_choices FROM tablename, UNNEST(data) dat
Can anyone help me how to UNNEST this table properly so I can have flattened schema for all data items?
Thanks!
Below is for BigQuery Standard SQL
#standardSQL
SELECT user, question_id, choice
FROM `project.dataset.table`,
UNNEST(data) question,
UNNEST(user_choices) choice
You can test, play with above using dummy data from your question like below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 1 user,
[STRUCT<question_id INT64, user_choices ARRAY<INT64>>
(1,[1,2,3]),
(2,[2,5]),
(3,[1,3])
] data UNION ALL
SELECT 2 user,
[STRUCT<question_id INT64, user_choices ARRAY<INT64>>
(1,[2,3]),
(2,[4,5]),
(3,[2,6])
] data
)
SELECT user, question_id, choice
FROM `project.dataset.table`,
UNNEST(data) question,
UNNEST(user_choices) choice
ORDER BY user, question_id, choice
with result
Row user question_id choice
1 1 1 1
2 1 1 2
3 1 1 3
4 1 2 2
5 1 2 5
6 1 3 1
7 1 3 3
8 2 1 2
9 2 1 3
10 2 2 4
11 2 2 5
12 2 3 2
13 2 3 6

Convert delimited strings to parent-child type rows in SQL Server 2008 R2

I have the following table data:
SELECT * from Autenticacao_Permissoes;
PermissionId PermissionName RoleId
------------ -------------- ---------
1 Pages.Clientes.Editar.Test 3
2 Pages.Sinistros.Editar 3
3 Pages.Hack.Ver.Editar.Comer.Maca 3
4 Pages.Sinistros.VerFolder3 3
5 Pages.Hack.Ver NULL
There is no limit to the permissions' depth and I would like to have a temporary table with the following format:
SiteMapId Title RoleId ParentId
--------- ------------------------ ------ --------
1 Pages NULL NULL
2 Clientes NULL 1
3 Sinistros NULL 1
4 Hack NULL 1
5 Editar NULL 2
6 Test 3 5
7 Editar 3 3
...
The idea is to use it according to the ASP.NET sitemap data structure.

How to apply Joins in Oracle to require result

I have Following 3 tables :
SHIFT_MASTER,PATTERN_MASTER,PATTERN_DETAILS
S_ID ,P_ID,P_D_ID are the priamry keys of SHIFT_MASTER,PATTERN_MASTER,PATTERN_DETAILS tables respectively.
SHIFT_MASTER
S_ID | S_NUMBER| S_Name
---------------------------------
1 A MORNING
2 B AFTERNOON
3 C NIGHT
PATTERN_MASTER
P_ID | P_NAME
----------------
1 Pattern 1
2 Pattern 2
PATTERN_DETAILS
P_D_ID|P_ID | S_ID| ...
---------------------
1 1 1
2 1 2
3 1 3
4 1 2
5 1 1
6 2 3
7 2 2
8 2 1
9 2 3
I GOT OUTPUT AS
P_ID | S_ID
1 1,2,3,2,1
2 3,2,1,3
USING QUERY
SELECT PATTERN_DETAILS.P_ID "PATTERN",
LISTAGG(PATTERN_DETAILS.S_ID, ', ')
WITHIN GROUP (ORDER BY PATTERN_DETAILS.P_D_ID) "SHIFT"
FROM PATTERN_DETAILS
GROUP BY PATTERN_DETAILS.P_ID;
WHAT I WANT IS
P_NAME | S_NUMBER
Pattern 1 A,B,C,B,A
Pattern 2 C,B,A,C
Any suggestion ??? Instead of P_ID i want to show pattern name and instead of shift id i want to show shift number .How to perform join operation along with listagg function ?
You need to join all three tables to get this,
SELECT pm.p_name "P_NAME",
listagg(sm.s_number, ', ') WITHIN GROUP (ORDER BY pd.p_d_id) "S_NUMBER"
FROM pattern_master pm,
pattern_details pd,
shift_master sm
WHERE sm.s_id= pd.s_id
AND pm.p_id = pd.p_id
GROUP BY pm.p_name;