Summary table with row/column totals using SQL pivot - sql

I need help with SQL for generating pivot table with row/column totals calculated ..I have 2 tables as given below
Table ProbCat
==============
probcat | probdesc
1 minor
2 high
3 showstopper
Table ProbSummary
===================
prodcat | noofproblems | stage
1 5 Dev
2 1 Dev
3 6 QA
3 6 Prod
and I would like to generate a pivot table with row/column total percentages as shown below. I have tried combination of 'pivot' and 'group by' but could not get the row & column total accurately
Probelm Summary view:
ProbCategory CategoryDesc Dev Qa Prod Total(%)
______________________________________________________
1 Minor 5 0 0 5(100*(5/18))
2 High 1 0 0 1(100*(1/18))
3 Showstopper 0 6 6 12(100*(6/18))
Total NA 6(%) 6(%) 6(%)

Just like others mentioned, your summary/total calculation should be done on the presentation layer. But here is my attempt to getting your output minus the last summary line:
;WITH Q1
AS (
SELECT pvt.probcat
,pvt.probdesc
,ISNULL(pvt.[Dev], 0) AS 'Dev'
,ISNULL(pvt.[QA], 0) AS 'QA'
,ISNULL(pvt.[Prod], 0) AS 'Prod'
FROM (
SELECT pc.probcat
,pc.probdesc
,ps.noofproblems
,ps.stage
FROM Probcat pc
LEFT JOIN ProbSummary ps ON pc.probcat = ps.probcat
) t
PIVOT(max(noofproblems) FOR stage IN (
[Dev]
,[QA]
,[Prod]
)) pvt
),
q2 as
(SELECT q1.*
,sum(q1.Dev + q1.QA + q1.Prod) AS Total
FROM q1
GROUP BY q1.probcat
,q1.probdesc
,q1.Dev
,q1.QA
,q1.Prod
)
select q2.probcat
,q2.probdesc
,q2.Dev
,q2.QA
,q2.Prod
,cast(q2.Total as varchar(10)) + ' (' +
cast(cast((cast(q2.Total as decimal(5,2))/cast(d.CrossSum as decimal(5,2)))*100
as decimal(5,2)) as varchar(10))
+ '% )' as FinalTotal
from q2
CROSS APPLY (
SELECT sum(q1.Dev + q1.QA + q1.Prod) AS CrossSum
FROM q1
) d
ORDER BY q2.probcat
SQL Fiddle Demo

with x as
(select p.probcat as probcategory, p.probdesc as categotydesc,
case when s.stage = 'Dev' and s.noofproblems > 0 then s.noofproblems else 0 end as Dev,
case when s.stage = 'QA' and s.noofproblems > 0 then s.noofproblems else 0 end as QA,
case when s.stage = 'Prod' and s.noofproblems > 0 then s.noofproblems else 0 end as Prod
from Probcat p join Probsummary s on p.probcat = s.prodcat)
select probcategory,categotydesc,Dev,QA,Prod, Dev+QA+Prod as Total
from x
this should give what you need except the Total row at the bottom.

Here's a PIVOT query you can work with.. your example has prodcat in one table and probcat in the other.. not sure if that was typo in second table so i just with with probcat for both. should be easy to fix it it gives you an error
;
WITH ProbCatSummary AS
(
SELECT pc.probcat,
pc.probdesc,
SUM(ps.noofproblems) AS noofproblems
FROM ProbCat pc
JOIN ProbSummary ps ON pc.probcat = ps.probcat
GROUP BY pc.probcat,
pc.probdesc
)
SELECT p.*,
[Total (%)] = CONVERT(VARCHAR,[Dev] + [QA] + [Prod])
+ ' (' + CONVERT(VARCHAR,
CAST(ROUND((([Dev] + [QA] + [Prod]) * 100.0 / SUM(ps.noofproblems) OVER()),2) AS DECIMAL(8,2))
)
+ ')'
FROM (
SELECT probcat, probdesc, ISNULL([Dev],0)[Dev], ISNULL([QA],0)[QA], ISNULL([Prod],0)[Prod]
FROM (SELECT pc.probcat, probdesc, stage, noofproblems
FROM ProbCat pc
JOIN ProbSummary ps ON pc.probcat = ps.probcat) AS s
PIVOT (SUM(noofproblems) FOR stage IN ([Dev], [QA], [Prod])) AS p
) p JOIN ProbCatSummary ps ON p.probcat = ps.probcat

