How to use count correctly in sql? - sql

I have tow tables 'matches' and 'forum' I need to get match information from the matches table which has comments in the forum table so I use the following query:
SELECT distinct forum.match_static_id, matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_id
WHERE forum.comments_yes_or_no = 1
I use distinct to avoid getting the same match twice if it has more than one comment in the forum table.
The problem is I want to get the count of each match comments with the same query is it possible? I use :
SELECT distinct forum.match_static_id, count(forum.comments), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
but it give me just one record (which is wrong). What is the problem ?? does I need to use group by ? and if yes where to but it in this crowded query?

Please try this:
SELECT forum.match_static_id, count(matches.id), matches.*
from forum
INNER JOIN matches
ON forum.match_static_id = matches.static_i
WHERE forum.comments_yes_or_no = 1
GROUP BY forum.id

Related

how to join multiple tables without showing repeated data?

I pop into a problem recently, and Im sure its because of how I Join them.
this is my code:
select LP_Pending_Info.Service_Order,
LP_Pending_Info.Pending_Days,
LP_Pending_Info.Service_Type,
LP_Pending_Info.ASC_Code,
LP_Pending_Info.Model,
LP_Pending_Info.IN_OUT_WTY,
LP_Part_Codes.PartCode,
LP_PS_Codes.PS,
LP_Confirmation_Codes.SO_NO,
LP_Pending_Info.Engineer_Code
from LP_Pending_Info
join LP_Part_Codes
on LP_Pending_Info.Service_order = LP_Part_Codes.Service_order
join LP_PS_Codes
on LP_Pending_Info.Service_Order = LP_PS_Codes.Service_Order
join LP_Confirmation_Codes
on LP_Pending_Info.Service_Order = LP_Confirmation_Codes.Service_Order
order by LP_Pending_Info.Service_order, LP_Part_Codes.PartCode;
For every service order I have 5 part code maximum.
If the service order have only one value it show the result correctly but when it have more than one Part code the problem begin.
for example: this service order"4182134076" has only 2 part code, first'GH81-13601A' and second 'GH96-09938A' so it should show the data 2 time but it repeat it for 8 time. what seems to be the problem?
If your records were exactly the same the distinct keyword would have solved it.
However in rows 2 and 3 which have the same Service_Order and Part_Code if you check the SO_NO you see it is different - that is why distinct won't work here - the rows are not identical.
I say you have some problem in one of the conditions in your joins. The different data is in the SO_NO column so check the raw data in the LP_Confirmation_Codes table for that Service_Order:
select * from LP_Confirmation_Codes where Service_Order = 4182134076
I assume you are missing an and with the value from the LP_Part_Codes or LP_PS_Codes (but can't be sure without seeing those tables and data myself).
By this sentence If the service order have only one value it show the result correctly but when it have more than one Part code the problem begin. - probably you are missing and and with the LP_Part_Codes table
Based on your output result, here are the following data that caused multiple output.
Service Order: 4182134076 has :
2 PartCode which are GH81-13601A and GH96-09938A
2 PS which are U and P
2 SO_NO which are 1.00024e+09 and 1.00022e+09
Therefore 2^3 returns 8 rows. I believe that you need to check where you should join your tables.
Use DINTINCT
select distinct LP_Pending_Info.Service_Order,LP_Pending_Info.Pending_Days,
LP_Pending_Info.Service_Type,LP_Pending_Info.ASC_Code,LP_Pending_Info.Model,
LP_Pending_Info.IN_OUT_WTY, LP_Part_Codes.PartCode,LP_PS_Codes.PS,
LP_Confirmation_Codes.SO_NO,LP_Pending_Info.Engineer_Code
from LP_Pending_Info
join LP_Part_Codes on LP_Pending_Info.Service_order = LP_Part_Codes.Service_order
join LP_PS_Codes on LP_Part_Codes.Service_Order = LP_PS_Codes.Service_Order
join LP_Confirmation_Codes on LP_PS_Codes.Service_Order = LP_Confirmation_Codes.Service_Order
order by LP_Pending_Info.Service_order, LP_Part_Codes.PartCode;
distinct will not return duplicates based on your select. So if a row is same, it will only return once.

Select Count of one table into another

I have one SQL statement as:
SELECT ARTICLES.NEWS_ARTCL_ID, ARTICLES.NEWS_ARTCL_TTL_DES,
ARTICLES.NEWS_ARTCL_CNTNT_T, ARTICLES.NEWS_ARTCL_PUB_DT,
ARTICLES.NEWS_ARTCL_AUTH_NM, ARTICLES.NEWS_ARTCL_URL, ARTICLES.MEDIA_URL,
ARTICLES.ARTCL_SRC_ID, SOURCES.ARTCL_SRC_NM, MEDIA.MEDIA_TYPE_DESCRIP
FROM
RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES,
RSKLMOBILEB2E.MEDIA_TYPE MEDIA,
RSKLMOBILEB2E.ARTICLE_SOURCE SOURCES
WHERE ARTICLES.MEDIA_TYPE_IDENTIF = MEDIA.MEDIA_TYPE_IDENTIF
AND ARTICLES.ARTCL_SRC_ID = SOURCES.ARTCL_SRC_ID
AND ARTICLES.ARTCL_SRC_ID = 1
ORDER BY ARTICLES.NEWS_ARTCL_PUB_DT
Now I need to combine another SQL statement into one which is:
SELECT COUNT ( * )
FROM RSKLMOBILEB2E.NEWS_LIKES LIKES, RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
WHERE LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
Basically I have one table which contains articles and I need to include the user likes which is in another table.
Use a subquery to add the likescount in your first query like this:
SELECT ARTICLES.NEWS_ARTCL_ID
,ARTICLES.NEWS_ARTCL_TTL_DES
,ARTICLES.NEWS_ARTCL_CNTNT_T
,ARTICLES.NEWS_ARTCL_PUB_DT
,ARTICLES.NEWS_ARTCL_AUTH_NM
,ARTICLES.NEWS_ARTCL_URL
,ARTICLES.MEDIA_URL
,ARTICLES.ARTCL_SRC_ID
,SOURCES.ARTCL_SRC_NM
,MEDIA.MEDIA_TYPE_DESCRIP
,(
SELECT COUNT(*)
FROM RSKLMOBILEB2E.NEWS_LIKES LIKES
WHERE LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
) AS LikesCount
FROM RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
,RSKLMOBILEB2E.MEDIA_TYPE MEDIA
,RSKLMOBILEB2E.ARTICLE_SOURCE SOURCES
WHERE ARTICLES.MEDIA_TYPE_IDENTIF = MEDIA.MEDIA_TYPE_IDENTIF
AND ARTICLES.ARTCL_SRC_ID = SOURCES.ARTCL_SRC_ID
AND ARTICLES.ARTCL_SRC_ID = 1
ORDER BY ARTICLES.NEWS_ARTCL_PUB_DT;
I'm not sure what you are trying to achieve but it seems you want to count all the data from 2 tables. You can edit your query to something like this.
SELECT COUNT (ARTICLES.*) FROM RSKLMOBILEB2E.NEWS_LIKES LIKES
JOIN RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
ON LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
I think that solution is in using Analytic Functions. Please have a look on https://oracle-base.com/articles/misc/analytic-functions
Please check following query (keep in mind I have no idea about your table structures). Due to left join records might be duplicated, this is why grouping is added.
SELECT ARTICLES.NEWS_ARTCL_ID, ARTICLES.NEWS_ARTCL_TTL_DES,
ARTICLES.NEWS_ARTCL_CNTNT_T, ARTICLES.NEWS_ARTCL_PUB_DT,
ARTICLES.NEWS_ARTCL_AUTH_NM, ARTICLES.NEWS_ARTCL_URL, ARTICLES.MEDIA_URL,
ARTICLES.ARTCL_SRC_ID, SOURCES.ARTCL_SRC_NM, MEDIA.MEDIA_TYPE_DESCRIP,
count(LIKES.ID) over ( partition by ARTICLES.NEWS_ARTCL_ID ) as num_likes
FROM RSKLMOBILEB2E.NEWS_ARTICLE ARTICLES
join RSKLMOBILEB2E.MEDIA_TYPE MEDIA
on ARTICLES.MEDIA_TYPE_IDENTIF = MEDIA.MEDIA_TYPE_IDENTIF
join RSKLMOBILEB2E.ARTICLE_SOURCE SOURCES
on ARTICLES.ARTCL_SRC_ID = SOURCES.ARTCL_SRC_ID
LEFT JOIN RSKLMOBILEB2E.NEWS_LIKES LIKES
ON LIKES.NEWS_ARTCL_ID = ARTICLES.NEWS_ARTCL_ID
WHERE
ARTICLES.ARTCL_SRC_ID = 1
group by ARTICLES.NEWS_ARTCL_ID, ARTICLES.NEWS_ARTCL_TTL_DES,
ARTICLES.NEWS_ARTCL_CNTNT_T, ARTICLES.NEWS_ARTCL_PUB_DT,
ARTICLES.NEWS_ARTCL_AUTH_NM, ARTICLES.NEWS_ARTCL_URL, ARTICLES.MEDIA_URL,
ARTICLES.ARTCL_SRC_ID, SOURCES.ARTCL_SRC_NM, MEDIA.MEDIA_TYPE_DESCRIP
ORDER BY ARTICLES.NEWS_ARTCL_PUB_DT
I also changed coma-separated list of tables from where condition to joins. I think this is more readable since table join conditions are separated from result filtering in where clause.

ms access query - Filter out values from another query

I've got a query running that pulls out the records I need.
I want to run another query that pulls out all the other records (excluding the ones in the first query).
I've read up on NOT IN and NOT LIKE but can't seem to get them to work.
The first query is named: qryHunnersPatients
Here's the code for the second query that I have so far:
Right now this is just pulling all the records - but I want to exclude those records in the qryHunnersPatients query
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID);
UPDATE:
I just tried the WHERE NOT EXISTS using the code below:
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID)
WHERE NOT EXISTS
(SELECT ID
FROM qryHunnersPatients AS hunners
WHERE hunners.ID = tblPatientHistoryBaseline.ID);
You need a SubQuery. As In understand that your Query qryHunnersPatients gives you the list of records that you do not wish to see, you need to include that in the NOT IN part of the Query.
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID)
WHERE
tblPatientHistoryBaseline.ID
NOT IN
(SELECT qryHunnersPatients.ID FROM qryHunnersPatients);
Assuming ID is unique, you can use WHERE NOT EXISTS:
SELECT {FieldList}
FROM tblPatientHistoryBaseline AS baseline
INNER JOIN tblQuestionnaires AS quest
ON (baseline.Visit = quest.Visit)
AND (baseline.ID = quest.ID);
WHERE NOT EXISTS (
SELECT ID
FROM qryHunnersPatients AS hunners
WHERE hunners.ID = baseline.ID
)
You don't need to bother using the aliases I've added to; I've just added them for readability.

