Right now I am collecting a sum of times based on grouping by a part, job, machine, and type.
I would like to have the summed times to be split across multiple columns rather than multiple rows based on the type. How can I do this?
Here is my code
SELECT
m.[machineName]
,pr.[jobNumber]
,p.[partNumber]
,sc.[type]
,SUM(pl.[elapsedTime]) AS elapsedTime
FROM wincc.dbo.productionLog pl
INNER JOIN wincc.dbo.machines m ON pl.[machineId] = m.id
INNER JOIN wincc.dbo.productionRuns pr ON pl.[productionRunId] = pr.id
INNER JOIN wincc.dbo.parts p ON pr.[partId] = p.Id
INNER JOIN wincc.dbo.statusCodes sc ON pl.[statusCodeId] = sc.id
GROUP BY
m.[machineName]
,pr.[jobNumber]
,p.[partNumber]
,sc.[type]
Which produces:
But I want:
Thank you All!
This is a form of table pivoting. Here's one way to do that with conditional aggregation:
SELECT
m.[machineName]
,pr.[jobNumber]
,p.[partNumber]
,SUM(CASE WHEN sc.Type = 'planned downtime' then pl.[elapsedTime] END) AS plannedDT
,SUM(CASE WHEN sc.Type = 'unplanned downtime' then pl.[elapsedTime] END) AS unplannedDT
,SUM(CASE WHEN sc.Type = 'production' then pl.[elapsedTime] END) AS production
,SUM(CASE WHEN sc.Type = 'rework' then pl.[elapsedTime] END) AS rework
FROM wincc.dbo.productionLog pl
INNER JOIN wincc.dbo.machines m ON pl.[machineId] = m.id
INNER JOIN wincc.dbo.productionRuns pr ON pl.[productionRunId] = pr.id
INNER JOIN wincc.dbo.parts p ON pr.[partId] = p.Id
INNER JOIN wincc.dbo.statusCodes sc ON pl.[statusCodeId] = sc.id
GROUP BY
m.[machineName]
,pr.[jobNumber]
,p.[partNumber]
Related
Im trying to do a count of particular transactions that have specific conditions and cant seem to make it work.Also in the query I select other columns from other tables
for example:
Want to select all transactions
where transactions.ID_1 = transactions.ID_2,
where transaction.dir = "outbound" and transactions.status in ("completed", "processing")
and do a count on this. like:
select
m.ID
,m.Number
,t.Status
,(column that counts of all transactions with the conditions mentioned above)
,p.label
from module m
inner join transactions t on t.ID_1 = m.ID
inner join process p on p.ID = m.ID`
Tried with sum and when and if statement but doesn't work
I consider this is a simple count you can use the next script:
SELECT COUNT(transactions.ID_1)
FROM YOUR_TABLE
WHERE transactions.ID_1 = transactions.ID_2
AND transaction.dir = "xxx"
AND transactions.status in ("a", "b")
Seems like you want to GROUP BY. Use a CASEexpression to do conditional aggregation.
select
m.ID
,m.Number
,t.Status
,SUM(case when transaction.dir = 'outbound'
and transactions.status in ('completed', 'processing') then 1
else 0
end)
,p.label
from module m
inner join transactions t on t.ID_1 = m.ID
inner join process p on p.ID = m.ID
group by m.ID
,m.Number
,t.Status
p.label
I am a beginner with SQL so apologise in advance if my terminology / coding is a little off, or maybe way off.
I have two queries which I would like to join into one. The first creating a list of productids which contain two specific processes.
I then want to use these productids in the second query.
Also is below correct?
group by products.productid having (sum(case when processid like 'pick%' then 1 else 0 end) + sum(case when processid like 'pack%' then 1 else 0 end) = 2)
Any help would be much appreciated, hope this makes sense.
SELECT
Products.ProductID
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
WHERE
products.active = 1
and Categorys.Categoryid in ('5','20')
group by products.productid
having (sum(case when processid like 'pick%' then 1 else 0 end) + sum(case when processid like 'pack%' then 1 else 0 end) = 2)
order by products.productid
SELECT
Products.ProductID,
Products.productdescription,
Boms.Type As Type,
Comp.ProductId as Component,
Comp.productdescription,
Boms.Quantity,
BomVersions.BomVersionID,
Processes.processid,
Processes.ProcessDescription
FROM Products
INNER JOIN Categories ON Products.Category = Categories.Category
INNER JOIN Boms ON Products.Product = Boms.Product AND Boms.BomVersion = Products.BomVersion
LEFT OUTER JOIN Products AS Comp ON Boms.Component = Comp.Product
LEFT OUTER JOIN Processes ON Boms.Process = Processes.Process
INNER JOIN BomVersions ON Products.BomVersion = BomVersions.BomVersion
WHERE
products.active = 1
order by products.productid, products.type,comp.productid
To combine them you could do the following.
SELECT b.*
FROM
(SELECT Products.ProductID FROM Products INNER JOIN ...) AS a
INNER JOIN
(SELECT Products.ProductID, Products.productdescription, Boms.Type As Type, ...) AS b
ON a.ProductID = b.ProductID
I have tables as below
guides users offers reservations manager_crm_issues
id id id offer_id issuable_id
user_id username guide_id issuable_type issuable_type
What I Would like to extract is
guide.id,
guide.username,
manager_crm_issues.count(issuuable_id)
The issuable_type's distinct values are {Reservation, Offer, Guide}, and it corresponds to issuable_id.
i.e. if issuable_type = 'Reservation' then the issuable_id = reservation.id
Question is, I Would like to count all the issues happened on Guide, and Guide is linked to Offer, Offer is linked to Reservation.
SELECT
a.guideId,
a.guideName,
count(case when cr.issuable_type = 'Reservation' and cr.issuable_id = a.rID THEN cr.id else 0 END),
count(case when co.issuable_type = 'Offer' AND co.issuable_id = a.offerId THEN co.id else 0 END),
count(case when cg.issuable_type = 'Guide' AND cg.issuable_id = a.guideId THEN cg.id else 0 END)
FROM
(SELECT
g.id AS guideId,
u.username AS guideName,
o.id as offerId,
r.id as rId
FROM guides g
INNER JOIN users u on u.id = g.user_id
INNER JOIN offers AS o on o.guide_id = g.id
INNER JOIN reservations AS r on r.offer_id = o.id) a
INNER JOIN manager_crm_issues cg ON cg.id = a.guideId
INNER JOIN manager_crm_issues co ON co.id = a.offerId
INNER JOIN manager_crm_issues cr ON cr.id = a.rId
group by 1,2
I tried to join tables like above, but the outcome seems inaccurate.
Would really appreciate your help.
Don't know if this is related to your issue because you do not say what the issue is but this does not do what you think it does:
count(case
when cr.issuable_type = 'Reservation' and cr.issuable_id = a.rID THEN cr.id
else 0
END),
count counts not nulls so your query will count everything. What you want is
count(case
when cr.issuable_type = 'Reservation' and cr.issuable_id = a.rID THEN cr.id
else null
END),
I have a query that returns the data i'm looking for using a distinct, but when I SUM that data I get a wrong amount for a my hierachy point '4-2-0-0-5-2'. 4-2-0-0-5-2 has multiple rows so when I sum it, it doesn't add up correctly. What would be the best way to incorporate a distinct into a SUM statement. Any help would be appreicated. Thanks.
First query :
Select distinct B.Proj_Nbr,c.proj_cc,h.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, A.Amount
from acct_bal a
inner join dim_proj b on a.dim_proj_id = b.dim_proj_id
inner join essbase_fcs.projects_hier_map c on c.proj_nbr = b.proj_nbr
inner join dim_per_mo d on d.dim_per_mo_id = a.dim_per_mo_id
Inner Join Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id
Inner Join Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id
inner join essbase_fcs.projects_hier_map h on h.proj_cc = g.cost_ctr
inner join dim_org g1 on c.proj_cc = g1.cost_ctr
Where F.Fin_Lee_Nbr = 500
and c.proj_hier like '4-2-0-0-5-2%'
And A.Dim_Scnro_Id = '45'
And D.Fscl_Yr = '2014'
And b.Proj_Nbr = '9005459'
and fscl_per ='1'
RESULT of 2 rows:
9005459 0358080 0358080 4-2-0-0-5-2 Global Sales.com (iSell) 179777.09
9005459 0358080 0358057 4-2-0-0-5-5 Global Sales.com (iSell) 2257.3**
When I want to sum the data I use this query below. This gives me the two rows i'm looking for, but proj_hier 4-2-0-0-5-2 has the wrong amount because it has multiple rows.
Select B.Proj_Nbr,c.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, sum(A.Amount)
from acct_bal a
inner join dim_proj b on a.dim_proj_id = b.dim_proj_id
inner join essbase_fcs.projects_hier_map c on c.proj_nbr = b.proj_nbr
inner join dim_per_mo d on d.dim_per_mo_id = a.dim_per_mo_id
Inner Join Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id
Inner Join Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id
inner join essbase_fcs.projects_hier_map h on h.proj_cc = g.cost_ctr
inner join dim_org g1 on c.proj_cc = g1.cost_ctr
Where F.Fin_Lee_Nbr = 500
and c.proj_hier like '4-2-0-0-5-2%'
And A.Dim_Scnro_Id = '45'
And D.Fscl_Yr = '2014'
And b.Proj_Nbr = '9005459'
and fscl_per ='1'
group by B.Proj_Nbr,c.proj_cc,f.dim_acct_id, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per
having Sum(A.Amount) <> 0
Order By H.Proj_Hier, B.Proj_Nbr, D.Fscl_Per
Please Generalize the Question and then ask, If i understood your problem Here is solution:
General Query :
select sum(a.amountColumn) from your_table
group by agrrColumnName;
If i change your query :
Select distinct B.Proj_Nbr,c.proj_cc,h.proj_cc, h.Proj_Hier, B.Proj_Nm, D.Fscl_Per, sum(A.Amount)
from acct_bal a
inner join dim_proj b on a.dim_proj_id = b.dim_proj_id
inner join essbase_fcs.projects_hier_map c on c.proj_nbr = b.proj_nbr
inner join dim_per_mo d on d.dim_per_mo_id = a.dim_per_mo_id
Inner Join Dim_Acct F On A.Dim_Acct_Id = F.Dim_Acct_Id
Inner Join Dim_Org G On A.Dim_Org_Id = G.Dim_Org_Id
inner join essbase_fcs.projects_hier_map h on h.proj_cc = g.cost_ctr
inner join dim_org g1 on c.proj_cc = g1.cost_ctr
Where F.Fin_Lee_Nbr = 500
and c.proj_hier like '4-2-0-0-5-2%'
And A.Dim_Scnro_Id = '45'
And D.Fscl_Yr = '2014'
And b.Proj_Nbr = '9005459'
and fscl_per ='1' group by B.Proj_Nbr;
I need a query that subtracts or adds according to a condition for SQLite using the CASE statement. This is my query at the moment:
SELECT s.companyName, CASE ia.type
WHEN ia.type='add' THEN SUM(ia.quantity)
WHEN ia.type='subtract' THEN SUM(-ia.type)
ELSE SUM(0) END AS total
FROM stocktake s
LEFT JOIN stocktake_adjustment sa ON s.stocktakeId = sa.stocktakeId
LEFT JOIN adjustment a ON a.adjustmentId = sa.adjustmentId
LEFT JOIN inventory_adjustment ia ON ia.adjustmentId = a.adjustmentId
LEFT JOIN inventory i ON i.inventoryId = ia.inventoryId
LEFT JOIN supplier s ON s.supplierId = i.supplierId
WHERE s.supplierId = '4da99b63-fcb9-9b9f-8415-4896caeb920c';
Basically I want to add if the condition is 'add' or subtract if condition is subtract, from the total.
Thank you in advance guys.
Move your CASE into SUM:
SELECT s.companyName, SUM(CASE ia.type
WHEN ia.type='add' THEN ia.quantity
WHEN ia.type='subtract' THEN -ia.quantity
ELSE 0
END)
AS total
FROM stocktake s
LEFT JOIN stocktake_adjustment sa ON s.stocktakeId = sa.stocktakeId
LEFT JOIN adjustment a ON a.adjustmentId = sa.adjustmentId
LEFT JOIN inventory_adjustment ia ON ia.adjustmentId = a.adjustmentId
LEFT JOIN inventory i ON i.inventoryId = ia.inventoryId
LEFT JOIN supplier s ON s.supplierId = i.supplierId
WHERE s.supplierId = '4da99b63-fcb9-9b9f-8415-4896caeb920c';
(I assumed that -ia.type in the original question was a typo, really meaning -ia.quantity)
I think what you need is to move the CASE statement inside the SUM, remove ia.type from between CASE and WHEN (or change to a simple CASE statement), and also add GROUP BY:
SELECT s.companyName,
SUM(CASE WHEN ia.type = 'add' THEN ia.Quantity
WHEN ia.type = 'subtract' THEN -ia.Quantity
ELSE 0
END) AS total
FROM stocktake s
LEFT JOIN stocktake_adjustment sa
ON s.stocktakeId = sa.stocktakeId
LEFT JOIN adjustment a
ON a.adjustmentId = sa.adjustmentId
LEFT JOIN inventory_adjustment ia
ON ia.adjustmentId = a.adjustmentId
LEFT JOIN inventory i
ON i.inventoryId = ia.inventoryId
LEFT JOIN supplier s
ON s.supplierId = i.supplierId
WHERE s.supplierId = '4da99b63-fcb9-9b9f-8415-4896caeb920c'
GROUP BY s.CompanyName;