Hi im new with sql queries i need help.
I have a table which have QuestionId from 0 to 8 but sometimes except zero a value can be repeated, what i want is top create a group from 0 to 8 and when other 0 occurs it will be a new group, instead of creating i want to give random number to SurveyID column.
QuestionID SurveyID
0 NULL
1 NULL
2 NULL
3 NULL
4 NULL
4 NULL
5 NULL
6 NULL
7 NULL
7 NULL
8 NULL
8 NULL
0 NULL
1 NULL
2 NULL
3 NULL
4 NULL
4 NULL
5 NULL
6 NULL
6 NULL
7 NULL
8 NULL
0 NULL
1 NULL
2 NULL
i want to update the surveyID in group from first 0 before second 0.
I want to give Unique random value to the group form 0 to 8 then again a new random value to 0 - 8
QuestionID SurveyID
0 8888
1 8888
2 8888
3 8888
4 8888
4 8888
5 8888
6 8888
7 8888
7 8888
8 8888
8 8888
0 1232
1 1232
2 1232
3 1232
I want this to be done.
You can use a CTE query to generate SurveyId the way you want. I assumed that your primary key column is named Id and table seq - please replace that. You can also use a different way to generate random numbers - up to you.
with cte as
(
select Id, questionId, convert(int, rand() * Id * 1000) as surverId
from seq
where questionId = 0
union all
select seq.Id, seq.questionId, cte.surverId
from seq
inner join cte on cte.Id + 1 = seq.Id
where seq.questionId <> 0
)
merge seq as target
using (select * from cte) as source (Id, questionId, surveyId)
on (target.Id = source.Id)
when matched then
update set target.SurveyId = source.surveyId;
Related
The USER table structure is
id owneeId delete shop
1 12 null 1
2 13 1 0
3 12 null 1
4 7 1 1
5 7 null 1
6 13 null 1
7 16 null 1
8 17 null 1
Now I am getting the result of ( query given below )
select * from users WHERE ownerId in ('12', '13') and delete is null or
ownerId not IN ('12','13') and shop=1 and delete is null
I will get all users with owner id 12 or 13 (with delete null) + all users with owner id not 12 or 13 + shop =1 and dlete is null.
Desired result
I will give another ownerID 7.
I need to get all the users with owner id 7 first, that means; from above table ,
all users with owner id 7 and shop =1 first ...only then all users with owner id 12 or 13 (with delete null) + all users with owner id not 12 or 13 + shop =1 and dlete is null
query will be something like
I need to add "select * from users where ownerId 7 first and shop 1 and delete null" to the query(above one, this one below- both same query)
select * from users WHERE ownerId in ('12', '13') and delete is null or
ownerId not IN ('12','13') and shop=1 and delete is null
** (PS)EDIT FOR BETTER CLARITY**
My current result is
id owneeId delete shop
1 12 null 1
2 12 null 1
2 7 null 1
2 13 null 1
2 16 null 1
2 17 null 1
what I need is- desired result
id owneeId delete shop
2 7 null 1
1 12 null 1
2 12 null 1
2 13 null 1
2 16 null 1
2 17 null 1
all user with ownerid 7 first , then the rest...( I am getting this result as an array of objects in node , in sql as table)
PS : Owner id can be any number, in this case it is 7. It can be 912, 989,89009,...etc
If you wish to just combine the 2 results, you can simply use union like below:
(
select *
from users
where ownerId = 7 and shop = 1
)
union
(
select *
from users
WHERE (ownerId in ('12', '13') and `delete` is null) or
(ownerId not IN ('12','13') and shop = 1 and `delete` is null)
)
Note: Using delete as one of the column names is not recommended since delete is a keyword in itself. You can rather go for deleted_at if it's a timestamp etc.
I'm using a Postgres database.
I've been trying to resolve this for hours and read dozens of topics with no result yet
Since I don't know how to explain my issue with words, here is what I need by example :
My query is
select distinct chiffre_affaires.contrat_pentete_id,
chiffre_affaires.chiffre_affaires_id,
chiffre_affaires.chiffre_affaires_montant_total
from chiffre_affaires;
Current output :
contrat_pentete_id
chiffre_affaires_id
chiffre_affaires_montant_total
1
1
111.7848
1
2
111.7848
1
3
111.7848
1
4
111.7848
1
5
111.7848
1
6
111.7848
2
7
90
2
8
90
2
9
90
2
10
90
Expected output :
null values can be replaced by 0, both null or 0 would work
contrat_pentete_id
chiffre_affaires_id
chiffre_affaires_montant_total
1
1
111.7848
1
2
null
1
3
null
1
4
null
1
5
null
1
6
null
2
7
90
2
8
null
2
9
null
2
10
null
Thank you in advance for any help !
Trying to understand what you want to achieve : for a group of rows with same contrat_pentete_id, ordered by chiffre_affaires_id ASC, you want to display the
chiffre_affaires_montant_total value for the first row, and NULL for the next rows. If so, you can try this :
SELECT DISTINCT
ca.contrat_pentete_id,
ca.chiffre_affaires_id,
CASE
WHEN ca.chiffre_affaires_id = first_value (ca.chiffre_affaires_id) OVER (ORDER BY ca.chiffre_affaires_id)
THEN ca.chiffre_affaires_montant_total
ELSE NULL
END AS ca.chiffre_affaires_montant_total
FROM chiffre_affaires AS ca
ORDER BY ca.contrat_pentete_id, ca.chiffre_affaires_id
Thanks to Edouard H. I finally wrote a script that did the job.
Here is the solution :
SELECT DISTINCT ca.contrat_pentete_id,
ca.chiffre_affaires_id,
ca.chiffre_affaires_annee_mois,
CASE
WHEN ca.chiffre_affaires_id =
first_value(ca.chiffre_affaires_id) OVER (PARTITION BY ca.contrat_pentete_id ORDER BY ca.chiffre_affaires_annee_mois)
THEN ca.chiffre_affaires_montant_total
END AS montant_facture
FROM chiffre_affaires AS ca
ORDER BY ca.contrat_pentete_id;
currently i have following table structure in sql server2008 r2
tbusertable
userid username uid status
1 abc null null
2 yax null null
3 xcd null null
4 max null null
5 wax null null
6 ear null null
7 yes null null
8 sqt null null
9 ora null null
tbphtable
pid userid phnos
1 1 456
2 2 456
3 3 4568
4 4 789
5 5 5555
6 6 4599
7 7 456
8 8 111
9 9 111
tbeidtable
eid userid eid
1 1 y#gmail.com
2 2 abd#gmail.com
3 3 erer#gmail.com
4 4 yer#gmail.com
5 5 g#gmail.com
6 6 g#gmail.com
i want to update uid column of tbusertable table with unique id, if they have same phnos for eid without cursor because table have large records and cursor take long time to run
desire output
userid 1,2,7 have same phnos so they have same unique id and similarly
userid 5,6 have same eid so they have different unique id
and similarly userid 8,9 have same phnos so they have same different unique id
tbusertable
userid username uid status
1 abc D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
2 yax D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
3 xcd null null
4 max 0608CFF7-3FC6-4952-91AE-5E42D6558827 null
5 wax 0608CFF7-3FC6-4952-91AE-5E42D6558827 null
6 ear null null
7 yes D7CCBC4E-EEE6-4AC8-806D-A04DCC77DF54 null
8 sqt 5823E1FD-2AF3-4BA7-8C48-946A16E0D3E2 null
9 ora 5823E1FD-2AF3-4BA7-8C48-946A16E0D3E2 null
If I understand correctly, you can calculate the new ids for each phone number using a subquery and then use this for the update:
update u
set uid = pp.new_uid
from tbusertable u join
tbphtable p
on u.userid = p.userid join
(select phnos, newid() as new_uid
from tbphtable
group by phnos
) pp
on p.phnos = pp.phnos;
EDIT:
Because of how SQL Server optimizes queries, you might need to put the newids in a temporary table:
select phnos, newid() as new_uid
into #pp
from tbphtable
group by phnos;
And then:
update u
set uid = pp.new_uid
from tbusertable u join
tbphtable p
on u.userid = p.userid join
#pp pp
on p.phnos = pp.phnos;
SQL Server can rewrite queries, resulting in functions being called more commonly than expected. Putting the values in a temporary table should solve that problem.
Sql Query
SELECT respondant.respondant_firstname as first_name,
question.question_id as question_id,
answer.answer_id,
answer.answer_text,
answer.answer_rate,
answer.answer_nps,
question_radio.question_radio_text as opt
FROM question
LEFT JOIN answer on answer.answer_question_id = question.question_id
LEFT JOIN question_radio on answer.answer_question_radio_id = question_radio.question_radio_id
LEFT JOIN respondant on answer.answer_respondant_id = respondant.respondant_id
WHERE question.question_feedback_id = 1
ORDER BY question.question_id, answer.answer_id
Output:
first_name question_id answer_id answer_text answer_rate answer_nps opt
RM 1 1 5 NULL NULL
Y 1 3 0 NULL NULL
Ben 1 5 0 NULL NULL
akash 1 8 2.5 NULL NULL
RM 2 2 0 4 NULL
Y 2 4 0 3 NULL
Ben 2 6 0 0 NULL
akash 2 9 0 0 NULL
Ben 3 7 Thanks 0 0 NULL
akash 3 10 0 0 NULL
I Need the output as:
first_name question_id answer_id answer_text answer_rate answer_nps opt
RM 1 1 5 NULL NULL
Y 1 3 0 NULL NULL
Ben 1 5 0 NULL NULL
akash 1 8 2.5 NULL NULL
RM 2 2 0 4 NULL
Y 2 4 0 3 NULL
Ben 2 6 0 0 NULL
akash 2 9 0 0 NULL
RM 3 NULL 0 0 NULL
Y 3 NULL 0 0 NULL
Ben 3 NULL ThankS 0 0 NULL
akash 3 NULL 0 0 NULL
Where in third and fourth row from last doesnt have the id 3 but i need to replace as 3 and other values in that two rows has to be null
What you want to do is rather complicated. You want a row for each responder and question, and then to fill in the details for those that responded. My guess is that you want the answer_id filled in, where available. That is, your desired results should have answer_ids for "Ben" and "Akash".
The following query generates all the rows using a cross join between the responders and the questions. Then it brings in the additional information:
SELECT r.respondant_firstname as first_name, q.question_id as question_id,
a.answer_id, a.answer_text, a.answer_rate, a.answer_nps,
qr.question_radio_text as opt
FROM respondant r CROSS JOIN
question q LEFT JOIN
answer a
on a.answer_question_id = q.question_id and
a.answer_respondant_id = r.respondant_id LEFT JOIN
question_radio qr
on a.answer_question_radio_id = qr.question_radio_id
WHERE q.question_feedback_id = 1
ORDER BY q.question_id, a.answer_id;
I have the following SQL tables, with the following data:
site-obj-Prices:
id Parameter Value ActionFunc ActionValue ChainTo ChainOperator GroupID
1 Locality 0 Set 6 NULL NULL 1
2 Locality 1 Set 3 NULL NULL 2
3 Locality 0 Set 15 4 AND 3
4 State 61 Set 15 NULL NULL 3
5 Locality 0 Set 18 6 AND 4
6 State 61 Set 18 7 AND 4
7 AreaCode 954 Set 18 NULL NULL 4
8 Locality 0 Add -1 9 AND 5
9 State 61 Add -1 10 AND 5
10 AreaCode 954 Add -1 11 AND 5
11 Supplier 242 Add -1 NULL NULL 5
12 Weight 3 Add 3 NULL NULL 6
13 Weight 3 Add 2 14 AND 7
14 Supplier 242 Add 2 NULL NULL 7
site-obj-PricesParams:
id Parameter Order
1 Locality 0
2 State 1
3 AreaCode 2
4 Weight 3
5 Supplier 4
And in the following query I need to change ActionLevel so it will reflect
MAX(Order) from [site-obj-PricesParams]
for the all the parameters in each GroupID.
So if I have a group with parameters 'Locality' and 'Weight', ActionLevel should be 3.
Any help will be appreciated.
SELECT
id, Parameter, Value, ActionFunc, ActionValue, ChainTo, ChainOperator, GroupID,
COUNT(GroupID) OVER(PARTITION BY GroupID) AS ActionLevel
FROM
[site-obj-Prices] as Actions
WHERE
GroupID NOT IN (SELECT [GroupID]
FROM [site-obj-Prices] as act
INNER JOIN #ParametersList as par ON act.Parameter = par.sKey
AND act.Value <> par.sValue
UNION
SELECT [GroupID]
FROM [site-obj-Prices] as act
LEFT JOIN #ParametersList as par ON act.Parameter = par.sKey
WHERE par.sKey IS NULL
)
ORDER BY
ActionLevel ASC
If i am not wrong in understanding your requirement then hopefully this would work.
SELECT id,Parameter,Value,ActionFunc,ActionValue,ChainTo,
ChainOperator,GroupID, MAX([Order]) OVER(PARTITION BY GroupID) AS ActionLevel
FROM [site-obj-Prices] as Actions
Inner Join [site-obj-PricesParams] as Params
On Actions.Parameter = Params.Parameter
Where GroupID NOT IN (
SELECT [GroupID]
FROM [site-obj-Prices] as act
INNER JOIN #ParametersList as par
ON act.Parameter=par.sKey AND act.Value<>par.sValue
UNION
SELECT [GroupID]
FROM [site-obj-Prices] as act
LEFT JOIN #ParametersList as par
ON act.Parameter=par.sKey WHERE par.sKey IS NULL
)
ORDER BY ActionLevel ASC