Solved

'COUNT' does not work

Posted on 2006-11-13
15
302 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
15 Comments
 
LVL 35

Assisted Solution

by:Raynard7
Raynard7 earned 100 total points
Comment Utility
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 100 total points
Comment Utility
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
Comment Utility
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
 
LVL 17

Expert Comment

by:HuyBD
Comment Utility
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
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 

Author Comment

by:Kongta
Comment Utility
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
Comment Utility
Sorry 8:27 and just got the results
0
 
LVL 28

Expert Comment

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

Author Comment

by:Kongta
Comment Utility
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
Comment Utility
yeah create this index

Create index IX_GFArchiv on GFArchiv(DatumBerechnung)

regards

Imran
0
 

Author Comment

by:Kongta
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now