i'm using postgre sql and i want to select data from database like this
------------------------------------
total1 | total2 | total3 | province|
------------------------------------
1 |1 |2 |Maluku |
2 |3 |4 |Aceh |
4 |7 |2 |Riau |
------------------------------------
but my result from query like this
------------------------------------
total1 | total2 | total3 | province|
------------------------------------
1 |1 |2 |Maluku |
1 |1 |2 |Aceh |
1 |1 |2 |Riau |
------------------------------------
my query
SELECT (SELECT COALESCE(COUNT(id),0) FROM vent) as total1,
(SELECT COALESCE(COUNT(id),0) FROM ventWHERE jenis='Vent-I') as total2,
(SELECT COALESCE(COUNT(id),0) FROM ventWHERE status='Terpakai') as total3,
b.provinsi as province
FROM public."admin_provinsi" as b LEFT JOIN
public."rs" as a
on a.provno = b.idprov
GROUP BY b.provinsi, b.idprov
ORDER BY total1 DESC
how to make result of the query like that?
my scheme database
table vent
|id|jenis |id_rs|status |
--------------------------
|1 |vent-i|1 |Terpakai|
|2 |vent-i|2 |Tidak |
table rs
|gid|name |provno|
-----------------
|1 |rs depok | 1 |
|1 |rs depok2| 1 |
table admin_provinsi
|idprov|nama |
---------------
|1 |Maluku|
|2 |Aceh |
|3 |Riau |
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 months ago.
Improve this question
This is only sample case, my original table is more complex.
Table A
| SchoolId| ClubId | ChildID|TeacherId|AttendanceDate|IsPresent|
|:------- |:------:| :-----:|:-------:|:------------:|:-------:|
| A | 1 | 1 |1 |22-MAY-2022 |1 |
| A | 1 | 2 |1 |22-MAY-2022 |0 |
| A | 1 | 3 |1 |22-MAY-2022 |1 |
| B | 2 | 11 |2 |22-MAY-2022 |1 |
| B | 2 | 22 |2 |22-MAY-2022 |0 |
| B | 2 | 33 |2 |22-MAY-2022 |0 |
Table B
|ChildID|TeacherId |CreateOn |IsPresent|ReasonId |
|:-----:|:-------: |:--------------:|:-------:|:-------:|
|2 |1 |22-MAY-2022 |0 |1 |
|2 |1 |23-MAY-2022 |0 |2 |
|22 |2 |22-MAY-2022 |0 |2 |
|33 |2 |22-MAY-2022 |0 |3 |
Table C
| ReasonId | ReasonMaster |
| -------- | -------------- |
| 1 | Health |
| 2 | Social |
| 3 | Unknown |
I want the left join result like this :
| SchoolId| ClubId | ChildID|TeacherId|AttendanceDate|IsPresent|ReasonId |ReasonMaster |
|:------- |:------:| :-----:|:-------:|:------------:|:-------:|:-------:|:------------:
| A | 1 | 1 |1 |22-MAY-2022 |1 | | |
| A | 1 | 2 |1 |22-MAY-2022 |0 |2 |Social |
| A | 1 | 3 |1 |22-MAY-2022 |1 | | |
| B | 2 | 11 |2 |22-MAY-2022 |1 | | |
| B | 2 | 22 |2 |22-MAY-2022 |0 |2 |Social |
| B | 2 | 33 |2 |22-MAY-2022 |0 |3 |Unknown |
Here are my cases:
I only want to retrieve the latest data from table B based on create on column on the table B to the table A. Because there is a duplicate input by users. for instance in the table B child Id= 2 & Teacher Id=1.
I only need to retrieve the the data if the status in the table A, column IsPresent=0
There is an additional data from table C which reason master.
Try this
Assuming MS SQL Server
--step 1 - Get the latest rec from table B
;with MaxCreate as ( Select ChildID,TeacherID,Max(CreateOn) as MaxCreateOn
from TableB
group by ChildID,TeacherID)
,LatestCreate as (select TableB.ChildID, TableB.TeacherId, TableB.CreateOn, TableB.IsPresent, TableB.ReasonId
from TableB
inner join MaxCreate
on TableB.ChildID = MaxCreate.ChildID
and TableB.TeacherId = MaxCreate.TeacherId
and TableB.CreateOn = MaxCreate.MaxCreateOn)
-- Now Join the latest to the other tables
Select
TableA.SchoolId
,TableA.ClubId
,TableA.ChildID
,TableA.TeacherId
,TableA.AttendanceDate
,TableA.IsPresent
,LatestCreate.ReasonId
,TableC.ReasonMaster
From
TableA Left join LatestCreate
on TableA.ChildID = LatestCreate.ChildID
and TableA.TeacherID = LatestCreate.TeacherID
left join TableC
on LatestCreate.ReasonId = TableC.ReasonId
Non CTE method
Select
TableA.SchoolId
,TableA.ClubId
,TableA.ChildID
,TableA.TeacherId
,TableA.AttendanceDate
,TableA.IsPresent
,LatestCreate.ReasonId
,TableC.ReasonMaster
From
TableB
inner join
( Select ChildID,TeacherID,Max(CreateOn) as MaxCreateOn
from TableB
group by ChildID,TeacherID) as MaxCreate
on TableB.ChildID = MaxCreate.ChildID
and TableB.TeacherId = MaxCreate.TeacherId
and TableB.CreateOn = MaxCreate.MaxCreateOn
Left join TableA
on TableA.ChildID = TableB.ChildID
and TableA.TeacherID = TableB.TeacherID
left join TableC
on TableB.ReasonId = TableC.ReasonId
I have a data set with 4 columns and want to count the number of times that the value in column 2 was equal to the value in one of the rows in column 0 and also the number of times that the value in column 3 was equal to the value in one of the rows in column 0. Also I want to filter the data based on the value in column 1.
Here is an example:
|0 |1 |2 |3 |
-----------------------------------------
|a |post |b |c |
|x |share |a |d |
|b |post |a |l |
|d |post |N/A |a |
-----------------------------------------
the result should look like this:
|0 |1 |2 |3 |4 |5 |
-------------------------------------------------------------
|a |post |b |c |2 |1 |
|b |post |a |l |1 |0 |
|d |post |N/A |a |0 |1 |
-------------------------------------------------------------
Therefore I need to add two columns to my data set. My initial thought is that I can use nested query. Here's my code:
SELECT *
FROM
(
select t.*,
(select count(*) from
(
select t.*,
(select count(*) from tab where [2] = t.[0]) [4]
from tab t
)
where [3] = t.[0]) [5]
from tab t
)
WHERE [1] = 'post'
but the the result of my query does not return column 4. Can you help me figure out the problem with my code?
Your query contains almost the correct code:
select
t.*,
(select count(*) from tab where [2] = t.[0]) [4],
(select count(*) from tab where [3] = t.[0]) [5]
from tab t
where t.[1] = 'post'
See the demo.
Results:
| 0 | 1 | 2 | 3 | 4 | 5 |
| --- | ---- | --- | --- | --- | --- |
| a | post | b | c | 2 | 1 |
| b | post | a | l | 1 | 0 |
| d | post | N/A | a | 0 | 1 |
I have table X
with 2 columns : ID , name
I need to select only the names with more than 1 ID (and count how much ID those name has)
table X
|namme|ID |
------------
|A |1 |
------------
|A |2 |
------------
|B |1 |
------------
|C |1 |
------------
|C |4 |
------------
|C |7 |
------------
from the table bellow the answer will be like:
|namme|ID Count|
----------------
|A |2 |
----------------
|C |3 |
----------------
name A has 2 IDs
name C has 3 IDs
Try, like:
SELECT Name
,COUNT(ID)
FROM Xtable
GROUP BY Name
HAVING COUNT(ID) > 1
use aggregate function count()
select name,count(*) as cnt from table group by name
having count(*)>1
I have a table which looks like this :
------------------------
|id | status |value|
------------------------
|1 | Y |10 |
|2 | N |10 |
|3 | Y |10 |
|4 | N |10 |
|5 | N |10 |
------------------------
For every status = 'N', I'd like to add 3 to its value and set its status to 'Y'. So, the outcome table should be:
------------------------
|id | status |value|
------------------------
|1 | Y |10 |
|2 | Y |13 |
|3 | Y |10 |
|4 | Y |13 |
|5 | Y |13 |
------------------------
How can I do this in the best way in SQL Server?
update your_table
set status = 'Y',
value = value + 3
where status = 'N'
I have a table with a bunch of scores for lessons, on a user by user basis
------------------------
|uid |lesson_id |score |
------------------------
|1 |0 |20 |
|1 |0 |25 |
|1 |0 |15 |
|1 |0 |40 |
|1 |1 |70 |
|1 |0 |10 |
|1 |1 |20 |
|1 |1 |55 |
|1 |1 |55 |
|1 |0 |5 |
|1 |2 |65 |
------------------------
I also have a table of all possible lessons that can be scored:
------------
|lesson_id |
------------
|0 |
|1 |
|2 |
|3 |
|4 |
|5 |
------------
I need to calculate the maximum score for each lesson in the second table from the scores in the first table, and take an average of that over the number of lessons in the second table:
So, the maximum scores for the scores table are (for user 1):
-----------------------
|lesson_id |max_score |
-----------------------
|0 |40 |
|1 |70 |
|2 |65 |
-----------------------
I need to sum them: 175 and divide by the total number of lessons in table 2: 6 which should give the answer 29.16
Any ideas how to do this in a single statement?
I can get the average of all max values for the scores table (for user 1) like so:
SELECT AVG(max_score) AS avg_max_score FROM
(
SELECT uid, lesson_id, MAX(score) AS max_score FROM cdu_user_progress
AND uid =1
GROUP BY lesson_id
) AS m
SELECT
AVG(max_score)
FROM
(
SELECT
lesson.lesson_id,
max(isNull(score,0)) as max_score
FROM
lesson
LEFT JOIN
cdu_user_progress
ON
lesson.lesson_id = cdu_user_progress.lesson_id
GROUP BY
lesson.lesson_id
) AS m