SQL Server error selecting count on a subquery - sql-server-2005

I am trying to select the count of the number of rows returned on a query. The query is
Select
a.itm_cd, max(b.doc_num) ,max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR' and a.create_dt >'21-AUG-16'
group by
a.itm_cd;
For this particular query I return 3 rows, I need to write a query that returns how many rows are returned.
I have tried this :
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b,itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd);
This results in a syntax error
Msg 102, level 15, state 1 line 1
Incorrect syntax near ')'.
I am not sure what I am doing wrong, I have a similar SQL statement that works this way in Oracle, but have not found where I am messing up in SQL Server.
UPDATE:
as per the first suggestion I received I tried :
Select
count(*)
from
(Select
a.itm_cd, max(b.doc_num), max(c.text)
from
ist b, itm_trn a, ist_cmnt c
where
a.ist_seq_num = b.ist_seq_num
and a.ist_seq_num = c.ist_seq_num
and a.ist_wr_dt = b.ist_wr_dt
and a.new_loc_cd like 'BOX115'
and a.ITT_CD = 'XFR'
and a.create_dt > '21-AUG-16'
group by
a.itm_cd) as T
The errors returned were
Msg 8155, Level 16, State 2, Line 12
No column was specified for column 2 of 'T'.
Msg 8155, Level 16, State 2, Line 12
No column was specified for column 3 of 'T'.

AS per the error message, you should add alias name for your aggregated columns. Try like this,
SELECT count(*)
FROM (
SELECT a.itm_cd
,max(b.doc_num) AS MaxDoc_num
,max(c.TEXT) AS MaxText
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'
GROUP BY a.itm_cd
) T
You can easily find the count using this query.
SELECT count(DISTINCT a.itm_cd)
FROM ist b
,itm_trn a
,ist_cmnt c
WHERE a.ist_seq_num = b.ist_seq_num
AND a.ist_seq_num = c.ist_seq_num
AND a.ist_wr_dt = b.ist_wr_dt
AND a.new_loc_cd LIKE 'BOX115'
AND a.ITT_CD = 'XFR'
AND a.create_dt > '21-AUG-16'

Related

How to Update with subquery in PostgreSQL

I have a function in MS SQL Server just like this:
UPDATE r
SET
monthly =
(
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = #type_rel_aux
AND h.hcar_day > #date_month_before
AND h.hcar_day <= #date_base
)
FROM #Month_Table r
WHERE type = 1;
and thats the result (after update):
Seq monthly
2 102471,34
1 -5129,46
3 -29841,23
4 0
But when I execute the same update in a fuction in PostgreSQL, all the rows get the same value:
UPDATE Month_Table
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base) FROM Month_Table r WHERE type = 1;
Result (after update), all the same value of Seq 3:]
Seq monthly
1 -29841,23
2 -29841,23
3 -29841,23
4 -29841,23
I don't see the cause of the problem...
Does PostgreSQL have different rules on UPDATE?
Can anyone help me?
Remove the FROM clause from Postgres:
UPDATE Month_Table r
SET variacao_mes_rs = (
SELECT SUM(-h.value_ini - h.purchase + h.sold + h.value_fin)
FROM hist_portfolio AS h
WHERE h.comp_id = r.comp_id
AND h.port_id = r.port_id
AND h.exte_id = r.cate_id
AND h.type_id = v_type_rel_aux
AND h.hcar_day > v_date_month_before
AND h.hcar_day <= v_date_base)
WHERE type = 1;
The FROM clause in an UPDATE behaves differently in the two databases, as you have discovered.

Error 1055 SQL - SELECT list is not in GROUP BY

