Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Syntax for group by

Good morning,

what is wrong with the syntax below? ForecastTemp1 does contain records,

Thanks in advance,

Leonardo

ForecastTemp1:
SQL select * FROM GISdb.dbo."SAP_SO_Report" where CD = 'D' and Year > 2013 and Area = 'FCYEAR';

ForecastTemp2:
load
Area as ValueType,
CD as [Credit / Debit],
GISLE,

SenderKey as Order,
SenderKey & ' ' & Sender as [Order (Long)],
ReceiverKey as Account,
ReceiverKey & ' ' & Receiver as [Account (Long)],
if (Month > 9, '0' & Month, '00' & Month) as [Month (Long)],
Year,
Unit,
sum (TotalCost) as ForecastTotal,
sum (TotalQty) as ForecastTotalQty,
ServiceGroup
resident ForecastTemp1
group by Area, CD, GISLE, SenderKey, ReceiverKey, Year, Month, ServiceGroup;
drop tables ForecastTemp1;

1 Solution

Accepted Solutions
jagan
Partner - Champion III
Partner - Champion III

Hi,

You missed Sender, Receiver, Unit in the Group by clause.  Try below statement

ForecastTemp2:
load
Area as ValueType,
CD as [Credit / Debit],
GISLE,

SenderKey as Order,
SenderKey & ' ' & Sender as [Order (Long)],
ReceiverKey as Account,
ReceiverKey & ' ' & Receiver as [Account (Long)],
if (Month > 9, '0' & Month, '00' & Month) as [Month (Long)],
Year,
Unit,
sum (TotalCost) as ForecastTotal,
sum (TotalQty) as ForecastTotalQty,
ServiceGroup
resident ForecastTemp1
group by Area, CD, GISLE, SenderKey, ReceiverKey, Year, Month, ServiceGroup, Sender, Receiver, Unit;
drop tables ForecastTemp1;

View solution in original post

7 Replies
datanibbler
Champion
Champion

Hi Leonardo,

in a LOAD with an aggregation function, the GROUP BY clause must contain ALL OTHER fields (except the ones where you have some aggregation fct.).

You might not want that - in that case you must not include the fields you do not want to group by in that LOAD, but join them to the aggregate table lateron.

HTH

Not applicable
Author

Hi Leonqrdo,

you need to add the non aggregated fields used in your load script to your group by, at the moment 'Sender', 'Receiver' and 'Unit' are missing from the group by

hope that helps

Joe

fvelascog72
Partner - Specialist
Partner - Specialist

Hi,

I think Unit, Sender and Receiver Fields may cause error syntax cause doesn´t have aggregation fucntion.

buzzy996
Master II
Master II

i think ur if statement causing the issue here,because of grouping.

jagan
Partner - Champion III
Partner - Champion III

Hi,

You missed Sender, Receiver, Unit in the Group by clause.  Try below statement

ForecastTemp2:
load
Area as ValueType,
CD as [Credit / Debit],
GISLE,

SenderKey as Order,
SenderKey & ' ' & Sender as [Order (Long)],
ReceiverKey as Account,
ReceiverKey & ' ' & Receiver as [Account (Long)],
if (Month > 9, '0' & Month, '00' & Month) as [Month (Long)],
Year,
Unit,
sum (TotalCost) as ForecastTotal,
sum (TotalQty) as ForecastTotalQty,
ServiceGroup
resident ForecastTemp1
group by Area, CD, GISLE, SenderKey, ReceiverKey, Year, Month, ServiceGroup, Sender, Receiver, Unit;
drop tables ForecastTemp1;

Peter_Cammaert
Partner - Champion III
Partner - Champion III

Or if you're sure that Sender will be the same if SenderKey is the same and the same logic applies to ReceiverKey/Receiver (it usually does), use this (fewer GROUP BY fields):

ForecastTemp2:
load
Area as ValueType,
CD as [Credit / Debit],
GISLE,

SenderKey as Order,
SenderKey & ' ' & Only(Sender) as [Order (Long)],
ReceiverKey as Account,
ReceiverKey & ' ' & Only(Receiver) as [Account (Long)],
if (Month > 9, '0' & Month, '00' & Month) as [Month (Long)],
Year,
Unit,
sum (TotalCost) as ForecastTotal,
sum (TotalQty) as ForecastTotalQty,
ServiceGroup
resident ForecastTemp1
group by Area, CD, GISLE, SenderKey, ReceiverKey, Year, Month, ServiceGroup, Unit;
drop tables ForecastTemp1;

sasiparupudi1
Master III
Master III

try


ForecastTemp1:
SQL select * FROM GISdb.dbo."SAP_SO_Report" where CD = 'D' and Year > 2013 and Area = 'FCYEAR';


NoConcatenate


ForecastTemp2:
load
Area as ValueType,
CD as [Credit / Debit],
GISLE,

SenderKey as Order,
SenderKey & ' ' & Sender as [Order (Long)],
ReceiverKey as Account,
ReceiverKey & ' ' & Receiver as [Account (Long)],
if (Month > 9, '0' & Month, '00' & Month) as [Month (Long)],
Year,
Unit,
sum (TotalCost) as ForecastTotal,
sum (TotalQty) as ForecastTotalQty,
ServiceGroup
resident ForecastTemp1
group by Area, CD, GISLE, SenderKey, ReceiverKey, Year, Month, ServiceGroup;
drop tables ForecastTemp1;