The sql syntax is following:
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y, bas1.assignments k, bas1.topics a
WHERE y.ID = k.student_id AND k.topic_id = a.id
WHERE a.NAME = 'History';
But I get some errors, any Ideas?
You're using WHERE twice
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y, bas1.assignments k, bas1.topics a
WHERE y.ID = k.student_id AND k.topic_id = a.id
AND a.NAME = 'History';
change to this
Don't you need join statements? E.g.,
SELECT y.NAME, y.EMAIL, k.DATE, k.WORK_NR, k.SCORE, k.FILENAME
FROM bas1.students y JOIN bas1.assignments k ON y.ID = k.student_id
JOIN bas1.topics a ON k.topic_id = a.id
WHERE a.NAME = 'History';
Related
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
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
)
Whenever I add the indented section of code listed below (LastTerm) the error ORA-01427 Single-Row Subquery Returns More Than One Row is returned by SQL Tools. If I removed this from section of code from my list, it runs and correctly pulls the data that I ask for from the tables. What do I need to change or fix to get this to run with that LastTerm section in the code instead of receiving that error? I'm not sure what I am doing wrong and I am not sure why the other two, Major and Standing, do run.
Any help with this would be greatly appreciated.
SELECT DISTINCT (c.sfrstcr_pidm)PIDM,
substr(frkiden.f_get_spriden_id(c.sfrstcr_pidm),1,9) ID,
substr(frkiden.f_get_spriden_first_name(c.sfrstcr_pidm),1,15) FIRST,
substr(frkiden.f_get_spriden_last_name(c.sfrstcr_pidm),1,60) LAST,
sovclas_clas_code,shrlgpa_gpa,spbpers_ethn_code,spbpers_sex,
(select x. sgbstdn_majr_code_1
from sgbstdn x where x.sgbstdn_pidm = c.sfrstcr_pidm
and x.sgbstdn_term_code_eff =
(select max(y. sgbstdn_term_code_eff) from sgbstdn y
where x. sgbstdn_pidm = y. sgbstdn_pidm
and y. sgbstdn_majr_code_1 is not null)) Major,
(select x.shrttrm_astd_code_end_of_term
from shrttrm x where x.shrttrm_pidm = c.sfrstcr_pidm
and x.shrttrm_term_code =
(select max(y.shrttrm_term_code) from shrttrm y
where x.shrttrm_pidm = y.shrttrm_pidm
and y.shrttrm_astd_code_end_of_term is not null)) Standing,
(select x.sfrstcr_term_code
from sfrstcr x where x.sfrstcr_pidm = c.sfrstcr_pidm
and x.sfrstcr_term_code =
(select max(y.sfrstcr_term_code) from sfrstcr y
where x.sfrstcr_pidm = y.sfrstcr_pidm
and y.sfrstcr_term_code IS NOT NULL )) LastTerm
FROM
sovclas,spbpers,shrlgpa,sfrstcr c
LEFT JOIN goremal
ON c.sfrstcr_pidm = goremal_pidm
AND goremal_emal_code = 'UNIV'
AND goremal_status_ind != 'I'
WHERE
c.sfrstcr_pidm = shrlgpa_pidm
AND c.sfrstcr_term_code = sovclas_term_code
AND c.sfrstcr_pidm = sovclas_pidm
AND c.sfrstcr_pidm = spbpers_pidm
AND shrlgpa_levl_code = 'UG'
AND shrlgpa_gpa_type_ind = 'O'
AND c.sfrstcr_term_code = sovclas_term_code
AND c.sfrstcr_term_code = '201510'
AND c.sfrstcr_rsts_code IN ('RE','RW','RL')
AND sovclas_clas_code NOT IN ('GR','ES')
AND c.sfrstcr_pidm IN
(SELECT DISTINCT(a.shrttrm_pidm)
FROM shrttrm a
WHERE a.shrttrm_astd_code_end_of_term IN ('S1','S2','S3','S4')
AND a.shrttrm_term_code =
(SELECT Max(b.shrttrm_term_code)
FROM shrttrm b
WHERE b.shrttrm_pidm = a.shrttrm_pidm AND
b.shrttrm_term_code < 201510 )) -- Change the code to the latest term without standing
AND c.sfrstcr_pidm IN
(SELECT DISTINCT(a.sgbstdn_pidm)
FROM sgbstdn a
WHERE a.sgbstdn_levl_code != 'GR'
AND a.sgbstdn_styp_code NOT IN ('L')
AND a.sgbstdn_term_code_eff =
(SELECT Max(b.sgbstdn_term_code_eff)
FROM sgbstdn b
WHERE b.sgbstdn_pidm = a.sgbstdn_pidm))
AND c.sfrstcr_pidm IN
(SELECT DISTINCT(a.sfrstcr_pidm)
FROM sfrstcr a
WHERE a.sfrstcr_rsts_code IS NOT NULL
AND a.sfrstcr_term_code =
(SELECT Max(b.sfrstcr_term_code)
FROM sfrstcr b
WHERE b.sfrstcr_pidm = a.sfrstcr_pidm AND
b.sfrstcr_term_code < '201510'))
Try adding (rownum = 1) like this:
(select x.shrttrm_astd_code_end_of_term
from shrttrm x where x.shrttrm_pidm = c.sfrstcr_pidm
and x.shrttrm_term_code =
(select max(y.shrttrm_term_code) from shrttrm y
where x.shrttrm_pidm = y.shrttrm_pidm
and y.shrttrm_astd_code_end_of_term is not null
and rownum = 1)) Standing
(select max(x.sfrstcr_term_code)
from sfrstcr x where x.sfrstcr_pidm = c.sfrstcr_pidm
and x.sfrstcr_term_code =
(select max(y.sfrstcr_term_code) from sfrstcr y
where x.sfrstcr_pidm = y.sfrstcr_pidm
and y.sfrstcr_term_code IS NOT NULL )) LastTerm
How can I get the sum of capacities.capacity ?
#capacitysum = User.joins("left join capacities c on users.id = c.user_id")
.where('c.created_at = (SELECT MAX(c.created_at) FROM c WHERE c.user_id = users.id)')
.where('users.role = ?', 'consultant')
I tried adding a .sum('c.capacity'), .sum(:capacity) and .sum('capacities.capacity') with no success...
Try with add a select statement :
#capacitysum = User.joins("left join capacities c on users.id = c.user_id")
.where('c.created_at = (SELECT MAX(c.created_at) FROM c WHERE c.user_id = users.id)')
.where('users.role = ?', 'consultant').select("sum(c.capacity)")
Trying to update the fact table with late coming dimension data. See Code below
UPDATE FactVehicleStock
SET
FactVehicleStock.[TOL_BidDateTime] = B.Bid_Date_and_Time,
FactVehicleStock.[TOL_AuctionDate] = B.Date_opened_for_tender,
FactVehicleStock.[TOL_OriginalLoadDate] = B.Original_Load_date,
FactVehicleStock.[TOL_ServiceHistory] = B.Service_History,
FactVehicleStock.[TOL_ReservedPrice] = B.Reserve_price,
FactVehicleStock.[TOL_BidPrice] = B.Bid_Price,
FactVehicleStock.[TOL_OriginalReservedPrice] = B.Original_Reserve_Price,
FactVehicleStock.[TOL_NoOfTimesReloaded] = B.Number_of_times_reloaded
FROM BMR_DWH.dbo.FactVehicleStock as A
INNER JOIN BMR_STAGE.dbo.STG_Traders_Online as B
ON A.StockbookNumber = B.Stock_Number
INNER JOIN BMR_DWH.[dbo].[DimDealership] as C
ON A.DEALERSHIP_KEY IN (SELECT Distinct [DEALERSHIP_KEY]
FROM BMR_DWH.[dbo].[DimDealership]
INNER JOIN [BMR_STAGE].[dbo].[STG_Traders_Online] E
ON LTRIM(RTRIM(C.MOLNUMBER)) = LTRIM(RTRIM(E.MOL_Number))
)
Try to use the right UPDATE SELECT Syntaxe.
If you look at the code I did, I had to change a bit your query to do the joins. See if it fits to you.
UPDATE BMR_DWH.dbo.FactVehicleStock as a
INNER JOIN BMR_STAGE.dbo.STG_Traders_Online as B
ON A.StockbookNumber = B.Stock_Number
INNER JOIN
(SELECT Distinct [DEALERSHIP_KEY]
FROM BMR_DWH.[dbo].[DimDealership] as C
INNER JOIN [BMR_STAGE].[dbo].[STG_Traders_Online] E
ON LTRIM(RTRIM(C.MOLNUMBER)) = LTRIM(RTRIM(E.MOL_Number)) ) D
ON A.DEALERSHIP_KEY = D.DEALERSHIP_KEY
SET
FactVehicleStock.[TOL_BidDateTime] = B.Bid_Date_and_Time,
FactVehicleStock.[TOL_AuctionDate] = B.Date_opened_for_tender,
FactVehicleStock.[TOL_OriginalLoadDate] = B.Original_Load_date,
FactVehicleStock.[TOL_ServiceHistory] = B.Service_History,
FactVehicleStock.[TOL_ReservedPrice] = B.Reserve_price,
FactVehicleStock.[TOL_BidPrice] = B.Bid_Price,
FactVehicleStock.[TOL_OriginalReservedPrice] = B.Original_Reserve_Price,
FactVehicleStock.[TOL_NoOfTimesReloaded] = B.Number_of_times_reloaded