Related

How add up values from multiple SQL columns based on occurrances

I need select values from a table and returns the total hours for all categories and their occurrences. The challenge is that there are different totals for each occurrence.
My query:
SELECT c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences
FROM dbo.Categories sc
INNER JOIN dbo.Categories c
ON sc.CategoryID = c.CategoryID
INNER JOIN dbo.OrderHistory oh
ON sc.GONumber = oh.OrderNumber
AND sc.Item = oh.ItemNumber
WHERE sc.BusinessGroupID = 1
AND oh.OrderNumber = 500
AND oh.ItemNumber = '100'
GROUP BY c.Category, c.HrsFirstOccur, c.HrsAddlOccur
returns the following results:
Category
HrsFirstOccur
HrsAddlOccur
Occurrences
Inertia
24
16
2
Lights
1
0.5
4
Labor
10
0
1
The total is calculated based on the number of occurrences. The first one is totaled then for each additional occurrence, the HrsAddlOccur is used.
My final result should be (24 + 16) + (1 + 0.5 + 0.5 + 0.5) + 10 for a grand total of 52.5.
How do I loop and process the results to total this up?
The total is calculated based on the number of occurrences. The first one is totaled then for each additional occurrence, the HrsAddlOccur is used.
SQL databases understand arithmetic. You can perform the computation on each row. As I understand, the logic you want is:
SELECT
c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences,
c.HrsFirstOccur + ( COUNT(*) - 1 ) * HrsAddlOccur As Total
FROM ... < rest of your query > ..
Later on you can aggregate the whole resultset to get the grand total:
SELECT SUM(Total) GrandTotal
FROM (
... < above query > ..
) t
you can sum them simply up
WITH CTE as(SELECT c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences
FROM dbo.Categories sc
INNER JOIN dbo.Categories c ON sc.CategoryID = c.CategoryID
INNER JOIN dbo.OrderHistory oh ON sc.GONumber = oh.OrderNumber
AND sc.Item = oh.ItemNumber
WHERE sc.BusinessGroupID = 1
AND oh.OrderNumber = 500
AND oh.ItemNumber = '100')
SELECT SUM(HrsFirstOccur + (CAST((Occurrences -1) AS DECIMAL(8,2)) * HrsAddlOccur)) as total FROM CTE
it would do it like the example
CREATE TABLE CTE
([Category] varchar(7), [HrsFirstOccur] int, [HrsAddlOccur] DECIMAL(8,2), [Occurrences] int)
;
INSERT INTO CTE
([Category], [HrsFirstOccur], [HrsAddlOccur], [Occurrences])
VALUES
('Inertia', 24, 16, 2),
('Lights', 1, 0.5, 4),
('Labor', 10, 0, 1)
;
3 rows affected
SELECT SUM(HrsFirstOccur + (CAST((Occurrences -1) AS DECIMAL(8,2)) * HrsAddlOccur)) as total
FROM CTE
total
52.5000
fiddle

show one row and sum values

