I am performing the following query having some syntax error:
SELECT count (tbl_staff.staff_id as staff_number),SELECT count (tbl_client.client_id as client_number),SELECT count (tbl_appointment.appt_id as appt_number),SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
LEFT JOIN tbl_staff
ON (
tbl_staff.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_appointment
ON (
tbl_appointment.merchant_id = tbl_subscription.merchant_id)
LEFT JOIN tbl_client
ON (
tbl_client.merchant_id = tbl_subscription.merchant_id)
WHERE tbl_subscription.subscription_id=1;
I want get the count of staff_id, client_d, appointment_id on particular Subscription_id.
Your select list is close, but has a few mistakes. Namely, you only need a single SELECT in your query (not one per field) and the "as ..." descriptor belongs outside the parenthesis.
So this part of the query
SELECT count (tbl_staff.staff_id as staff_number),
SELECT count (tbl_client.client_id as client_number),
SELECT count (tbl_appointment.appt_id as appt_number),
SELECT count (tbl_subscription.subscription_id as subscription_number)
FROM tbl_subscription
would become
SELECT count (tbl_staff.staff_id) as staff_number,
count (tbl_client.client_id) as client_number,
count (tbl_appointment.appt_id) as appt_number,
count (tbl_subscription.subscription_id) as subscription_number
FROM tbl_subscription
Related
Is it possible to use two "select" in the same query?
I tried it but got the syntax error several times.
My query example:
SELECT
comp.id,
comp.document,
comp.dateStart,
comp.companyName,
comp.fantasyName,
comp.legalNature,
comp.mainActivity,
comp.situation,
comp.shareCapital,
comp.idCompanyStatus,
pp.userCredentialId,
uc.name,
cs.name AS 'nameStatus',
cs.color AS 'colorStatus',
cs.description,
comp.idPurchasedProduct,
comp.actived,
comp.createAt,
comp.updateAt,
comp.phone
FROM `PurchasedProduct` pp
INNER JOIN
`Company` comp on comp.idPurchasedProduct = pp.id
INNER JOIN
`UserCustomer` uc on pp.userCredentialId = uc.credentialId
INNER JOIN
`CompanyStatus` cs on cs.id = comp.idCompanyStatus
WHERE
comp.actived = 1
LIMIT 0,5;
SELECT COUNT(id) AS totalItems, CEILING(COUNT(id) / 10) AS totalPages FROM Company;
I would like the result shown to be all queries on the screen.
Basically, what I want is that the result shown when executing the query is the first and second "select" together.
I really don't know how or don't understand how to do this.
Example:
first result with seconde result
I want to show both results at once.
The documents is fake, not real. Only for demo.
You should be able to do by having the second query as its own JOIN query. Since there is no group by, it is only returning a single row. By no join condition, the value will be available for every row otherwise. So you SHOULD be able to get by doing
select
[ all your other columns ],
JustCounts.TotalItems,
JustCounts.TotalPages
from
[PurchasedProduct and all your other joins]
JOIN ( SELECT
COUNT(id) AS totalItems,
CEILING(COUNT(id) / 10) AS totalPages
FROM Company ) as JustCounts
where
[rest of your original query]
I have a sql query based on 2 IDs in the same table, the results of the sum have come out correctly, but the problem is group by cannot handle data that appears 2 times
SELECT coa_a.debet_april,coa_a.namacoaapril, coa_b.kredit_april ,coa_b.namacoaapril
FROM `t_jurnalumum` join coa on coa.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(a.Nilai) as debet_april, coa.namacoa as namacoaapril
from t_jurnalumum a j
join coa on a.IdDebet=coa.m_coa_4_id
where year (a.Tanggal)=2021 and month (a.Tanggal)=4
GROUP by a.IdDebet ) as coa_a on coa_a.m_coa_4_id=t_jurnalumum.IdDebet
join (select DISTINCT m_coa_4_id, sum(b.Nilai) as kredit_april, coa.namacoa as namacoaapril
from t_jurnalumum b
join coa on b.IdKredit=coa.m_coa_4_id
where year (b.Tanggal)=2021 and month (b.Tanggal)=4
GROUP by b.IdKredit ) as coa_b on coa_b.m_coa_4_id=t_jurnalumum.IdKredit
GROUP by coa_b.namacoaapril, coa_a.namacoaapril
this the result
and this is the main table
It is because you are using the same table. The first sub query of join will give you the distinct value from first group by and second group by gives distinct value. And at the end you also have grouped the whole result. The first one gives two distinct value and second one gives two distinct value which eventually gives all value.
I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.
I have two procedures that calculate two data. Now i would like to get the sum of those two output. Is it possible in sql?
ex:
Select bio.*,
SUM (cnt_report,cnt_report_2) as TOTAL --CAN I DO THIS?
cnt_report + cnt_report_2 as Total --This doesn't seem to work
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11 ON bio.id = cr11.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11 ON bio.id = cr11.id2
Your Sum function will work with a little tweak, but you will need to add a GROUP BY line at the end. Also, you seem to have named your two sub queries with the same alias. Try this:
Select bio.*,
SUM (cr11_1.cnt_report + cr11_2.cnt_report_2) as TOTAL
from biographical bio
LEFT JOIN (
SELECT cr.id, COUNT (*) AS cnt_report
FROM report cr
GROUP BY cr.id
) cr11_1 ON bio.id = cr11_1.id
LEFT JOIN (
SELECT cr.id2,
COUNT (*) AS cnt_report_2
FROM report cr
GROUP BY cr.id2
) cr11_2 ON bio.id = cr11_2.id2
GROUP BY bio.*
You can not do this:
sum(something, something_else)
but you can do this:
sum(something + something_else)
You can figure out how to implement that into your query. Remember that if you are selecting other fields along with an aggregate, you need a group by clause.
I have a function that gets a SQL code and inserts a count field in it and executes the query to return the number of rows in it. The objective is to have a dynamic SQL code and be able to get its record count no matter what code it has, because I use it in a registry filter window and I never know what code may be generated, because the user can add as many filters as he/she wants.
But as I use the group by clause, the result is wrong because it is counting the number of times a main registry appears because of the use on many join connections.
The result of that code above should only one row with a columns with 10 as result, but I get a new table with the first columns with a 2 in the first row and a 1 on the other rows.
If I take off the group by clause I will receive a 11 as a count result, but the first row will be counted twice.
What should I do to get a single row and the correct number?
SELECT
COUNT(*) QUERYRECORDCOUNT, // this line appears only in the Count() function
ARTISTA.*,
CATEGORIA.NOME AS CATEGORIA,
ATIVIDADE.NOME AS ATIVIDADE,
LOCALIDADE.NOME AS CIDADE,
MATRICULA.NUMERO AS MAP
FROM
ARTISTA
LEFT JOIN PERFIL ON PERFIL.REGISTRO = ARTISTA.ARTISTA_ID
LEFT JOIN CATEGORIA ON CATEGORIA.CATEGORIA_ID = PERFIL.CATEGORIA
LEFT JOIN ATIVIDADE ON ATIVIDADE.ATIVIDADE_ID = PERFIL.ATIVIDADE
LEFT JOIN LOCALIDADE ON LOCALIDADE.LOCALIDADE_ID = ARTISTA.LOCAL_ATIV_CIDADE
LEFT JOIN MATRICULA ON MATRICULA.REGISTRO = ARTISTA.ARTISTA_ID
WHERE
((ARTISTA.SIT_PERFIL <> 'NORMAL') AND (ARTISTA.SIT_PERFIL <> 'PRIVADO'))
GROUP BY
ARTISTA.ARTISTA_ID
ORDER BY
ARTISTA.ARTISTA_ID;
This always gives you the number of rows for any query you have:
Select count(*) as rowcount from
(
Paste your query here
) as countquery
Since your are GROUPING BY ARTISTA.ARTISTA_ID, COUNT(*) QUERYRECORDCOUNT will return records count for each ARTISTA.ARTISTA_ID value.
If you want GLOBAL count, then you need to use a nested query:
SELECT COUNT(*) AS QUERYRECORDCOUNT
FROM (SELECT
ARTISTA.*,
CATEGORIA.NOME AS CATEGORIA,
ATIVIDADE.NOME AS ATIVIDADE,
LOCALIDADE.NOME AS CIDADE,
MATRICULA.NUMERO AS MAP
FROM
ARTISTA
LEFT JOIN PERFIL ON PERFIL.REGISTRO = ARTISTA.ARTISTA_ID
LEFT JOIN CATEGORIA ON CATEGORIA.CATEGORIA_ID = PERFIL.CATEGORIA
LEFT JOIN ATIVIDADE ON ATIVIDADE.ATIVIDADE_ID = PERFIL.ATIVIDADE
LEFT JOIN LOCALIDADE ON LOCALIDADE.LOCALIDADE_ID = ARTISTA.LOCAL_ATIV_CIDADE
LEFT JOIN MATRICULA ON MATRICULA.REGISTRO = ARTISTA.ARTISTA_ID
WHERE
((ARTISTA.SIT_PERFIL <> 'NORMAL') AND (ARTISTA.SIT_PERFIL <> 'PRIVADO'))
GROUP BY
ARTISTA.ARTISTA_ID
ORDER BY
ARTISTA.ARTISTA_ID);
In this case, you may not need to select those many columns.
If you need to retrieve the all records count with details, then better to use two separate queries.