I'm struggling with this SQL consult, the error message is:
1055 - Expression #10 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'ctrl2019.s.cgsc_cuenta' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by.
And here is the code:
SELECT c.cgcu_cuenta AS id,
g.ger_cuenta, g.ger_nombre,
p.cgp_cuenta, p.cgp_nombre,
r.rub_cuenta, r.rub_nombre,
c.cgcu_cuenta, c.cgcu_nombre,
s.cgsc_cuenta, s.cgsc_nombre,
SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0)) AS debe, 0 AS haber,
'D' AS nat_id,
c.cgcu_cuenta AS cuenta, c.cgcu_nombre AS nombre
FROM ctrl2019.cat_Genero g
INNER JOIN ctrl2019.cat_CgGrupo p USING(ger_id)
INNER JOIN ctrl2019.cat_Rubro r USING(ger_id, cgp_id)
INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
LEFT JOIN (
SELECT a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id,
SUM(IFNULL(a.msl_debe, 0)) AS debe, SUM(IFNULL(a.msl_haber, 0)) AS haber
FROM ldf.vin_EntePublicoLDF e
INNER JOIN ldf.blz_Mensual_2019 a USING(gpo_id, ur_id)
WHERE e.ejr_id = 2019
AND a.ger_id = 1
AND e.ent_id = 12
AND a.msc_id IN (0, 1, 2, 3)
GROUP BY a.ger_id, a.grp_id, a.rub_id, a.cta_id, a.sct_id
)a ON s.ger_id = a.ger_id AND s.cgp_id = a.grp_id AND s.rub_id = a.rub_id AND s.cgcu_id = a.cta_id AND s.cgsc_id = a.sct_id
WHERE g.ger_id = 1
GROUP BY g.ger_id, p.cgp_id, r.rub_id, c.cgcu_id;
I have no idea why it gives me this error, i'm new in sql.
you just need to add all the columns in GROUP BY which are present in the SELECT statement.
Seems like simple aggregation here. I cleaned up the code and added some columns to the GROUP BY.
SELECT
id = c.cgcu_cuenta
,g.ger_cuenta
,g.ger_nombre
,p.cgp_cuenta
,p.cgp_nombre
,r.rub_cuenta
,r.rub_nombre
,c.cgcu_cuenta
,c.cgcu_nombre
,s.cgsc_cuenta
,s.cgsc_nombre
,debe = SUM(IFNULL(a.debe, 0)) - SUM(IFNULL(a.haber, 0))
,haber = 0
,nat_id = 'D'
,cuenta = c.cgcu_cuenta
,nombre = c.cgcu_nombre
FROM ctrl2019.cat_Genero g
INNER JOIN ctrl2019.cat_CgGrupo p ON USING(ger_id)
INNER JOIN ctrl2019.cat_Rubro r ON USING(ger_id, cgp_id)
INNER JOIN ctrl2019.cat_CgCuenta c USING(ger_id, cgp_id, rub_id)
INNER JOIN ctrl2019.cat_CgSubcuenta s USING(ger_id, cgp_id, rub_id, cgcu_id)
LEFT JOIN (
SELECT
a2.ger_id
,a2.grp_id
,a2.rub_id
,a2.cta_id
,a2.sct_id
,debe = SUM(IFNULL(a2.msl_debe, 0))
,haber = SUM(IFNULL(a2.msl_haber, 0))
FROM ldf.vin_EntePublicoLDF E
INNER JOIN ldf.blz_Mensual_2019 a2 USING(gpo_id, ur_id)
WHERE e.ejr_id = 2019
AND a2.ger_id = 1
AND e.ent_id = 12
AND a2.msc_id IN (0, 1, 2, 3)
GROUP BY
a2.ger_id
,a2.grp_id
,a2.rub_id
,a2.cta_id
,a2.sct_id
) A
ON s.ger_id = A.ger_id
AND s.cgp_id = A.grp_id
AND s.rub_id = A.rub_id
AND s.cgcu_id = A.cta_id
AND s.cgsc_id = A.sct_id
WHERE g.ger_id = 1
GROUP BY
g.ger_id
,p.cgp_id
,r.rub_id
,C.cgcu_id
,c.cgcu_cuenta
,g.ger_cuenta
,g.ger_nombre
,p.cgp_cuenta
,p.cgp_nombre
,r.rub_cuenta
,r.rub_nombre
,c.cgcu_nombre
,s.cgsc_cuenta
,s.cgsc_nombre

ORA-00904: invalid identifier but table and column names are correct?

I have the below SQL query but it gives the error message ORA-00904: "KUST_ADR"."KU_NR": invalid identifier even though those are the correct table and column names. What else could be the cause?
update auf_adr
set email = (select k.ku_email
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
)
where auf_adr.adr_art = 2 and
exists (select 1
from auf_kopf k join
kust_adr ka
on k.kunr = ka.ku_nr
where auf_adr.auf_nr = k.auf_nr and
ka.ku_adr_art = 1 and
auf_adr.email <> ka.ku_email and
(select sum(s.rg_anz)
from auf_stat s
where s.auf_nr = k.auf_nr
) = 0
);
There is a "and" missing after each of the "where" clause line, this could be the issue.
where auf_adr.auf_nr = k.auf_nr AND

query is returning " single-row subquery returns more than one row"