I have a query to select invoices and their items from database. I have posted example down below with two invoices having 3 invoice items (I'm using linked servers from MSSQL to Oracle DB).
Example with cID 1616251758086 got 3 positions with same "Stawka VAT" so I would like to show only one row with "Stawka VAT" = 0.23000 but values in "Netto", "VAT", "Brutto" should be summed (commented code with SUM works fine but still shows 3 rows, each one with whole sum).
Example with cID 1616251238669 got 3 positions with different "Stawka VAT" values so it should show output like already does.
I tried bunch of things but with no proper result.
CURRENT CODE:
SELECT
invo.cID, -- ID dokumentu
cProcessID, -- Dane faktury ERP
cERPNumber, -- Dane z pola cProcessID
cERPStatus, -- Wartość zmienia się na 200 po pobraniu danych
invo.cType as 'Typ dok',
cINumberExternal as 'Nr dok',
(CASE WHEN invo.cType = 'creditmemo' THEN cReferencingInvoiceNr ELSE '' END) as 'Korekta',
cIDateExternal as 'Data wyst',
cDeliveryDate as 'Data dok',
cDateOfReceipt AS 'Data wpływu',
ISNULL(invo_items.cText, '') as 'Treść',
invo_items.cCurrency as 'Waluta',
ISNULL(cCurrencyRate, '') as 'Kurs',
invo_items.cIItemNumber as 'Pozycja faktury',
invo_items.cTaxRate as 'Stawka VAT',
invo_items.cNetTotalAmount as 'Netto',
invo_items.cTaxAmount as 'VAT',
invo_items.cNetTotalAmount+invo_items.cTaxAmount as 'Brutto',
--SUM(invo_items.cNetTotalAmount) OVER(Partition by invo_items.ciid, invo_items.cTaxRate) as 'Netto',
--SUM(invo_items.cTaxAmount) OVER(Partition by invo_items.ciid, invo_items.cTaxRate) as 'VAT',
--SUM(invo_items.cNetTotalAmount+invo_items.cTaxAmount) OVER(Partition by invo_items.ciid,invo_items.cTaxRate) as 'Brutto',
cBaselineDate as 'Termin płatności',
invo.cVendorID as 'VendorID',
cVendorVatnumber as 'VendorNIP',
CONCAT(kh.cname, '', kh.cname2) as 'VendorName',
kh.cStreet as 'Ulica',
kh.cCity as 'Miasto',
kh.cCountry as 'Kraj'
FROM ORACLE_OTTO..xxxxxx.INVOICES as invo
JOIN ORACLE_OTTO..xxxxxx.VENDORMASTERDATA as kh on kh.cvendorid = invo.cvendorid
JOIN ORACLE_OTTO..xxxxxx.INVOICEITEMS as invo_items on invo.cid = invo_items.ciid
WHERE invo.cID in ('1616251758086', '1616251238669')
CURRENT OUTPUT:
cID
Pozycja faktury
Stawka VAT
Netto
VAT
Brutto
1616251758086
1
0.23000
4.0000
0.9200
4.9200
1616251758086
2
0.23000
1500.0000
345.0000
1845.0000
1616251758086
3
0.23000
20981.0000
4825.6300
25806.6300
1616251238669
1
0.23000
465.2100
107.0000
572.2100
1616251238669
2
0.08000
112.2800
8.9800
121.2600
1616251238669
3
0.05000
152.6400
7.6300
160.2700
EXPECTED OUTPUT:
cID
Pozycja faktury
Stawka VAT
Netto
VAT
Brutto
1616251758086
1,2,3
0.23000
22485.0000
5171.5500
27656.5500
1616251238669
1
0.23000
465.2100
107.0000
572.2100
1616251238669
2
0.08000
112.2800
8.9800
121.2600
1616251238669
3
0.05000
152.6400
7.6300
160.2700
Maybe using a common table expression would help?
with cte as (
select .... -- your query from the question
)
select
cte.cID,
cte.cIItemnumber as 'Pozycja faktury',
cte.cTaxRate as 'Stawka VAT',
SUM(cte.cNetTotalAmount) as 'Netto',
SUM(cte.cTaxAmount) as 'VAT',
SUM(cte.cNetTotalAmount+cte.cTaxAmount) as 'Brutto'
from cte
group by cte.cID, cte.CIItemnumber,cte.cTaxRate
Did not try this out of course, but you will get my drift... :)
This one should do your job:
SELECT
invo.cID,
STUFF((
SELECT ', ' + CAST(i_qry.cIItemNumber AS varchar)
FROM ORACLE_OTTO..xxxxxx.INVOICEITEMS i_qry
WHERE invo.cid = i_qry.ciid
FOR XML PATH('')
), 1, 2, '') AS [Pozycja faktury],
invo_items.cTaxRate AS [Stawka VAT],
SUM(invo_items.cNetTotalAmount) AS [Netto],
SUM(invo_items.cTaxAmount) AS [VAT],
SUM(invo_items.cNetTotalAmount + invo_items.cTaxAmount) AS [Brutto]
FROM
ORACLE_OTTO..xxxxxx.INVOICES as invo
JOIN ORACLE_OTTO..xxxxxx.VENDORMASTERDATA as kh on kh.cvendorid = invo.cvendorid
JOIN ORACLE_OTTO..xxxxxx.INVOICEITEMS as invo_items on invo.cid = invo_items.ciid
GROUP BY
invo.cID, invo_items.cTaxRate
In this case I assume that ORACLE_OTTO..xxxxxx.INVOICESITEMS.cIItemNumber is a number. In case it is a char/varchar, just remove the CAST around it.

Bizarre Join with comma

