0

Although the following query runs, no data is being returned. The query splits columns into JudgmentMonth, Consumer and Commercial. My knowledge of SQL Server is not great but I'm guessing no date parameters are given hence no data is being returned. I'm not sure where date is to be manually entered after >= and <=

WITH dset AS
(
    SELECT 
        COUNT(category) AS Volumes, 
        MONTH(creation_date) AS JudgmentMonth, 
        transaction_type, 
        REPLACE(record_type, 3, 2) AS RecordType
    FROM 
        table 
    WHERE 
        transaction_type = 'jg' 
        AND category = 'CCJ' 
        AND CAST(creation_date AS DATE) >=  CONVERT(VARCHAR(12), GETDATE(), 101) 
        AND CAST(creation_date AS DATE) <=  CONVERT(VARCHAR(12), GETDATE(), 101)
    GROUP BY 
        MONTH(creation_date), transaction_type, REPLACE(record_type, 3, 2)
)
SELECT    
    x.JudgmentMonth,
    MAX(CASE x.RecordType WHEN '1' THEN x.Volumes END) CONSUMER,
    MAX(CASE x.RecordType WHEN '2' THEN x.Volumes END) COMMERCIAL
FROM    
    (SELECT    
         r.JudgmentMonth, r.RecordType, r.Volumes
     FROM    
         dset r) x
GROUP BY 
    x.JudgmentMonth
3
  • 2
    Why is this tagged with Oracle? Commented Jun 11, 2018 at 15:50
  • 2
    Why are you converting your dates (GETDATE()) to varchar? That's not going to work. Just use convert(date, getdate()) instead? And if it's just one date, then use = Commented Jun 11, 2018 at 15:52
  • usually, comparing different data types doesnt make sense.. Commented Jun 11, 2018 at 16:00

2 Answers 2

1

You are giving it dates: you give it getdate() twice!

creation_date >= getdate and creation_date <= getdate

is the same as

creation_date = getdate()

So unless you have rows with a creation_date exactly matching the current date/time, you won't get much at all.

Sign up to request clarification or add additional context in comments.

Comments

1

Converting your column creation_date to a date is going to make your query non-SARGable. Assuming that what you want is data for tthe current date, I would suggest:

...
WHERE ...
  AND creation_date >= CONVERT(date, GETDATE())
  AND creation_date < CONVERT(date, DATEADD(DAY, 1, GETDATE()))
GROUP BY...

This will create a SARGable query, and thus perform far faster than converting both sides of the expression.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.