I'm trying to add all the values in a column to get the total of that column, anyone know the trick to this?
Here's my query:
select T.*,
sum(nvl(BIN1,0))+sum(nvl(BIN2,0))+sum(nvl((BIN3,0)) as TOTAL
from
(
SELECT CSM_FLASKS.FLASK_CODE,
MN_ASSAYS_STAGES.ASSAY_STAGE_ID,
VITRO_REP.TREATMENT_DOSE(CSM_FLASKS.GROUP_ID) AS TREATMENT_DOSE,
MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 1) AS BIN1,
MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 2) AS BIN2,
MN_RI_REP.FLASK_BIN_VALUE_BY_INDEX(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 3) AS BIN3,
MN_RI_REP.CBPI_FLASK(MN_ASSAYS_STAGES.ASSAY_STAGE_ID, CSM_FLASKS.FLASK_ID, 1, 2, 2) AS CBPI,
CSM_EXPERIMENTS.EXPT_CODE
FROM CSM_TREATMENT_GROUPS_EXPTS
INNER JOIN CSM_ASSAYS
ON CSM_ASSAYS.EXPT_ID = CSM_TREATMENT_GROUPS_EXPTS.EXPERIMENT_ID
INNER JOIN CSM_FLASKS
ON CSM_TREATMENT_GROUPS_EXPTS.GROUP_ID = CSM_FLASKS.GROUP_ID
INNER JOIN MN_ASSAYS
ON CSM_ASSAYS.ASSAY_ID = MN_ASSAYS.ASSAY_ID
INNER JOIN MN_ASSAYS_STAGES
ON MN_ASSAYS.MN_ASSAY_ID = MN_ASSAYS_STAGES.MN_ASSAY_ID
INNER JOIN CSM_EXPERIMENTS
ON CSM_EXPERIMENTS.EXPT_ID = CSM_TREATMENT_GROUPS_EXPTS.EXPERIMENT_ID
AND CSM_EXPERIMENTS.EXPT_ID = CSM_ASSAYS.EXPT_ID
WHERE CSM_ASSAYS.ASSAY_ID = 1000060
AND MN_ASSAYS_STAGES.STAGE_ID = 2
ORDER BY CSM_TREATMENT_GROUPS_EXPTS.ORDER_INDEX,
CSM_FLASKS.FLASK_ID,
CSM_FLASKS.IS_PLUS DESC
) T
Error:
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
*Cause:
*Action:
Error at Line: 1 Column: 9
When using any aggregate function, you will need to add the non aggregated fields into a GROUP BY clause. So in your column you need to list those fields that are not listed in the aggregate function SUM in the GROUP BY clause. Therefore your query should be as following:
SELECT FLASK_CODE,
SUM(nvl(BIN1, 0)) + SUM(nvl(BIN2, 0)) + SUM(nvl((BIN3, 0)) AS TOTAL
FROM
(
...
) T
GROUP BY FLASK_CODE
This is just an example. But you have to determine what fields to select in the SELECT clause and needs to be listed too in GROUP BY clause.
Try This:
If You add all Data in single column means:
select sum( column_name ) from table inner join .........;
Finally you find the total
Related
I have this query:
SELECT
MAX(P.Descricao) AS Name,
SUM(FC.Credito) AS Sales,
CAST(CASE
WHEN FC.Troco = 1 THEN SUM(FC.Debito)
ELSE 0
END AS decimal(10, 2)) AS Purchases
FROM
FluxoCaixa FC
INNER JOIN
Pagamento P ON FC.ID_Pagamento = P.Pagamento
I'm getting this error:
Column 'FluxoCaixa.Troco' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I know that an error is returning because the column FC.Troco is not aggregated, but how do I aggregate a column whose values are bit?
In the CASE-WHEN, I want to SUM(FC.Debito) case FC.Troco be 1(true).
I think you want conditional aggregation:
SELECT MAX(P.Descricao) AS Name, SUM(FC.Credito) AS Sales,
SUM(CASE WHEN FC.Troco = 1 THEN FC.Debito ELSE 0 END) AS Purchases
FROM FluxoCaixa FC INNER JOIN
Pagamento P
ON Fc.ID_Pagamento = P.Pagamento;
Here is my query and I want to add the "count of SalID group by OFID" and store the result in the same table.
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
T_OF_User.SalID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
I tried this:
SELECT
T_OF.OFID,
T_OF.OFDateDPrev, T_OF.OFDateFPrev,
T_OF_User.OFUserID,
Count (SalID) FROM T_OF_User GROUP BY OFID
INTO T_tracing
FROM T_OF
INNER JOIN T_OF_User
ON T_OF_User.OFID = T_OF.OFID
But I have an error message. Any help please?
I think you want a window function:
SELECT T_OF.OFID, T_OF.OFDateDPrev, T_OF.OFDateFPrev, T_OF_User.OFUserID,
Count(SalID) OVER (PARTITION BY T_OF.OFID) as cnt
INTO T_tracing
FROM T_OF JOIN
T_OF_User
ON T_OF_User.OFID = T_OF.OFID;
You also need to give the result of the expression a name for T_Tracing.
SELECT Name,'1','2','3'
FROM (
SELECT
database_jobdefect.defect_id,
database_session.name,
count(*) AS cu
FROM
database_session IN ner
JOIN database_job ON database_job.session_id = database_session.id
INNER JOIN database_jobdefect ON database_job.id = database_jobdefect.job_id
GROUP BY
database_session.name,
database_jobdefect.defect_id) PIVOT (count(database_session.name)
FOR database_jobdefect.defect_id IN ([1],[2],[3]))
The error i am getting is
"ProgrammingError: syntax error at or near "(" LINE 1:
...on.name,database_jobdefect.defect_id) PIVOT (count(database_..."
Postgres doesn't support pivot syntax. Just use conditional aggregation:
SELECT Name,
COUNT(*) FILTER (WHERE djd.defect_id = 1) THEN defect_1,
COUNT(*) FILTER (WHERE djd.defect_id = 2) THEN defect_2,
COUNT(*) FILTER (WHERE djd.defect_id = 3) THEN defect_3
FROM database_session ds JOIN
database_job dj
ON dj.session_id = ds.id JOIN
database_jobdefect djd
ON dj.id = djd.job_id
GROUP BY ds.name;
I tried to show staff name, purchase date and count of transactions.
Below is my code; and I get this error:
Msg 147, Level 15, State 1, Line 260
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Code:
select
mt.StaffName,
mp.PurchaseDate,
[TotalTransaction] = count(mp.PurchaseID)
from
MsStaff mt
join
MsPurchase mp on mt.staffid = mp.staffid
where
mt.staffname like '%o%'
and count(mp.PurchaseID) > 1
You need to use GBH — GROUP BY and HAVING clauses.
SELECT mt.StaffName,
mp.PurchaseDate,
[TotalTransaction] = COUNT(mp.PurchaseID)
FROM MsStaff mt JOIN MsPurchase mp ON mt.staffid = mp.staffid
WHERE mt.staffname LIKE '%o%'
GROUP BY mt.StaffName, mp.PurchaseDate
HAVING COUNT(mp.PurchaseID) > 1
Note that COUNT(mp.PurchaseID) only counts the non-NULL values in the mp.PurchaseID column. It is slower than COUNT(*) unless the optimizer can determine that there are no NULL values in mp.PurchaseID.
I have the following CrossTab query
TRANSFORM Max(VWDRSSTA.DATUM_ZEIT) AS MaxOfDATUM_ZEIT
SELECT VWDRSSTA.ANTRAGSNUMMER
,IIF(VWDRSSTA.SYSTEM = 'VS', (
SELECT (Max(VWDRSSTA.DUNKEL)) AS d
FROM VWDRSSTA
), NULL) AS Dunkel
,Max(VWDRSSTA.VERS_NR_INT) AS Versicherungsnummer
FROM VWDRSSTA
INNER JOIN V_NAMES ON (VWDRSSTA.SYSTEM = V_NAMES.SYSTEM_CODE)
AND (VWDRSSTA.EREIGNIS = V_NAMES.EREIGNIS)
GROUP BY VWDRSSTA.ANTRAGSNUMMER
ORDER BY VWDRSSTA.ANTRAGSNUMMER
PIVOT V_NAMES.MAPPED_NAME;
which gives me the error "Multi-level GROUP BY clause is not allowed in a subquery". Where am I going wrong with the code?
Try with VWDRSSTA.SYSTEM in the GROUP BY clause.
It should do the trick.
Edit to detail my answer :
TRANSFORM Max(VWDRSSTA.DATUM_ZEIT) AS MaxOfDATUM_ZEIT
SELECT VWDRSSTA.ANTRAGSNUMMER
,IIF(VWDRSSTA.SYSTEM = 'VS', (
SELECT (Max(VWDRSSTA.DUNKEL)) AS d
FROM VWDRSSTA
), NULL) AS Dunkel
,Max(VWDRSSTA.VERS_NR_INT) AS Versicherungsnummer
FROM VWDRSSTA
INNER JOIN V_NAMES ON (VWDRSSTA.SYSTEM = V_NAMES.SYSTEM_CODE)
AND (VWDRSSTA.EREIGNIS = V_NAMES.EREIGNIS)
GROUP BY VWDRSSTA.ANTRAGSNUMMER, VWDRSSTA.SYSTEM
ORDER BY VWDRSSTA.ANTRAGSNUMMER
PIVOT V_NAMES.MAPPED_NAME;
You cross-tab query contains a secondary "aggregated" SQL and this is not allowed in ACCESS Cross-tab query.
change the Select max(vwdrssta.dunkel) ... part to DMax("dunkel", "vwdrssta") which will get rid of that error message.