Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:51:41+00:00 2026-05-26T23:51:41+00:00

I have a query which involves 2 tables ‘Coupons’ and ‘CouponUsedLog’ in SQL Server,

  • 0

I have a query which involves 2 tables ‘Coupons’ and ‘CouponUsedLog’ in SQL Server, the query below will obtain some information from these 2 tables for statistics study use. Somehow I feel that while my query works and returns me the desired results, I feel that I can be written in a more efficient way, can someone please advice if there’s a better way to rewrite this? Am I using too many unnecessary variables and joins? Thanks.

DECLARE @CouponISSUED        int=null
DECLARE @CouponUSED          int=null
DECLARE @CouponAVAILABLE     int=null
DECLARE @CouponEXPIRED       int=null
DECLARE @CouponLastUsed      Date=null


--Total CouponIssued
SET    @CouponISSUED = 
(
    select count(*) 

    from   Coupon C Left Join 
           couponusedlog CU on C.autoid = CU.Coupon_AutoID

    where  C.VoidedBy is null and 
           C.VoidedOn is null and 
           DeletedBy  is null and 
           DeletedOn  is null and 
           Card_AutoID in (Select AutoID 
                           from   Card 
                           where  MemberID = 'Mem001')
)

--Total CouponUsed
SET @CouponUSED = 
(
    select count(*) 
    from   couponusedlog CU Left Join 
           Coupon C on CU.Coupon_AutoID = V.autoid

    where  CU.VoidedBy is null and 
           CU.VoidedOn is null and 
           C.Card_AutoID in (select AutoID 
                             from   Card 
                             where  MemberID = 'Mem001')
)


SET @CouponAVAILABLE = @CouponISSUED - @CouponUSED


--Expired Coupons
SET @CouponEXPIRED = 
(
    select Count(*) 

    from   Coupon C Left Join 
           couponusedlog CU on C.autoid = CU.Coupon_AutoID

    where  C.VoidedBy is null and 
           C.VoidedOn is null and 
           deletedBy  is null and 
           deletedOn  is null and
           Card_AutoID in (select AutoID 
                           from  Card 
                           where MemberID = 'Mem002') and
           CONVERT (date, getdate()) > C.expirydate
) 

--Last Used On
SET @CouponLastUsed = 
(
    select CONVERT(varchar(10), 
           Max(VU.AddedOn), 103) AS [DD/MM/YYYY] 

    from   couponusedlog CU Left Join 
           coupon C on CU.Coupon_AutoID = C.autoid

    where  CU.voidedBy is null and 
           CU.voidedOn is null and 
           C.Card_AutoID in (select AutoID 
                             from Card 
                             where MemberID = 'Mem002')
)



Select  @CouponISSUED    As Coupon_Issued,
        @CouponUSED      As Coupon_Used, 
        @CouponAVAILABLE As Coupon_Available,
        @CouponEXPIRED   As Coupon_Expired,
        @CouponLastUsed  As Last_Coupon_UsedOn
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:51:42+00:00Added an answer on May 26, 2026 at 11:51 pm

    In general its better to do things in a single query if you you’re just looking for counts of things particularly against nearly the same data set then in four separate queries.

    This query combines what you need into a single query by converting your WHERE Clauses into SUMS of CASE statements. The MAX of the date is just a normal thing you can do when you’re doing a count or a sum.

    SELECT COUNT(*) couponissued, 
           SUM(CASE 
                 WHEN deletedby IS NULL 
                      AND deletedon IS NULL THEN 1 
                 ELSE 0 
               END) AS couponused, 
           SUM(CASE 
                 WHEN deletedby IS NULL 
                      AND deletedon IS NULL 
                      AND Getdate() > c.expirydate THEN 1 
                 ELSE 0 
               END) AS couponex,
     MAX(vu.addedon) CouponEXPIRED 
    FROM   [couponusedlog] cu 
           LEFT JOIN [Coupon] c 
             ON ( cu.coupon_autoid = v.autoid ) 
    WHERE  cu.voidedby IS NULL 
           AND cu.voidedon IS NULL 
           AND ( c.card_autoid IN (SELECT [AutoID] 
                                   FROM   [Card] 
                                   WHERE  memberid = 'Mem001') ) 
    

    You can then convert that into a Common Table Expression to do your subtraction and formatting

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a query which involves joins and finally returns the below result,
I am running oracle and have a query which pulls some results from the
I have a big mysql query which needs to trawl through 4 tables to
When you are writing a linq query that involves two tables/entities for which you
I have some code which I've been using to query MySQL, and I'm hoping
I have a query which is meant to show me any rows in table
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have a query which searches two separate fields in the same table... looking
I have a query which returns a series of cells of data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.