?
Solved

'COUNT' does not work

Posted on 2006-11-13
15
Medium Priority
?
349 Views
Last Modified: 2008-02-26
I do have following tbl 'GFArchiv'

Symbol   ....    DatumBerechnung .....   Technik ....... Investitionsliste ..... Unterbewertung ...

2001 JP           10.11.2006                   True             True                       False
3541 JP           10.11.2006                    True            False                      True
...
2001 JP           11.11.2006                     True            True                       True
3541 JP           11.11.2006                     True            True                       True


I would like now to count how many at each day have been 'true' on Investitionsliste, but only the ones who had 'True" in Technik like:

Date                         CountTechnikTrue          CountInvestitionslisteTrue  
10.11.2006               2                                   1        
11.11.2006               2                                   2

I've tried this with

SELECT     CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104) AS Date, COUNT(Investitionsliste) AS Expr2
FROM         dbo.GFArchiv
WHERE     (Technik = 1)
GROUP BY CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104)
HAVING      (COUNT(Investitionsliste) = 1)

but it runs forever. Seems something wrong but what?

Can I count in the same step also the one where 'Unterbewertung' is true, but only the ones where 'Investitionsliste' is true as well?

thx
Kongta



0
Comment
Question by:Kongta
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 400 total points
ID: 17928585
Hi,

The query that you supplied did not give both counts.

If you did

SELECT     CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104) AS Date, sum(case when Investitionsliste = true then 1 else 0  end) AS countInvestitonsliste, sum(case when Technik = true then 1 else 0  end) AS countTechnik,
FROM         dbo.GFArchiv
WHERE     (Technik = 1)
GROUP BY CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104)
HAVING      (COUNT(Investitionsliste) = 1)


it should give you the count of each
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 total points
ID: 17928586
SELECT     CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104) AS Date,
                SUM( CASE WHEN  Investitionsliste= 'True' THEN 1 ELSE 0 END) AS Expr2,
                SUM( CASE WHEN  Unterbewertung= 'True' THEN 1 ELSE 0 END) AS Expr3

FROM         dbo.GFArchiv
WHERE     (Technik = 1)
GROUP BY CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104)
0
 
LVL 11

Expert Comment

by:Otana
ID: 17928591
Try this:

SELECT
Date, SUM(CASE Technik WHEN 'True' THEN 1 ELSE 0 END) CountTechnikTrue, SUM(CASE Investitionsliste WHEN 'True' THEN 1 ELSE 0 END) CountInvestitionslisteTrue
FROM dbo.GFArchiv
WHERE Technik = 'True'
GROUP BY Date
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 17

Expert Comment

by:HuyBD
ID: 17928594
SELECT     CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104) AS Date,
SUM(Investitionsliste) AS Expr2
FROM         dbo.GFArchiv
WHERE     (Technik = 1)
GROUP BY CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104)
0
 

Author Comment

by:Kongta
ID: 17928644
the fields 'Technik', 'Investitionsliste', 'Unterbewertung' are bit-fields and I get an error saying can't convert 'True'. I now changed true into 'CASE WHEN  Investitionsliste= 1....

but now I don't get the error anymore but it runs again forever?

I inted to use the results for a diagram in MS Access front-end, shall I export data first in a table and use them after for the diagram or take the procedure as a source as it looks more complex than I thought?
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17928648
try this


SELECT      DatumBerechnung,
            sum(case when Technik = 'true' then 1 else 0 end) countTechnik,
            sum(case when Technik = 'true' and Investitionsliste = 'true' then 1 else 0 end) countInvestitionsliste,
FROM         dbo.GFArchiv

GROUP BY  DatumBerechnung
0
 
LVL 28

Accepted Solution

by:
imran_fast earned 1000 total points
ID: 17928650
SELECT      DatumBerechnung,
          sum(case when Technik = 1 then 1 else 0 end) countTechnik,
          sum(case when Technik = 1 and Investitionsliste = 1 then 1 else 0 end) countInvestitionsliste,
FROM         dbo.GFArchiv

GROUP BY  DatumBerechnung
0
 

Author Comment

by:Kongta
ID: 17928689
I use now imran_fast latest version but wait now 6'+ and QA is still running. Actualy there are 6 millions rst in the tbl, so I assume this should be no problem for SQL.
0
 

Author Comment

by:Kongta
ID: 17928692
Sorry 8:27 and just got the results
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17928703
hi Kongta,
Is it fine or have some issure in it.
0
 

Author Comment

by:Kongta
ID: 17928711
I've now changed DatumBerechnung into CONVERT(smalldatetime, CONVERT(char(10), DatumBerechnung, 104), 104)  but still takes very long. Do you see a way to improve? I have more complicated calculations which take not that long.
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17928739
yeah create this index

Create index IX_GFArchiv on GFArchiv(DatumBerechnung)

regards

Imran
0
 

Author Comment

by:Kongta
ID: 17928755
I had already an index (Symbol, Variante, DatumBerechnung) on this tbl. But DatumBerechnung ist like 10.11.2005 14:23:21. Shall I or can I convert this into 10.11.2005 meaning is there a easy way to convert all entries in the tbl at once and than proceed after with a more easy date?
0
 
LVL 28

Expert Comment

by:imran_fast
ID: 17928785
>> there a easy way to convert all entries in the tbl at once and than proceed after with a more easy date?
updating 6 million rows will take hell of time.
try this if you dont succeed in speed up then add a new column in yourtable CustomDate update that column to have the value and then create index on that column
but first try the one below.

SELECT      convert(varchar(10),DatumBerechnung, 102)
          sum(case when Technik = 1 then 1 else 0 end) countTechnik,
          sum(case when Technik = 1 and Investitionsliste = 1 then 1 else 0 end) countInvestitionsliste,
FROM         dbo.GFArchiv

GROUP BY   convert(varchar(10),DatumBerechnung, 102)
0
 

Author Comment

by:Kongta
ID: 17928824
ok, doesn't improve that much. Let me try to solve by adding a new column.

I'll close this Q as the initial problem has been solved. Thx to all. I award Raynard7 & aneeshattingal  with 100 points as their solution was correct as well & imran_fast with the 250 as he gave me the follow-up.

Rgds
Kongta
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question