group by and summ
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
group by and summ
Based the table attached how to group by TEST3 and summ the VE+VI on TOTALE?
In this case:
CATANIA = 56
NAPOLI = 59
In this case:
CATANIA = 56
NAPOLI = 59
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
Isn't it simply
SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST GROUP BY TEST3
?
SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST GROUP BY TEST3
?
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
work fine but into the filed TEST9 i can have also othe value VR, VS, FV... eccHansV wrote:Isn't it simply
SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST GROUP BY TEST3
?
I need to filter only Ve and Vi
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
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
SELECT TEST3, Sum(TOTALE) AS SUMMATOTALE FROM TEST WHERE TEST9 IN ('VE','VI') GROUP BY TEST3
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
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
if i understand, for 3 filter item in TEST9 ...
('VE','VI','VR')
correct?
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
.... based this.HansV wrote:Yes, indeed.
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
Note:
peraph splitting string from "-"?
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
yes, sure!HansV wrote:Do you build the SQL string in code?
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
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
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
sorry for delay i test it now...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"
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
Perfect!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"
... 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
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
The code should also work correctly if strFilter doesn't contain "-".
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
I m sorry, but have changed the field TEST9 property in Number and the strFiletr = "265-266-267-268-269-270"..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"
When the value in TEST9 are letter the sql work but with number, no.
Peraphs i need to use chr(34) in replace statement?
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
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
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
hI Hans... based the this prob is possible to know how many items are in the string strFilter???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"
-
- Administrator
- Posts: 78620
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: group by and summ
The number of items is
UBound(Split(strFilter, "-")) + 1
or
Len(strFilter) - Len(Replace(strFilter, "-", "")) + 1
UBound(Split(strFilter, "-")) + 1
or
Len(strFilter) - Len(Replace(strFilter, "-", "")) + 1
Best wishes,
Hans
Hans
-
- PlatinumLounger
- Posts: 4368
- Joined: 26 Apr 2010, 17:36
Re: group by and summ
TKS!HansV wrote:The number of items is
UBound(Split(strFilter, "-")) + 1
or
Len(strFilter) - Len(Replace(strFilter, "-", "")) + 1