I am not sure why the below query is giving the " single-row subquery returns more than one row" please let me know if I am missing anything.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END
The error message advise very clearly, more than one row are returned from the sub-query after the key word "Then". You can add "Top 1" after the first SELECT keyword for SQL Serve4.
Or get the sub query end with "and rownum =1" for Oracle. The following is for SQL Server.
CASE
WHEN s.servprov_gid = 'IFFCO.CAR-60041'
THEN
(SELECT TOP 1 E.EQUIPMENT_NUMBER
FROM S_EQUIPMENT SE,
EQUIPMENT E,
SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
SHIPMENT S
WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
AND SE.S_EQUIPMENT_GID = SSEJ.S_EQUIPMENT_GID
AND E.EQUIPMENT_GID = SE.EQUIPMENT_GID
AND SSEJ.SHIPMENT_GID = S.SHIPMENT_GID
)
ELSE
(SELECT MIN(se.equipment_number)
FROM shipment_s_equipment_join ssej,
s_equipment se
WHERE ssej.shipment_gid = 'IFFCO/LOGISTICS.L171203007'
AND ssej.s_equipment_gid = se.s_equipment_gid
AND se.equipment_number IS NOT NULL
)
END

Union two select statements with NOT IN - Oracle

I have two select statements and i need to output 1st select records and the other should receive the records that not output by select 1. 2nd select should not contain the data of select 1. here is my code
select b.tcl_tcserno, b.tcl_clmcode, b.tcl_clname, c.prd_desc, e.eqt_desc,
b.tcl_conamount, b.tcl_intrate, f.numirp_minrate, f.dblirp_intrate,
b.tcl_prdcode, e.eqt_type
FROM leaseinfo.trn_ira_intreductapproval a,
leaseinfo.tbltrialcalculation b,
corpinfo.tblproduct c,
leaseinfo.tbltrialequipment d,
leaseinfo.tblequipmenttype e,
leaseinfo.ref_irp_inerestratepara f
WHERE a.numira_tcserno = b.tcl_tcserno
AND b.tcl_prdcode = c.prd_code
AND b.tcl_tcserno = d.teq_tcserno
AND b.tcl_prdcode = e.eqt_prdcode
AND d.teq_eqttype = e.eqt_type
AND b.tcl_prdcode = f.strirp_productcode
AND d.teq_eqttype = f.strirp_eqpttype
AND a.strira_status = 'E'
AND (f.numirp_minrate - b.tcl_intrate)<
(SELECT g.intds_uplim
FROM glinfo.ref_tblintratefordesignation g
WHERE g.intds_designation IN (
SELECT s.str_off_type
FROM dpg.inf_responsible_maildetails s
WHERE s.str_user_code = '10020336'))
and (b.tcl_prdcode='LE' OR b.tcl_prdcode='UV' OR b.tcl_prdcode='HP')
and (e.EQT_TYPE='2' OR e.EQT_TYPE='9' OR e.EQT_TYPE='15' OR e.EQT_TYPE='17' OR e.EQT_TYPE='21' OR e.EQT_TYPE='23' OR e.EQT_TYPE='25' OR e.EQT_TYPE='28' OR e.EQT_TYPE='30')
ORDER BY b.tcl_tcserno
union all
SELECT b.tcl_tcserno, b.tcl_clmcode, b.tcl_clname, c.prd_desc, e.eqt_desc,
b.tcl_conamount, b.tcl_intrate, f.numirp_minrate, f.dblirp_intrate,
b.tcl_prdcode, e.eqt_type
FROM leaseinfo.trn_ira_intreductapproval a,
leaseinfo.tbltrialcalculation b,
corpinfo.tblproduct c,
leaseinfo.tbltrialequipment d,
leaseinfo.tblequipmenttype e,
leaseinfo.ref_irp_inerestratepara f
WHERE a.numira_tcserno = b.tcl_tcserno
AND b.tcl_prdcode = c.prd_code
AND b.tcl_tcserno = d.teq_tcserno
AND b.tcl_prdcode = e.eqt_prdcode
AND d.teq_eqttype = e.eqt_type
AND b.tcl_prdcode = f.strirp_productcode
AND d.teq_eqttype = f.strirp_eqpttype
AND a.strira_status = 'E'
ORDER BY b.tcl_tcserno
i think not in will work with this. but i don't have an idea to do with it. any help would be appreciated
This will do job using set notation. Assumes same columns in both queries to work
With dat1 as (....),
Dat2 as (....)
Select * from dat1 Union all
(
Select * from dat2 except select * from dat1
)