I am trying to write following query. Have one problem in this query.
SELECT
JSON_OBJECT(
'Const_Id',
`Constituencies`.`Const_Id`,
'Const_name',
CONVERT(`Constituencies`.`Const_name` USING utf8),
'event_count',
(
SELECT
COUNT(`events`.`event_id`)
FROM
`events`
INNER JOIN `cons_circles_list` ON
`cons_circles_list`.`cons_id` = `Constituencies`.`Const_Id` AND
`cons_circles_list`.`active` = 1 AND
`cons_circles_list`.`deleted` = 0
WHERE `events`.`cons_circle_id` = `cons_circles_list`.`cons_circle_id` AND
`events`.`deleted` = 0
)
) as data FROM
`Constituencies`
WHERE
`Constituencies`.`deleted` = 0
following line is have problem
cons_circles_list.cons_id = Constituencies.Const_Id
It's showing Unknown column Constituencies.Const_Id in 'on clause'
Please suggest any other methods.
You can not use the 'cons_circles_list'.'cons_id' = 'Constituencies'.'Const_Id' in inner join of 'events' and 'cons_circles_list'.
Please try to rewrite the query in this manner:
SELECT
JSON_OBJECT(
'Const_Id', Const_Id,
'Const_name', CONVERT(Const_name USING utf8),
'event_count', event_count
) as data FROM(
SELECT
`Constituencies`.`Const_Id` Const_Id,
`Constituencies`.`Const_name` Const_name,
COUNT(`events`.`event_id`) event_count
FROM
'events`
INNER JOIN `cons_circles_list` ON
`events`.`cons_circle_id` = `cons_circles_list`.`cons_circle_id`
INNER JOIN `Constituencies` ON `cons_circles_list`.`cons_id` = `Constituencies`.`Const_Id`
WHERE `events`.`deleted` = 0 AND
`cons_circles_list`.`active` = 1 AND
`cons_circles_list`.`deleted` = 0 AND
`Constituencies`.`deleted` = 0
GROUP BY `Constituencies`.`Const_Id`, `Constituencies`.`Const_name`
)q
Related
How do I include SUM((pm.Quantity * bl.TotalQty)) AS NextBOMItemCount WHERE projectbomlist.ParentPartNum = bl.PartNum?
The data should not be changed, the same data should be retrieved, the however additional column has to be included.
VIEW: `NEWprojectBOMItemCount
select
`pm`.`ProjectCode` AS `ProjectCode`,
`bl`.`PartNum` AS `PartNum`,
sum((`pm`.`Quantity` * `bl`.`TotalQty`)) AS `BOMItemCount`,
`bl`.`mp` AS `mp`,
`p`.`complete` AS `complete`,
`bl`.`RMInd` AS `RMInd`,
`bl`.`M_PartNum` AS `M_PartNum`
from
(
(`projectmachine` `pm` join `projectbomlist` `bl`)
join `projects` `p`
)
where
(
(`pm`.`MachineListID` = `bl`.`MachineListID`)
and (`pm`.`ProjectCode` = `bl`.`ProjectCode`)
and (`pm`.`ProjectCode` = `p`.`ProjectCode`)
and (`p`.`AfterProjectHeirarchyInd` = 'Y')
)
and and pm.ProjectCode = 'AB212323'
group by
`pm`.`ProjectCode` ,
`bl`.`PartNum`
order by
`pm`.`ProjectCode` ,
`bl`.`PartNum`
Or, another option can be, please consider above view used in below query, please suggest changes to the below query as shown above (repeating here)
`sum((pm.Quantity * bl.TotalQty)) AS NextBOMItemCount where projectbomlist.ParentPartNum = bl.PartNum` - in place of `(select-NextBOMItemCount)`?
Please see PBLH.ParentPartNum is the column that I should compare with BL.ProjectCode to get NextBOMItemCount value.
QUERY calling view: NEWprojectBOMItemCount
Select
BL.PartNum PartNumber,
PBLH.ParentPartNum NextBOM,
(select-NextBOMItemCount),
BOMItemCount TotalQty,
PL.Description,
BL.MP as PartType,
PL.Vendor,
PL.QBType
from
NEWprojectBOMItemcount BL,
bomwiz.partslist PL,
bomwiz.projectbomlistheirarchy PBLH
Where
BL.PartNum = PL.PartNum
And BL.PartNum = PBLH.PartNum
And BL.ProjectCode = PBLH.ProjectCode
And BL.projectCode = 'AB212323'
Order By PartNumber
I think that you are looking for conditional aggregation. Your requirement could be expressed as follows:
SUM(
CASE WHEN blh.ParentPartNum = bl.PartNum
THEN pm.Quantity * bl.TotalQty
ELSE 0
END
) AS NextBOMItemCount
Let me pinpoint other issues with your query:
you have unwanted parentheses all around, and I am suspicious about the syntax of the JOINs ; you need to move conditions to the ON clause of the relevant JOIN.
every non-aggregated column must appear in the GROUP BY clause - you have missing columns there
backquotes are usually not needed
Here is an updated version of the query:
SELECT
pm.ProjectCode AS ProjectCode,
bl.PartNum AS PartNum,
SUM(pm.Quantity * bl.TotalQty) AS BOMItemCount,
SUM(
CASE WHEN blh.ParentPartNum = bl.PartNum
THEN pm.Quantity * bl.TotalQty
ELSE 0
END
) AS NextBOMItemCount,
bl.mp AS mp,
p.complete AS complete,
bl.RMInd AS RMInd,
bl.M_PartNum AS M_PartNum
FROM
projectmachine AS pm
INNER JOIN projectbomlist AS bl
ON pm.MachineListID = bl.MachineListID
AND pm.ProjectCode = bl.ProjectCode
INNER JOIN join projects AS p
ON pm.ProjectCode = p.ProjectCode
AND p.AfterProjectHeirarchyInd = 'Y'
INNER JOIN projectbomlistheirarchy blh
ON bl.ProjectCode = blh.ProjectCode
WHERE
pm.ProjectCode = 'AB212323'
GROUP BY
pm.ProjectCode,
bl.PartNum,
bl.mp,
p.complete,
bl.RMInd,
bl.M_PartNum
ORDER BY
pm.ProjectCode,
bl.PartNum
I would want to count entries in a column and display the count beside it.
However, I'm clueless on how can I do it.
Desired output:
arrangement_number tray_no rl_type flag(count of occurrence)
------------------ ------- ---- ----
2774818 381001 R 3
2774818 381001 R 3
2774818 381001 L 3
2778470 405128 R 1
2779702 265265 R 2
2779702 265265 R 2
I'm currently trying queries using #variables but I still cant get it right.
each row are unique and I need them not to be grouped.
Update: Expanded Table added source code
Note: I'm currently joining 5 tables now
Actual Query:
SELECT
log.arrangement_number,
header.tray_number,
detail.rl_type,
-- some more fields here
FROM
log
INNER JOIN
header ON log.arrangement_number = header.rxarrangement_number
AND log.production_place_code = header.production_place_code
INNER JOIN
detail ON log.arrangement_number = detail.rxarrangement_number
AND log.production_place_code = detail.production_place_code
INNER JOIN
deliveryperiod ON log.arrangement_number = deliveryperiod.arrangement_number
AND log.production_place_code = deliveryperiod.production_place_code
AND detail.rl_type = deliveryperiod.rl_type
INNER JOIN
calc ON calc.arrangement_number = log.arrangement_number
AND calc.production_place_code = log.production_place_code
AND deliveryperiod.rl_type = calc.rl_type
AND detail.rl_type = calc.rl_type
WHERE
header.status_code IN ('20' , '21')
AND log.process_code = '12'
AND deliveryperiod.process_code_current = '12'
AND deliveryperiod.sub_process_code_current IN ('100' , '105')
AND lot_number = '120131'
ORDER BY log.lot_number , log.sequence_number , deliveryperiod.rl_type DESC
SELECT t1.tray_no,
t2.flag
FROM yourTable
INNER JOIN
(
SELECT tray_no, COUNT(*) AS flag
FROM yourTable
GROUP BY tray_no
) t2
ON t1.tray_no = t2.tray_no
try this...
SELECT tray_no, COUNT(*) 'flag'
FROM table1
GROUP BY tray_no
I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id
The following is the MS Sql server Update statement
Update
HC_TranDetails
SET
InsPayments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 2
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
Adjustments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 8
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
FROM
HC_TranDetails TD
Now i am trying the same kind of statement in mysql as follows
UPDATE claimdetails SET balanceAmount = (SELECT IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0)
FROM claimdetail CD WHERE CD.claimID = CDS.claimID)
FROM ClaimDetail CDS
But it is showing as syntax Error near 'From ClaimDetail CDS' at line 4
MySQL is squeamish about updates on the same table. The easy fix is to include an extra level of subquery. The proper fix, though, is to use a join
UPDATE claimdetails join
(select claimid, IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0) as val
from ClaimDetails
group by claimid
) agg
on claimdetails.claimid = agg.claimid
SET balanceAmount = agg.val;
You can join the table you want to update with a subquery that calculates the balance for each claimid on the other table.
By using LEFT JOIN, it will update all records on table claimdetails. A value of 0 will be updated to any non existent claimid on the subquery.
UPDATE claimdetails a
LEFT JOIN
(
SELECT claimID,
SUM(IFNULL(debitamount, 0)) - SUM(IFNULL(creditamount,0)) bal
FROM claimdetail
GROUP BY claimID
) b ON a.claimID = b.claimID
SET a.balanceAmount = IFNULL(b.bal, 0)
I'm trying to build what appears to be a fairly simple nested query, but when I combine the following two working queries I'm getting the following error:
Unknown table 'cm' in field list
ErrorNr. = 1109.
SELECT `cm`.`cans_id` AS `cans_id`,
`cm`.`cans_date` AS `cans_date`,
`cm`.`begintfin` AS `begIntFin`,
`c`.`cans_id` AS `CAN_ID`,
`c`.`q001` AS `q001`,
`c`.`q002` AS `q002`,
`c`.`q093` AS `q093`,
`c`.`q094` AS `q094`,
`c`.`mru` AS `mru2`,
(SELECT Count(0) AS `count(*)`
FROM `cans2notes` `nt`
JOIN `cans2meta` `cm`
ON `cm`.`cans_id` = `nt`.`cans_id`
JOIN `cans2surveys` `c`
ON `c`.`cans_id` = `cm`.`cans_id`
WHERE ( `nt`.`cans_id` = `c`.`cans_id` ));
Any ideas on this would be very appreciated.
You are referring to a table, but there is no FROM or WHERE clause.
Basically you have
select col,
col,
col,
col,
(select count(*) from sometable)
But you don't have a
FROM table cm
INNER JOIN table c
ON CM.ID = C.OTHER_ID
I'm guessing what you are trying to do is this:
SELECT `cm`.`cans_id` AS `cans_id`,
`cm`.`cans_date` AS `cans_date`,
`cm`.`begintfin` AS `begIntFin`,
`c`.`cans_id` AS `CAN_ID`,
`c`.`q001` AS `q001`,
`c`.`q002` AS `q002`,
`c`.`q093` AS `q093`,
`c`.`q094` AS `q094`,
`c`.`mru` AS `mru2`,
`count(*)` AS 'count'
FROM `cans2notes` `nt`
JOIN `cans2meta` `cm`
ON `cm`.`cans_id` = `nt`.`cans_id`
JOIN `cans2surveys` `c`
ON `c`.`cans_id` = `cm`.`cans_id`
WHERE ( `nt`.`cans_id` = `c`.`cans_id` )
GROUP BY `cm`.`cans_id`,
`cm`.`cans_date`,
`cm`.`begintfin`,
`c`.`cans_id`,
`c`.`q001`,
`c`.`q002`,
`c`.`q093`,
`c`.`q094`,
`c`.`mru`;