Showing posts with label bit. Show all posts
Showing posts with label bit. Show all posts

Wednesday, March 21, 2012

Nested SQL problem

Hi,

Although I am quite familiar with MS Access-grade SQL, I am struggling a bit with proper grown up SQL Server. My usual approach to counting things in Access is to first create a query with the conditions on the data, then use this as the basis of a second query that does the actual counting of the presorted data. I believe the way to do this in SQL server is to use a nested query. I want to generate the top 10 highest counts for each pesticide detected (detection is level>0) for a client between two dates.

Currently I am using

<code>

SELECT top 10 Count(Pesticide) AS CountOfPesticide, Pesticide FROM (SELECT tblData.Pesticide, tblData.Level, tblData.Clast, tblData.Client FROM tblData WHERE (((tblData.Day>@.sdate) AND (tblData.Day<@.edate))) and (tbldata.level>0) and (tbldata.clast=@.clast) and (tbldata.client=@.client)) as monkeyboy GROUP BY Pesticide ORDER BY Count(Pesticide) ASC"

</code>

The results that the above SQL turns out though are not reliable. For example, if I set the dates to now and 14 days ago, it produces higher counts for some pesticides then if I set the dates to now and 30 days ago. Any pointers or general advice about nested sql is gratefully accepted!

thanks

Mike

I had to split out your SQL statement so that I could understand the logic:
SELECT TOP 10
   Count(Pesticide) AS CountOfPesticide,
   Pesticide
FROM
   (
      SELECT
         tblData.Pesticide,
         tblData.Level,
         tblData.Clast,
         tblData.Client
      FROM
         tblData
      WHERE
         (
            (
               (tblData.Day>@.sdate) AND
               (tblData.Day<@.edate)
            )
         ) AND
         (tbldata.level>0) AND
         (tbldata.clast=@.clast) AND
         (tbldata.client=@.client)
   ) AS monkeyboy
GROUP BY
   Pesticide
ORDER BY
   Count(Pesticide) ASC

I think it could be simplified to this (which does not change the logicat all but removes extraneous punctuation and columns that are notneeded and only confuse issues) :
SELECT TOP 10

Count(Pesticide) AS CountOfPesticide,
   Pesticide
FROM
   (
      SELECT
         tblData.Pesticide
      FROM
         tblData
      WHERE
         tblData.Day>@.sdate AND
         tblData.Day<@.edate AND
         tbldata.level>0 AND
        tbldata.clast=@.clast AND
        tbldata.client=@.client
   ) AS monkeyboy
GROUP BY
   Pesticide
ORDER BY
   1 ASC

And unless I am missing something, I think it could be further simplified to this:
SELECT TOP 10

Count(Pesticide) AS CountOfPesticide,
   Pesticide
FROM
   tblData
WHERE
   tblData.Day>@.sdate AND
   tblData.Day<@.edate AND
   tbldata.level>0 AND
  tbldata.clast=@.clast AND
  tbldata.client=@.client
GROUP BY
   Pesticide
ORDER BY
   1 ASC
The nested query is usually only needed if you need to perform a GROUP BY within in.
If the missing GROUP BY is not the problem, then my next guess thatthere is something unusual going on with the data types. Thequestions I have are:
What is the data type of tblData.Day?|||

Thanks Terri,

This is very helpful and I'll go and play with the new simplified SQL.

The tblData.Day and @.sdate (startdate) and @.edate (enddate) are all dates.

Since I am in the UK, I have had to manually convert the parameter dates to strings in the "MM/DD/YYYY" format so that SQL server behaves itself (I believe it works in US-style date formats or ISO ones only). I have been caught out before with date-related conversion problems and so have taken exeptional care here to make sure I haven't been caught out again.

thanks for your help

Mike

|||Try using YYYYMMDD format instead to avoid those sorts of problems(this format is called ISO). You would still declare @.sdate and@.edate as one of the datetime data types.

Friday, March 9, 2012

Negate a SQL Bit

Seems an easy questions, but how do I negate a SQL bit variable in SQL Server 2000? i.e. If the Bit is 1, I want it to be set to 0. Not that this is not a select, e.g.

SET @.b = 1
SET @.b = NOT @.b -- This line does not work.Does not matter - found it :

SET @.b = 1
SET @.b = ~@.b

Wednesday, March 7, 2012

NEED VERY BASIC HELP

At my workplace our ERP software in on a VFP database. I have learned quite
a bit about extracting data with the select statements from the VFP
database. We are in the talks of converting the the SQL version of our ERP
and I have some very basic questions since I am extremely new to the SQL
world.
I have created executable forms in VFP that queries the VFP tables for
certain users. Can I continue to use these forms or do I need to go to
another software. Is my understanding correct that SQL cannot create forms
like the VFP does? If I can use the VFP interface what is the Select syntax
to access the SQL database from within the VFP interface?
The VFP looks like this: Select 'field names' from 'database name'!'table
name'
What would the SQL look like? I can use the Query Analyzer to do Select
statements but I would like to be able to query the SQL database from within
the VFP interface. Thanks for your help.
Please see my comments to your other posts.
Cindy Winegarden MCSD, Microsoft Most Valuable Professional
cindy_winegarden@.msn.com www.cindywinegarden.com
Blog: http://spaces.msn.com/members/cindywinegarden
"Preacher Man" <nospam> wrote in message
news:uSJS23GwFHA.2808@.TK2MSFTNGP10.phx.gbl...

NEED VERY BASIC HELP

At my workplace our ERP software in on a VFP database. I have learned quite
a bit about extracting data with the select statements from the VFP
database. We are in the talks of converting the the SQL version of our ERP
and I have some very basic questions since I am extremely new to the SQL
world.
I have created executable forms in VFP that queries the VFP tables for
certain users. Can I continue to use these forms or do I need to go to
another software. Is my understanding correct that SQL cannot create forms
like the VFP does? If I can use the VFP interface what is the Select syntax
to access the SQL database from within the VFP interface?
The VFP looks like this: Select 'field names' from 'database name'!'table
name'
What would the SQL look like? I can use the Query Analyzer to do Select
statements but I would like to be able to query the SQL database from within
the VFP interface. Thanks for your help.
Please see my comments to your other posts.
Cindy Winegarden MCSD, Microsoft Most Valuable Professional
cindy_winegarden@.msn.com www.cindywinegarden.com
Blog: http://spaces.msn.com/members/cindywinegarden
"Preacher Man" <nospam> wrote in message
news:uSJS23GwFHA.2808@.TK2MSFTNGP10.phx.gbl...

NEED VERY BASIC HELP

At my workplace our ERP software in on a VFP database. I have learned quite
a bit about extracting data with the select statements from the VFP
database. We are in the talks of converting the the SQL version of our ERP
and I have some very basic questions since I am extremely new to the SQL
world.
I have created executable forms in VFP that queries the VFP tables for
certain users. Can I continue to use these forms or do I need to go to
another software. Is my understanding correct that SQL cannot create forms
like the VFP does? If I can use the VFP interface what is the Select syntax
to access the SQL database from within the VFP interface?
The VFP looks like this: Select 'field names' from 'database name'!'table
name'
What would the SQL look like? I can use the Query Analyzer to do Select
statements but I would like to be able to query the SQL database from within
the VFP interface. Thanks for your help.Please see my comments to your other posts.
Cindy Winegarden MCSD, Microsoft Most Valuable Professional
cindy_winegarden@.msn.com www.cindywinegarden.com
Blog: http://spaces.msn.com/members/cindywinegarden
"Preacher Man" <nospam> wrote in message
news:uSJS23GwFHA.2808@.TK2MSFTNGP10.phx.gbl...