SQL - Getting a column from another table to join this query

I've got the code below which displays the location_id and total number of antisocial crimes but I would like to get the location_name from a different table called location_dim be output as well. I tried to find a way to UNION it but couldn't get it to work. Any ideas?
SELECT fk5_location_id , COUNT(fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT
WHERE fk1_time_id = 3 AND fk3_crime_id = 1
GROUP BY fk5_location_id;
You want to use join to lookup the location name. The query would probably look like this:
SELECT ld.location_name, COUNT(cf.fk3_crime_id) as TOTAL_ANTISOCIAL_CRIMES
from CRIME_FACT cf join
LOCATION_DIM ld
on cf.fk5_location_id = ld.location_id
WHERE cf.fk1_time_id = 3 AND cf.fk3_crime_id = 1
GROUP BY ld.location_name;
You need to put in the right column names for ld.location_name and ld.location_id.
you need to find a relationship between the two tables to link a location to crime. that way you could use a "join" and select the fields from each table you are interested in.
I suggest taking a step back and reading up on the fundamentals of relational databases. There are many good books out there which is the perfect place to start.

sql query question inner join

LEFT JOIN PatientClinics AB ON PPhy.PatientID = AB.PatientID
JOIN Clinics CL ON CL.ID = AB.ClinicID
AND COUNT(AB.ClinicID) = 1
I get error using Count(AB.ClinicID) = 1 (ClinicID has duplicate values in the table and
I want to use only 1 value of each duplicate value of ClinicId to produce result)
What mistake am I making?
I've never seen a COUNT() being used in a JOIN before. Maybe you should use:
HAVING COUNT(AB.ClinicID) = 1
instead.
Count() can't be used as a join/filter predicate. It can be used in the HAVING clause however. You should include the entire query in order to get a better example of how to rewrite it.
maybe investigate the HAVING clause instead of using COUNT where you put it.
hard to help without the full query.