I'm looking at someone else's code and find this bizarre join:
SELECT
SUM(
(
intUnitOverheadCost + intUnitLaborCost + intUnitMaterialCost + intUnitSubcontractCost
+ intUnitDutyCost + intUnitFreightCost + intUnitMiscCost
)
*
(
(
CASE
WHEN imtSource = 3
THEN - 1
ELSE 1
END
) * intQuantity
)
)
FROM PartTransactions --imt
INNER JOIN PartTransactionCosts --int
ON imtPartTransactionID = intPartTransactionID
LEFT JOIN Warehouses --imw
ON imtPartWarehouseLocationID = imwWarehouseID
, ProductionProperties --xap <-- weird join
WHERE imtJobID = jmpJobID
AND imtSource IN (2,3)
AND imtReceiptID = ''
AND Upper(imtTableName) <> 'RECEIPTLINES'
AND imtNonInventoryTransaction <= {?CHECKBOXGROUP_4_ShowNonInventory}
AND imtJobType IN (1, 3)
AND imtTransactionDate < DATEADD(d, 1, {?PROMPT_1_TODATE})
AND (
imtNonNettable = 0
OR (
imtNonNettable <> 0
AND ISNULL(imwDoNotIncludeInJobCosts, 0) = 0
)
)
AND intCostType = (
CASE -- Always 1
WHEN xapIMCostingMethod = 1
THEN 1
WHEN xapIMCostingMethod = 2
THEN 2
WHEN xapIMCostingMethod = 3
THEN 3
ELSE 4
END
)
There is only one record in table ProductionProperties and the result of select xapIMCostingMethod from ProductionProperties is always 1.
There are always 4 enumerated results in PartTransactionCosts, but only 1 result is allowed.
ProductionProperties.xapIMCostingMethod is implicitly joining to PartTransactionCosts.intCostType
My specific question is what is really going on with this comma join? It looks like it has to be a cross-join, later filtered in the WHERE clause with one possible result.
Agree with the previous answer. It is a cartesian join but since the rows are 1 it doesn't cause an issue.
I'm thinking if you added rows to ProductionProperties then it would serve as a multiplier for your sum. I did a little experiment to show the issue:
declare #tableMoney table (
unit int,
Product char(5),
xapIMPCostingMethod int,
Cost money
)
declare #tableProdProperties table (
xapIMPCostingMethod int
)
insert #tableMoney (unit, Product, xapIMPCostingMethod, Cost)
values
(1,'bike',1, 2.00),
(1,'car',1, 2.25),
(2,'boat',2, 4.50)
insert #tableProdProperties (xapIMPCostingMethod)
values (1),
(2)
select sum(Cost)
from #tableMoney, #tableProdProperties
I also don't like to use joins where it isn't clear what is joining to what so I always use an alias:
select sum(Cost)
from #tableMoney tbm join #tableProdProperties tpp
on tbm.xapIMPCostingMethod = tpp.xapIMPCostingMethod

Using pivot table with column and row totals in sql server 2008

