group by and summ

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

group by and summ

Post by sal21 »

Based the table attached how to group by TEST3 and summ the VE+VI on TOTALE?

In this case:

CATANIA = 56
NAPOLI = 59

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

Isn't it simply

SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST GROUP BY TEST3

?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:Isn't it simply

SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST GROUP BY TEST3

?
work fine but into the filed TEST9 i can have also othe value VR, VS, FV... ecc
I need to filter only Ve and Vi

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

That wasn't clear from your sample database. Try

SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('VE','VI') GROUP BY TEST3
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:That wasn't clear from your sample database. Try

SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('VE','VI') GROUP BY TEST3
:clapping: :clapping:

if i understand, for 3 filter item in TEST9 ...

('VE','VI','VR')

correct?

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

Yes, indeed.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:Yes, indeed.
.... based this.
admit have a string=DF-ER-FG or ED-TG... ecc
how to fill dinamiclly the filter ('DF',ER','FG') or ('ED','TG')...

I hope you have understand me :clapping:

Note:
peraph splitting string from "-"?

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

Do you build the SQL string in code?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:Do you build the SQL string in code?
yes, sure! :thankyou:

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

You could use code like this:

Code: Select all

Dim strSQL As String
Dim strFilter As String

strFilter = "DF-ER-FG"

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('" & _
  Replace(strFilter, "-", "','") & "') GROUP BY TEST3"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:You could use code like this:

Code: Select all

Dim strSQL As String
Dim strFilter As String

strFilter = "DF-ER-FG"

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('" & _
  Replace(strFilter, "-", "','") & "') GROUP BY TEST3"
sorry for delay i test it now...

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:You could use code like this:

Code: Select all

Dim strSQL As String
Dim strFilter As String

strFilter = "DF-ER-FG"

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('" & _
  Replace(strFilter, "-", "','") & "') GROUP BY TEST3"
Perfect!

... but, in this case, the var strFilter is filled from a result of loop (for next) and i can have also a single vlaue for example FC, how to check when i need to use Replace(strFilter, "-", "','"), peraphs:

Code: Select all

if instr(strFilter is present "-") then 
strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('" & _
  Replace(strFilter, "-", "','") & "') GROUP BY TEST3"
else 
...wath?
end if

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

The code should also work correctly if strFilter doesn't contain "-".
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:You could use code like this:

Code: Select all

Dim strSQL As String
Dim strFilter As String

strFilter = "DF-ER-FG"

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('" & _
  Replace(strFilter, "-", "','") & "') GROUP BY TEST3"
I m sorry, but have changed the field TEST9 property in Number and the strFiletr = "265-266-267-268-269-270"..
When the value in TEST9 are letter the sql work but with number, no.
Peraphs i need to use chr(34) in replace statement?

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

No, you need to omit the single quotes:

Code: Select all

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN (" & _
  Replace(strFilter, "-", ",") & ") GROUP BY TEST3"
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:No, you need to omit the single quotes:

Code: Select all

strSQL = "SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN (" & _
  Replace(strFilter, "-", ",") & ") GROUP BY TEST3"
hI Hans... based the this prob is possible to know how many items are in the string strFilter???

User avatar
HansV
Administrator
Posts: 78620
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: group by and summ

Post by HansV »

The number of items is

UBound(Split(strFilter, "-")) + 1

or

Len(strFilter) - Len(Replace(strFilter, "-", "")) + 1
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4368
Joined: 26 Apr 2010, 17:36

Re: group by and summ

Post by sal21 »

HansV wrote:The number of items is

UBound(Split(strFilter, "-")) + 1

or

Len(strFilter) - Len(Replace(strFilter, "-", "")) + 1
TKS!