My query is as follows, and contains a subquery within it:
SELECT
dbo.Lawsuit.LawsuitNUM, dbo.Lawsuit.LawsuitYear,
dbo.Groups.GroupName, dbo.LawsuitType.LawsuitType,
dbo.Courts.CourtName,
(select
LawsuitID, DOJ, NextMeeting, ReceiptNUM, ExportNUM, ExportDate
from
(select
dbo.LawsuitExport.LawsuitID,
dbo.LawsuitExport.DOJ,
dbo.LawsuitExport.NextMeeting,
dbo.LawsuitExport.ReceiptNUM,
dbo.LawsuitExport.ExportNUM,
dbo.LawsuitExport.ExportDate,
row_number() over(partition by dbo.LawsuitExport.LawsuitID
order by dbo.LawsuitExport.ExportDate desc) as rn
from
dbo.LawsuitExport) as T
where
rn = 1)
FROM
dbo.Courts
INNER JOIN
dbo.LawsuitType ON dbo.Courts.CourtID = dbo.LawsuitType.CourtID
INNER JOIN
dbo.Groups ON dbo.LawsuitType.LawsuitTypeID = dbo.Groups.LawsuitTypeID
INNER JOIN
dbo.Lawsuit ON dbo.Groups.GroupID = dbo.Lawsuit.GroupID
INNER JOIN
dbo.LawsuitExport ON dbo.Lawsuit.LawsuitID = dbo.LawsuitExport.LawsuitID
The error I am receiving is:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS
The line
select LawsuitID,DOJ,NextMeeting,ReceiptNUM,ExportNUM,ExportDate doesnt work because you can only have 1 item returned from a sub query when it is trying to be used in another select statement. For example
SELECT X FROM Y GOOD
SELECT X, (SELECT A,B,C FROM FOO) FROM Y NOT GOOD
A,B,C cannot be mapped to 1 single element so that is invalid
If that's what is your intention then consider modifying your query like
SELECT
dbo.Lawsuit.LawsuitNUM, dbo.Lawsuit.LawsuitYear,
dbo.Groups.GroupName, dbo.LawsuitType.LawsuitType,
dbo.Courts.CourtName,XXX.LawsuitID, XXX.DOJ, XXX.NextMeeting, XXX.ReceiptNUM, XXX.ExportNUM, XXX.ExportDate
FROM
dbo.Courts
INNER JOIN
dbo.LawsuitType ON dbo.Courts.CourtID = dbo.LawsuitType.CourtID
INNER JOIN
dbo.Groups ON dbo.LawsuitType.LawsuitTypeID = dbo.Groups.LawsuitTypeID
INNER JOIN
dbo.Lawsuit ON dbo.Groups.GroupID = dbo.Lawsuit.GroupID
INNER JOIN
dbo.LawsuitExport ON dbo.Lawsuit.LawsuitID = dbo.LawsuitExport.LawsuitID
INNER JOIN (select
LawsuitID,
DOJ,
NextMeeting,
ReceiptNUM,
ExportNUM,
ExportDate,
row_number() over(partition by LawsuitID
order by ExportDate desc) as rn
from
dbo.LawsuitExport) XXX ON dbo.Lawsuit.LawsuitID = XXX.LawsuitID
WHERE XXX.rn = 1;
Related
I have an employee table called resources. the resources table has info about the resources and their managers along with their levels.
I need a query to always return the L7 manager (not the immediate manager)
This is what I've come up with so far.
But the problem in this is because of the where condition where mananger_level = 7 and Im joining cte with resource_alias, a few resources are getting filtered out
with recursive cte (resource_alias, manager_alias, manager_level) as (
select resource_alias,
manager_alias,
manager_level
from rpt.resources
union all
select r.resource_alias,
r.manager_alias,
r.manager_level
from rpt.resources r
inner join cte
on r.resource_alias = cte.manager_alias
)
select rr.resource_full_name,
m.resource_id,
rr.manager_level,
m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
r.eligible_program_id,
rr.manager_alias,
rr.manager_name,
m.skip_reason
from rpt.model_input_resources m
join rpt.resource_pool r on m.resource_alias = r.resource_alias
join rpt.programs p on find_in_set(p.id, r.eligible_program_id) > 0
join rpt.resources rr on rr.resource_alias = r.resource_alias
left join cte on cte.resource_alias = rr.resource_alias
where m.run_id in ('202301291674972257') and cte.manager_level = 7
and m.skip_reason <> 'Good' and r.portfolio in ('NACF') and r.resource_type in ('Launch Manager') and
m.resource_id not in ( select distinct(resource_id) from rpt.project_resource_recommendation_runs where run_id in ('202301291674972257') ) -- and p.program in (?)
group by m.resource_alias,
r.resource_type,
r.resource_home_city,
r.resource_home_state,
r.resource_home_country,
r.resource_home_zip,
rr.manager_alias,
m.skip_reason
I have the following query:
SELECT
p.fsym_id,
b.p_co_sec_name_desc AS Company_Name,
p.p_date,
p.p_price AS Unadjusted_Price,
b.region AS Region,
f_splitadjprice(p.fsym_id,p.p_date,p.p_price) AS O_Split_Adjusted_Price,
f_prevunadjprice(p.fsym_id,p.p_date,Previous_Date,p.p_price),
(
SELECT MAX(f.p_date)
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = p.fsym_id AND f.p_date<p.p_date
) Previous_Date
FROM
fp_v2_fp_basic_prices p
LEFT JOIN (
SELECT r2.region, b2.p_co_sec_name_desc, b2.fsym_id
FROM fp_v2_fp_sec_coverage b2
LEFT JOIN sym_v1_sym_region r2 ON b2.fsym_id = r2.fsym_id
WHERE r2.region = "EUR") b
ON b.fsym_id =p.fsym_id;
I get the error, "Previous_date not on column list" when trying to call the function f_prevundadjprice. Basically what I want to do is create the column previous date using (SELECT MAX..) and then use the value from this column in the function f_prevunadjprice.
If you use p. to reference all the fields coming from fp_v2_fp_basic_prices , you should also do it in the next line:
f_prevunadjprice(p.fsym_id,p.p_date,Previous_Date,p.p_price)
try to change it by:
f_prevunadjprice(p.fsym_id,p.p_date,p.Previous_Date,p.p_price)
If you continue with the same error, you should ensure the table/view fp_v2_fp_basic_prices has a column named Previous_Date
You should also use AS here:
(
SELECT MAX(f.p_date)
FROM fp_v2_fp_basic_prices AS f
WHERE f.fsym_id = p.fsym_id AND f.p_date<p.p_date
) Previous_Date
So:
...) AS Previous_Date
you already have one subquery. If you were using TSQL you could outer apply the second one. MySQL doesn't support apply, so you'll have to left join to it:
SELECT
p.fsym_id,
b.p_co_sec_name_desc AS Company_Name,
p.p_date,
p.p_price AS Unadjusted_Price,
b.region AS Region,
f_splitadjprice(p.fsym_id,p.p_date,p.p_price) AS O_Split_Adjusted_Price,
f_prevunadjprice(p.fsym_id,p.p_date,Previous_Date,p.p_price),
PreviousDate.maxdate
FROM
fp_v2_fp_basic_prices p
LEFT JOIN (
SELECT r2.region, b2.p_co_sec_name_desc, b2.fsym_id
FROM fp_v2_fp_sec_coverage b2
LEFT JOIN sym_v1_sym_region r2 ON b2.fsym_id = r2.fsym_id
WHERE r2.region = "EUR") b
ON b.fsym_id =p.fsym_id;
LEFT JOIN (
SELECT f.fsym_id,f.p_date,MAX(f.p_date) as maxdate
FROM fp_v2_fp_basic_prices AS f
group by f.fsym_id, f.p_date
) Previous_Date
on Previous_Date.fsym_id = p.fsym_id and AND Previous_Date.p_date<p.p_date
I have this query which i want to get rank from the data on my database
set #urut:=0;
set #rankhrg:=0;
select #urut:=#urut+1 as urut, a.id_tender, b.nama_tender, b.nomor_tender, b.tgl_close1 as tgl_close,
(SELECT rankhrg
from (select sum(tot_harga) as hrg_twr, id_rekanan, id_tender, #rankhrg:=#rankhrg+1 as rankhrg from tb_real_barang where id_tender = s.id_tender group by id_rekanan) as rank_harga
left join tb_master_tender s on s.id_tender = b.id_tender
where rank_harga.id_rekanan = a.id_rekanan
order by rank_harga.hrg_twr asc) as ranking
from tb_real_tender a
left join tb_master_tender b on a.id_tender = b.id_tender
where a.id_rekanan = 1
order by convert(a.id_tender,unsigned) desc
i want to pass id_tender into the select inside the select when i want to get rankhrg :
select sum(tot_harga) as hrg_twr, id_rekanan, id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang
where id_tender = s.id_tender
group by id_rekanan
but I always get error that said that s.id_tender is unknown in where clause.
can someone guide me how to pass the parameter into that insert?
thank you :)
You are not joining with that table tb_master_tender and neither it's present in outer query FROM clause. So, you need to do a JOIN separately for that inner query like below
select sum(trb.tot_harga) as hrg_twr,
trb.id_rekanan,
trb.id_tender,
#rankhrg:=#rankhrg+1 as rankhrg
from tb_real_barang trb
left join tb_master_tender s on trb.id_tender = s.id_tender
group by trb.id_rekanan
I have the following code that produces a list of order numbers and values...
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`;
I need to update a table called matcontctsafter which has an OrderNo field and currently blank InvoiceAmount column that I need the relative SUM(v.UnitPrice) in.
Can anybody help me construct the UPDATE clause?
UPDATE matcontctsafter m
INNER JOIN (
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`) InvoiceAmount
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`
) sq ON m.OrderNo = sq.OrderNo
SET m.InvoiceAmount = sq.InvoiceAmount;
UPDATE matcontctsafter m SET m.InvoiceAmount = (SELECT
SUM(v.UnitPrice)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.VMainID = d.MainID
WHERE m.OrderNo = d.OrderNo);
Using this query
SELECT DISTINCT(job_primary.id) AS id
FROM `job_primary`
LEFT JOIN `job_skill` ON `job_skill`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_facts` ON `job_facts`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_location` ON `job_location`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_people` ON `job_people`.`job_id` = `job_primary`.`id`
LEFT JOIN `job_sec` ON `job_sec`.`job_id` = `job_primary`.`id`
AND job_sec.exp_start_date > UNIX_TIMESTAMP()
INNER JOIN `user_skills` ON `user_skills`.`skill_id` = `job_skill`.`skill_id`
AND user_skills.user_id = 1
WHERE job_primary.posted_by != 1
I am getting only the id value. But I want to get the rest of the values with distinct id. Don't think about rest of the codes. The problem is here, if I use:
select *, job_primary.id as id
to select code then I get all data. But it is not distinct. So if I used
SELECT *, distinct(job_primary.id) as id
or
SELECT distinct(job_primary.id), * as id
but it is showing errors.
I think your confussion is what to query itself, nixon1333. What do you want to ask to the database? If you want just one row per id, group'em by with SELECT * FROM ... GROUP BY job_primary.id