简体   繁体   中英

GROUP BY clause not showing desired result

I have following 3 tables:

POS_Transactions(TransactionDate,TerminalID,TransactionTypeID,TotalAmount)
Terminal(TerminalID,CountryID)
Country(CountryID,CountryName,CurrencyName)

Now I am using inner joins to link these table, but I am not getting the desired result ie It is not grouping Country-wise

   SELECT C.countryname      'CountryName', 
       C.currencyname     'CurrencyName', 
       transactiondate, 
       Sum(CASE transactiontypeid 
             WHEN 6 THEN 1 
             ELSE 0 
           END)           'Number of Cards Issue', 
       Sum(CASE transactiontypeid 
             WHEN 6 THEN totalamount 
             ELSE 0 
           END)           'Total Amount Loaded', 
       Count(DISTINCT CASE transactiontypeid 
                        WHEN 4 THEN pan 
                        ELSE NULL 
                      END)'Number of Card Redeemed', 
       Sum(CASE transactiontypeid 
             WHEN 4 THEN 1 
             ELSE 0 
           END)           'Number of Sales Transaction', 
       Sum(CASE transactiontypeid 
             WHEN 4 THEN totalamount 
             ELSE 0 
           END)           'Total Values of Sale Transaction' 
INTO   #temp 
FROM   pos_transactions p 
       INNER JOIN terminal T 
               ON T.terminalid = p.terminalid 
       INNER JOIN country C 
               ON T.countryid = C.countryid 
GROUP  BY transactiondate, 
          C.countryname, 
          C.currencyname, 
          C.countryid 

select [Number of Cards Issue],[Total Amount Loaded], [Number of Card Redeemed],[Number of Sales Transaction],

[Total Values of Sale Transaction],CountryName,CurrencyName from #temp

where (TransactionDate >= @DateFrom)    

and (TransactionDate < @DateTo)   

drop table #temp

For Example, If there are two records Transactions in Country UAE then It is showing individual results:

(CountryName,Numbers of Cards Issued,CurrencyName,Total Amount Loaded,Number of Sales Transaction,Total Values of Sale Transaction)

UAE        1         SAR        320.000     0       0.0000

UAE        2         SAR        320.000     0       0.0000

Instead it should group result for Country UAE.Should be

UAE      3      SAR     640.000     0       0.0000

What am I doing wrong?

If the TransactionDate is irrelevant for your collection you should remove it from both the select and the group by. If you are trying to group the transaction date and not the time may I suggest you use the following in your group by and select parts of your statement.

GROUP BY CAST(transactiondate AS DATE)

Try removing from the group by clause all fields that are not in your select list ( excluding fields in aggregate functions) you should then get an intelligible result.

Then try adding the removed fields back to your group by clause, and adding them to your select list also - you'll see there must be multiple values in at least one of these fields against the single country_name.

You're grouping by transactionDate (among others) and putting that data in to #temp. Nothing in the second query changes this, so the result is still grouped by transactionDate even if you select it or not.

So I guess you're left with a couple of options:

  1. Remove transactionDate from the "select into #temp"-query. I guess this is not a good option though or you would have removed it already.

  2. Group by and aggregate the second query by all selected columns.

Example (edit: in my haste I forgot to use aggregates. Updated):

SELECT SUM([Number of Cards Issue]), SUM([Total Amount Loaded]), SUM([Number of Card Redeemed]),
   SUM([Number of Sales Transaction]), SUM([Total Values of Sale Transaction]), 
   CountryName, CurrencyName 
FROM #temp
WHERE (TransactionDate >= @DateFrom) and (TransactionDate < @DateTo)
GROUP BY 
   CountryName, CurrencyName 

There is a logical Problem. In the first query (into temp) you group some things to transaction level/date level. (GROUP BY transactiondate). Then you select this data from temp, awaiting Data grouped by Country.

Because you need the transaction date as criterium in your second query you do have to group this second query (FROM temp) as well.

OR, to have it in one query, you have to put your where-clause in your first query and remove transactiondate from your grouping:

SELECT C.countryname      'CountryName', 
   C.currencyname     'CurrencyName', 
   transactiondate, 
   Sum(CASE transactiontypeid 
         WHEN 6 THEN 1 
         ELSE 0 
       END)           'Number of Cards Issue', 
   Sum(CASE transactiontypeid 
         WHEN 6 THEN totalamount 
         ELSE 0 
       END)           'Total Amount Loaded', 
   Count(DISTINCT CASE transactiontypeid 
                    WHEN 4 THEN pan 
                    ELSE NULL 
                  END)'Number of Card Redeemed', 
   Sum(CASE transactiontypeid 
         WHEN 4 THEN 1 
         ELSE 0 
       END)           'Number of Sales Transaction', 
   Sum(CASE transactiontypeid 
         WHEN 4 THEN totalamount 
         ELSE 0 
       END)           'Total Values of Sale Transaction' 
INTO   #temp 
FROM   pos_transactions p 
   INNER JOIN terminal T 
           ON T.terminalid = p.terminalid 
   INNER JOIN country C 
           ON T.countryid = C.countryid 
where (TransactionDate >= @DateFrom)    
and (TransactionDate < @DateTo)
GROUP  BY C.countryname, 
      C.currencyname, 
      C.countryid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM