I have following two tables:
Table 1: Students
name:string,
age:integer,
class:integer,
number_of_allowed_trips:integer
Table 2: Trips
allowed_age:integer,
trip_name:string,
class:integer
I'm trying to assign for each student number of allowed trips based on some conditions. My query is:
UPDATE
`mydataset.students` AS s
SET
s.number_of_allowed_trips=(
SELECT
COUNT(*)
FROM
`mydataset.trips` AS t
WHERE
t.r.class= 9)
FROM
`mydataset.trips` AS t
WHERE
r.name = 'Jack'
AND r.class = 9
AND w.class = r.class
But this query doesn't work. What am I doing false? How can I get this code worked?
UPDATE
`mydataset.students` AS s
SET
s.number_of_allowed_trips= (
SELECT
COUNT(*)
FROM
`mydataset.trips` AS t
WHERE
t.class = s.class
AND s.age= 9 )
WHERE
s.id=s.id
This works fine!
Related
I am trying to write a query to answer the question "What Pop Tart flavor is sold in the most stores?"
Here is my schema:
I have this attempt:
SELECT COUNT(*) AS CountOfStores, PTF.PopTartFlavor AS PopTartFlavor
FROM tKrogerStore_PopTartFlavor KSPTF
INNER JOIN tPopTartFlavor PTF
ON KSPTF.PopTartFlavorID = PTF.PopTartFlavorID
GROUP BY PTF.PopTartFlavor
ORDER BY CountOfStores DESC
but the query gives me this:
and I just want the first 2 rows because they both have a count of 2, but if I say TOP 1 I just get the first row with a count of 2 and I don't get both rows that have a count of 2. How do I get both rows that have the same count (2)?
you could try uisng having the count(*) = to the max result
SELECT COUNT(*) AS CountOfStores, PTF.PopTartFlavor AS PopTartFlavor
FROM tKrogerStore_PopTartFlavor KSPTF
INNER JOIN tPopTartFlavor PTF
ON KSPTF.PopTartFlavorID = PTF.PopTartFlavorID
GROUP BY PTF.PopTartFlavor
HAVING COUNT(*) = (
select max(CountOfStores)
from (
SELECT COUNT(*) AS CountOfStores, PTF.PopTartFlavor AS PopTartFlavor
FROM tKrogerStore_PopTartFlavor KSPTF
INNER JOIN tPopTartFlavor PTF
ON KSPTF.PopTartFlavorID = PTF.PopTartFlavorID
GROUP BY PTF.PopTartFlavor
) t
)
I would capture the binned count and the overall max count in the same subquery query (the latter using an aggregated windowed function), then in the outer query filter for when they're equal.
select CountOfStores, PopTartFlavor
from (
select CountOfStores = count(*),
maxCountOfStores = max(count(*)) over(),
PTF.PopTartFlavor
from #tKrogerStore_PopTartFlavor KSPTF
join #tPopTartFlavor PTF ON KSPTF.PopTartFlavorID = PTF.PopTartFlavorID
group by PTF.PopTartFlavor
) c
where countOfStores = maxCountOfStores
I have a table called 'A' and another table called 'B'.
Here in table A i keep all the master details and where B it keeps the status of field A like approved, rejected.
My need is i need a single query with output as
{
submitted_count: 5,
{[details of first app], [details of 2 app], [], [],[]},
rejected_count : 2,
{[details of first app],[details of second app]}
}
How would i achieve this ?
If you want to convert the result of the query to JSON you need to use the json_agg function.
select json_agg(t)
from (
Select
count(1) as total,
string_agg(tb.detail,',') as details
FROM A tb
inner join B tbb
on tb.id = tbB.id_A
where tbb.status = true
union
Select
count(1) as total,
string_agg(tb.detail,',') as details
FROM A tb
inner join B tbb
on tb.id = tbB.id_A
where tbb.status = false
) t;
The output is a little bit different:
[{"total":2,"details":"Bob,Logan"},{"total":3,"details":"Scott,Jean,Gambit"}]
There is an example here how to use it
I have two tables named [DrugPrescriptionEdition] and [PrescriptionDoseDetail] and now, I join that two tables using the below query and taking a result set.
select * from DrugPrescription dp where id in(
SELECT distinct dpe.template
FROM [DrugPrescriptionEdition] dpe
join PrescriptionDoseDetail pdd on pdd.prescription = dpe.id
where doseEnd_endDate is NULL and doseEnd_doseEndType =1
)
but now I want to take records only contain, (1,2) combination of 'datasource' column and prescription.id should be same.
Example : like records { prescriptionID =4 and there contain ,(1,2) }. I will not consider, only 1 ,or 2 contain records.
Need some expert help to adding this conditions to my above query and modify it .
Expected result : I need to filter out , above query result using this, new condition too.
Let me assume your records are in a single table. Here is one method:
select t.*
from t
where (t.dataSource = 1 and
exists (select 1
from t t2
where t2. prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
) or
(t.dataSource = 2 and
exists (select 1
from t t2
where t2.prescriptionid = t.prescriptionid and
t2.dataSource = 2
)
);
It is unclear if any other data sources are allowed. If they are not, then add:
and
not exists (select 1
from t t3
where t3.prescriptionid = t.prescriptionid and
t3.dataSource not in (1, 2)
)
I am trying to increment a column on a sql server table based on the join between the initial table and the joined table. The idea is to update tblForm10Objectives, set the ObjectiveNumber column to an increment number starting with 1 based on the number of rows returned from the join of tblForm10GoalsObjectives and tblForm10Objectives where ID_Form10Goal equals a number. Example query so far:
Update tblForm10Objectives
Set ObjectiveNumber = rn
From (
Select ROW_NUMBER() over (PARTITION by OG.ID_Form10Goal) as rn
, *
From (
Select *
From tblForm10GoalsObjectives OG
Join tblForm10Objectives O On OG.ID_Form10Objective = O.ID_Form10Objective
Where OG.ID_Form10Goal = 4
Order by O.ID_Form10Objective
) as tblForm10Objectives;
If the select portion of the query is performed the columns are displayed so you can see the ObjectiveNumber is currently 0 where ID_Form10Goal = 4
Once the update runs I need for the ObjectiveNumber to show 1 , 2; since there are two rows for ID_Form10Goal = 4.
I had to introduce a new table to the logic of this update statement, the table name is tblForm10Goals. The objectives need to be pulled by ID_Agency instead of ID_Form10Goal I am getting an error message stating a "a multipart identifier 'dbo.tblForm10Objectives.ID_Form10Objective = rns.ID_Form10Objective' could not be bound. I am using the following SQL Update statement:
UPDATE dbo.tblForm10Objectives
SET ObjectiveNumber = rn
FROM tblForm10Goals As g
Left Join tblForm10GoalsObjectives gobs ON g.ID_Form10Goal = gobs.ID_Form10Goal
Right Join
(
SELECT
ROW_NUMBER() OVER (PARTITION BY g.ID_Agency
ORDER BY OB.ID_Form10Objective) AS rn,
OB.ID_Form10Objective
FROM tblForm10Goals g
LEFT JOIN dbo.tblForm10GoalsObjectives gobs ON g.ID_Form10Goal = gobs.ID_Form10Goal
RIGHT JOIN dbo.tblForm10Objectives OB ON gobs.ID_Form10Objective = OB.ID_Form10Objective
Where g.ID_Agency = 2
) rns ON dbo.tblForm10Objectives.ID_Form10Object = rns.ID_Form10Objective
Your example seems to be missing a closing parenthesis somewhere, and without the table structures to look at, I can't be certain of my answer. It seems you have two tables:
tblForm10Objectives
-------------------
ID_Form10Objective
ObjectiveNumber
...
and
tblForm10GoalsObjectives
------------------------
ID_Form10Goal
ID_Form10Objective
...
If this is the case, the following query should give you the results you desire:
UPDATE dbo.tblForm10Objectives
SET ObjectiveNumber = rn
FROM dbo.tblForm10Objectives INNER JOIN
(
SELECT
ROW_NUMBER() OVER (PARTITION BY OG.ID_Form10Goal
ORDER BY O.ID_Form10Objective) AS rn,
O.ID_Form10Objective
FROM dbo.tblForm10Objectives O INNER JOIN
dbo.tblForm10GoalsObjectives OG ON OG.ID_Form10Objective = O.ID_Form10Objective
Where OG.ID_Form10Goal = 4
) rns ON dbo.tblForm10Objectives.ID_Form10Objective = rns.ID_Form10Objective
If you run the inner SELECT statement, you will see the desired ObjectiveNumber values and the corresponding ID_Form10Objective that will get updated with those values.
If you post your table structures, I or someone else may be able to be of more help.
I have one table and I need to check if two users, for whom I have the IDs (e.g. 20 and 21) share the same course, just true or false.
Table: jos_gj_users
Columns: id_user, id_group
Data Example: (20; 4)
(20; 5)
(20; 6)
(21; 6)
(21; 7)
The data above shows that user 20 and user 21 share the course 6 but how do I get this with SQL just by entering the IDs and without looping through the results with PHP?
Try a self-join:
SELECT T1.id_group
FROM jos_gj_users T1
JOIN jos_gj_users T2
ON T1.id_group = T2.id_group
WHERE T1.id_user = 20
AND T2.id_user = 21
To just get a "true or false" result you can check from the client to see if at least one row exists in the result set rather than fetching the entire results.
Alternatively you can do it in SQL by wrapping the above query in another SELECT that uses EXISTS:
SELECT CASE WHEN EXISTS
(
SELECT T1.id_group
FROM jos_gj_users T1
JOIN jos_gj_users T2
ON T1.id_group = T2.id_group
WHERE T1.id_user = 20
AND T2.id_user = 21
) THEN 1 ELSE 0 END AS result
This query returns either 0 (false) or 1 (true).
The idea is that you have to join the table to itself. In the first half you look for user 1 and in the second half you look for user 2. And of course only those rows that have the same id_group in both half are relevant:
SELECT count(*)
FROM jos_gj_users As j1, jos_gj_users As j2
WHERE j1.id_user = 20 AND j2.id_user = 21
AND j1.id_group = j2.id_group
This will always return one row with one column: The number of shared courses. If it is 0, they don't share any courses.
You could do it with a subselect:
select id_group
from jos_gj_users
where (id_user = 20)
and id_group in (select id_group from jos_gj_users where id_user = 21)
SELECT COUNT(*) > 0 FROM jos_gj_users WHERE id_user=54321 AND id_group IN ( SELECT id_group FROM jos_gj_users WHERE id_user = 1345 )
This is query that shows users from same groups.
SELECT
*
FROM
jos_gj_users T1
INNER JOIN jos_gj_users T2 ON T1.id_group = T2.id_group
Give this a try - it accepts the input parameters in the first bolded area, and returns a value of TRUE or FALSE via a CASE statement based on the values in the second bolded areas.
SELECT DISTINCT CASE WHEN
(SELECT DISTINCT COUNT(id_group) FROM jos_gj_users WHERE id_user IN (20, 21) GROUP BY id_group
HAVING COUNT(DISTINCT id_user) = 2) IS NOT NULL THEN 'TRUE'
ELSE 'FALSE'
END
FROM jos_gj_users