I have a table with following columns
defect_id, developer_name, status, summary, root_cause,
Secondary_RC, description, Comments, environment_name
The column root_cause has Enviro, Requi, Dev, TSc, TD, Unkn as its values and
column environment_name has QA1, QA2, QA3
I need to prepare a report in the below format
Enviro Requi Dev TSc TD Unkn Total
QA1 9 1 14 17 2 3 46
QA2 8 1 14 0 5 1 29
QA3 1 1 7 0 0 1 10
Total 18 3 35 17 7 5 85
I have prepare the report till
Enviro Requi Dev TSc TD Unkn
QA1 9 1 14 17 2 3
QA2 8 1 14 0 5 1
QA3 1 1 7 0 0 1
I used the below query to get the above result
select *
from
(
select environment_name as " ", value
from test1
unpivot
(
value
for col in (root_cause)
) unp
) src
pivot
(
count(value)
for value in ([Enviro] , [Requi] , [Dev] , [Tsc], [TD] , [Unkn])
) piv
Can anyone help to get the totals for columns and rows?
There may be various approaches to this. You can calculate all the totals after the pivot, or you can get the totals first, then pivot all the results. It is also possible to have kind of middle ground: get one kind of the totals (e.g. the row-wise ones), pivot, then get the other kind, although that might be overdoing it.
The first of the mentioned approaches, getting all the totals after the pivot, could be done in a very straightforward way, and the only thing potentially new to you in the below implementation might be GROUP BY ROLLUP():
SELECT
[ ] = ISNULL(environment_name, 'Total'),
[Enviro] = SUM([Enviro]),
[Requi] = SUM([Requi]),
[Dev] = SUM([Dev]),
[Tsc] = SUM([Tsc]),
[TD] = SUM([TD]),
[Unkn] = SUM([Unkn]),
Total = SUM([Enviro] + [Requi] + [Dev] + [Tsc] + [TD] + [Unkn])
FROM (
SELECT environment_name, root_cause
FROM test1
) s
PIVOT (
COUNT(root_cause)
FOR root_cause IN ([Enviro], [Requi], [Dev], [Tsc], [TD], [Unkn])
) p
GROUP BY
ROLLUP(environment_name)
;
Basically, the GROUP BY ROLLUP() part produces the Total row for you. The grouping is first done by environment_name, then the grand total row is added.
To do just the opposite, i.e. get the totals prior to pivoting, you could employ GROUP BY CUBE() like this:
SELECT
[ ] = environment_name,
[Enviro] = ISNULL([Enviro], 0),
[Requi] = ISNULL([Requi] , 0),
[Dev] = ISNULL([Dev] , 0),
[Tsc] = ISNULL([Tsc] , 0),
[TD] = ISNULL([TD] , 0),
[Unkn] = ISNULL([Unkn] , 0),
Total = ISNULL(Total , 0)
FROM (
SELECT
environment_name = ISNULL(environment_name, 'Total'),
root_cause = ISNULL(root_cause, 'Total'),
cnt = COUNT(*)
FROM test1
WHERE root_cause IS NOT NULL
GROUP BY
CUBE(environment_name, root_cause)
) s
PIVOT (
SUM(cnt)
FOR root_cause IN ([Enviro], [Requi], [Dev], [Tsc], [TD], [Unkn], Total)
) p
;
Both methods can be tested and played with at SQL Fiddle:
Method 1
Method 2
Note. I've omitted the unpivoting step in both suggestions because unpivoting a single column seemed clearly redundant. If there's more to it, though, adjusting either of the queries should be easy.
You can find Total for root_cause and environment_name using ROLLUP.
RNO_COLTOTAL - Logic to place Total in last column, since the columns Tsc,Unkn will overlap the column Total when pivoting, since its ordering alphabetically.
RNO_ROWTOTAL - Logic to place Total in last row since a value that is starting with U,W,X,Y,Z can overlap the value Total, since its ordering alphabetically.
SUM(VALUE) - Can define on what aggregate function we can use with ROLLUP.
QUERY 1
SELECT CASE WHEN root_cause IS NULL THEN 1 ELSE 0 END RNO_COLTOTAL,
CASE WHEN environment_name IS NULL THEN 1 ELSE 0 END RNO_ROWTOTAL,
ISNULL(environment_name,'Total')environment_name,
ISNULL(root_cause,'Total')root_cause,
SUM(VALUE) VALUE
INTO #NEWTABLE
FROM
(
-- Find the count for environment_name,root_cause
SELECT DISTINCT *,COUNT(*) OVER(PARTITION BY environment_name,root_cause)VALUE
FROM #TEMP
)TAB
GROUP BY root_cause,environment_name
WITH CUBE
We will get the following logic when CUBE is used
We declare variables for pivoting.
#cols - Column values for pivoting.
#NulltoZeroCols - Replace null values with zero.
QUERY 2
DECLARE #cols NVARCHAR (MAX)
SELECT #cols = COALESCE (#cols + ',[' + root_cause + ']',
'[' + root_cause + ']')
FROM (SELECT DISTINCT RNO_COLTOTAL,root_cause FROM #NEWTABLE) PV
ORDER BY RNO_COLTOTAL,root_cause
DECLARE #NulltoZeroCols NVARCHAR (MAX)
SET #NullToZeroCols = SUBSTRING((SELECT ',ISNULL(['+root_cause+'],0) AS ['+root_cause+']'
FROM(SELECT DISTINCT RNO_COLTOTAL,root_cause FROM #NEWTABLE GROUP BY RNO_COLTOTAL,root_cause)TAB
ORDER BY RNO_COLTOTAL FOR XML PATH('')),2,8000)
Now pivot it dynamically
DECLARE #query NVARCHAR(MAX)
SET #query = 'SELECT environment_name,'+ #NulltoZeroCols +' FROM
(
SELECT RNO_ROWTOTAL,environment_name,root_cause,VALUE
FROM #NEWTABLE
) x
PIVOT
(
MIN(VALUE)
FOR [root_cause] IN (' + #cols + ')
) p
ORDER BY RNO_ROWTOTAL,environment_name;'
EXEC SP_EXECUTESQL #query
RESULT
I think you need to calculate the Total separately. Using this simple query for the total (sorry, had to give the alias name for your " " column):
select environment_name as en,
count (*) AS Total
FROM test1
WHERE value in ('Enviro', 'Requi', 'Dev', 'Tsc', 'TD', 'Unkn')
GROUP BY environment_name
you can easily join both queries together to get the required report:
SELECT * FROM
(select *
from
(
select environment_name as en, value
from test1
unpivot
(
value
for col in (root_cause)
) unp
) src
pivot
(
count(value)
for value in ([Enviro] , [Requi] , [Dev] , [Tsc], [TD] , [Unkn])
) piv
) AS a
INNER JOIN
( select environment_name as en,
count (*) AS Total
FROM test1
WHERE value in ('Enviro', 'Requi', 'Dev', 'Tsc', 'TD', 'Unkn')
GROUP BY environment_name
) AS b ON a.en = b.en
UNION ALL
SELECT * FROM
(select *
from
(
select 'Total' as en, value
from test1
unpivot
(
value
for col in (root_cause)
) unp
) src
pivot
(
count(value)
for value in ([Enviro] , [Requi] , [Dev] , [Tsc], [TD] , [Unkn])
) piv
) AS a
INNER JOIN
( select 'Total' as en,
count (*) AS Total
FROM test1
WHERE value in ('Enviro', 'Requi', 'Dev', 'Tsc', 'TD', 'Unkn')
) AS b
I have not tested it, but believe it will work
You can also put you select pivot, without totals, in a temp table and then add the totalS with a new select:
SELECT environment_name=ISNULL(environment_name, ‘Total’) , Enviro=SUM(Enviro), Requi=SUM(Requi), Dev=SUM(Dev), TSc=SUM(TSc), TD=SUM(TD), Unkn =SUM(Unkn),
Total = sum(Enviro+Requi+Dev+TSc+TD+Unkn)
FROM #temp
GROUP BY ROLLUP(environment_name)

