Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Query Multiple su tabella

Author  Topic 

giannilionetti
Starting Member

3 Posts

Posted - 2013-01-15 : 10:50:14
Ho necessità di raggruppare dati di Produzione come in tabella sottostante, qualcuno saprebbe dirmi come fare?

CodProdotto Turno Data PezziBuoni PezziCattivi
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 0 1
xxxxx 1 20/12/2012 1 0
xxxxx 2 19/12/2012 1 0
xxxxx 2 19/12/2012 0 1
xxxxx 2 19/12/2012 0 1
xxxxx 1 19/12/2012 1 0
xxxxx 1 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0

Risultato Atteso

Turno Data PezziBuoni PezziCattivi
1 20/12/2012 3 1
2 20/12/2012 0 0
1 19/12/2012 1 0
2 19/12/2012 1 2
1 18/12/2012 1 0
2 18/12/2012 3 0

theboyholty
Posting Yak Master

226 Posts

Posted - 2013-01-15 : 10:55:21
Forgive my ignorance, but could we have it in English please?

---------------------------------------------------------------------------------
http://www.mannyroadend.co.uk A Bury FC supporters website and forum
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-01-15 : 11:01:41
If I knew how to say "Is this homework?" in Italian, I wouldn't answer, but..

SELECT turno,data,sum(PezziBuono) as PezziBuono,sum(PezziCattivi) as PezziCattivi
FROM sottostante
GROUP BY turno,data

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-15 : 12:35:51
it should be

SELECT p.turno,
p.data,
COALESCE(q.PezziBuono,0) AS PezziBuono,
COALESCE(q.PezziCattivi,0) AS PezziCattivi
FROM
(
SELECT *
FROM (SELECT DISTINCT Turno FROM sottostante) m
CROSS JOIN (SELECT DISTINCT data FROM sottostante)n
)p
LEFT JOIN (SELECT Turno,data,SUM(PezziBuoni) AS PezziBuoni, SUM(PezziCattivi) AS PezziCattivi
FROM sottostante
GROUP BY Turno,data
) q
ON q.Turno = p.Turno
AND q.data = p.data


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

giannilionetti
Starting Member

3 Posts

Posted - 2013-01-16 : 03:42:22
Sorry i miss this is an english forum.
I've an PRODUCTION table as :

CodProd Turn Date Good NoGood
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 0 1
xxxxx 1 20/12/2012 1 0
xxxxx 2 19/12/2012 1 0
xxxxx 2 19/12/2012 0 1
xxxxx 2 19/12/2012 0 1
xxxxx 1 19/12/2012 1 0
xxxxx 1 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
Output
I need group by following data Output of Good and NoGood for Date and Turn as:
Cod.Prod Turn Date Good NoGood
xxx 1 20/12/2012 3 1
xxx 2 20/12/2012 0 0
' 1 19/12/2012 1 0
' 2 19/12/2012 1 2
' 1 18/12/2012 1 0
xxx 2 18/12/2012 3 0

Thanks
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-16 : 03:54:14
Check Visakh's post 01/15/2013 : 12:35:51

And add ORDER BY data DESC at the end of visakh's query

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-16 : 22:47:24
quote:
Originally posted by giannilionetti

Sorry i miss this is an english forum.
I've an PRODUCTION table as :

CodProd Turn Date Good NoGood
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 1 0
xxxxx 1 20/12/2012 0 1
xxxxx 1 20/12/2012 1 0
xxxxx 2 19/12/2012 1 0
xxxxx 2 19/12/2012 0 1
xxxxx 2 19/12/2012 0 1
xxxxx 1 19/12/2012 1 0
xxxxx 1 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
xxxxx 2 18/12/2012 1 0
Output
I need group by following data Output of Good and NoGood for Date and Turn as:
Cod.Prod Turn Date Good NoGood
xxx 1 20/12/2012 3 1
xxx 2 20/12/2012 0 0
' 1 19/12/2012 1 0
' 2 19/12/2012 1 2
' 1 18/12/2012 1 0
xxx 2 18/12/2012 3 0

Thanks



Will CodProd have same value for all the rows?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -