SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Query Multiple su tabella
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

giannilionetti
Starting Member

3 Posts

Posted - 01/15/2013 :  10:50:14  Show Profile  Reply with Quote
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

United Kingdom
221 Posts

Posted - 01/15/2013 :  10:55:21  Show Profile  Visit theboyholty's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 01/15/2013 :  11:01:41  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 01/15/2013 :  12:35:51  Show Profile  Reply with Quote
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 - 01/16/2013 :  03:42:22  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

India
2169 Posts

Posted - 01/16/2013 :  03:54:14  Show Profile  Reply with Quote
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

India
52249 Posts

Posted - 01/16/2013 :  22:47:24  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000