Counting in a sql query

So I'm having a table like
Now I need to get this packed into a datagridview when a choice is made in a combobox filled with the uv ='owner'.
If I make a choice of the uv eg MG. I get a list of all his files/dosno he worked in and the times he spend working on the file.
I do this with this query :
SELECT kbpres.uv,
dbo.doss.dosno,
SUM(dbo.kbpres.uur) AS somuur,
SUM(dbo.kbpres.minuut) AS somminuut,
CAST (( SUM(dbo.kbpres.uur) + SUM(dbo.kbpres.minuut) / 60 ) AS VARCHAR(4)
) +
'u ' + CAST (( SUM(dbo.kbpres.minuut) % 60 ) AS VARCHAR(2)) + 'm' AS
[derivedColumn],
doss.behdr
FROM dbo.kbpres
INNER JOIN dbo.doss
ON dbo.kbpres.ino = dbo.doss.ino
WHERE ( dbo.kbpres.uv LIKE #cboBeheerder )
GROUP BY kbpres.uv,
dbo.doss.dosno,
doss.behdr
(Allthough I would only like to group by UV, and have to add the dosno and behdr as well ??)
The problem is now, how can I count the correct cost, as it is per record different.
for MG it would be :
10 * 60 for dosno 88888
20 * 76 for 66666
60*10 + (28hours+10minutes * 10) + 10*2 for 12345
Any idea if this is even possible ??
SELECT dosno,
SUM(uur)*60 + SUM(minuut) AS Time,
(SUM(uur)*60 + SUM(minuut)) * cost AS TotalCost
FROM dbo.kbpres k
INNER JOIN dbo.doss d ON k.ino = d.ino
GROUP BY dosno,k.ino,d.ino,cost
WHERE k.uv = 2
As cost seems to be a function of uv and dosno try
SELECT dosno,SUM(Time) AS Time,SUM(TotalCost) AS TotalCost FROM
(
SELECT dosno,
uur*60 + minuut AS Time,
(uur*60 + minuut) * cost AS TotalCost
FROM dbo.kbpres k
INNER JOIN dbo.doss d ON k.ino = d.ino
GROUP BY dosno,k.ino,d.ino,cost
WHERE k.uv = 2
) t
GROUP BY dosno