SQL Grand Total row from Multi table Select Query - sql

I have been able to use the below SQL query to give me 4 rows of data
SELECT 'Sales Order ' as Type, Format(Sum(T1.C_NetAmountLessDiscount),
'#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesOrder as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) AND T1.C_OrderType
!= 'BK' AND T1.C_OrderType != 'INV'
Union
SELECT 'Despatch Notes ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount),
'#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesDeliveryNote as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) AND T1.C_Order is
null AND T1.C_BillingStatus = '0'
Union
SELECT 'Invoices ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount), '#.00')
As NetAmount, Format(Sum(T1.C_MarginAmount), '#.00') As MarginAmount,
Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount), '#.00%') As
Margin
From T_SalesInvoice as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date) And
T1.C_DeliveryNote is null And T1.C_SourceOrder is null and T1.C_InvoiceType
= '0'
Union
SELECT 'Credit Notes ' AS Type, Format(Sum(T1.C_NetAmountLessDiscount), '-
#.00') As NetAmount, Format(Sum(T1.C_MarginAmount), '-#.00') As
MarginAmount, Format(Sum(T1.C_MarginAmount)/Sum(T1.C_NetAmountLessDiscount),
'#.00%') As Margin
From T_SalesCreditNote as T1
Where cast(T1.C_Date as Date) = cast(getdate() as Date)
This gives me a breakdown of the orders as I need but I also want to have a Grand Total row that sums each column.
If I insert the above sql query into the below
Select 'Grand Total', Sum(CAST(NetAmount AS float)), Sum(CAST(MarginAmount
AS float)),null
From
(
-----Above SQL Query in Here
)tbl
I get one single line with the correct totals but no breakdown rows.
How can I do this so it displays the four rows of each type and a grand total row at the bottom.

Use common table expressions (CTE).
WITH t AS(
-- Your query goes here
-- SELECT 1 AS A, 'Sales Order' ...
-- UNION SELECT 2 AS A, 'Despatch Notes' ...
-- UNION SELECT 3 AS A, 'Invoices' ...
-- UNION SELECT 4 AS A, 'Credit Notes' ...
)
SELECT
A,
Type,
NetAmount,
MarginAmount,
Margin
FROM
t
UNION SELECT
5 AS A,
'Grand Total' AS Type,
FORMAT(SUM(CAST(NetAmount AS FLOAT)), '#.00') As NetAmount,
FORMAT(SUM(CAST(MarginAmount AS FLOAT)), '#.00') As MarginAmount,
NULL
FROM
t
ORDER BY
A
Note: I added formatting the total values to ensure the total values of NetAmount and MarginAmount to share the same data type as the breakdown values. Consider to remove formatting the values from your query and add it to the presentation layer of your application.

Grouping with a ROLLUP might be just the trick for this.
Simplified Example Snippet:
DECLARE #Table1 TABLE (
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
NetAmount DECIMAL(10,2) NOT NULL DEFAULT 0
);
DECLARE #Table2 TABLE (
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
NetAmount DECIMAL(10,2) NOT NULL DEFAULT 0
);
INSERT INTO #Table1 (Name, NetAmount)
VALUES ('A', 4.1), ('A', 6.1);
INSERT INTO #Table2 (Name, NetAmount)
VALUES ('B', 9.1), ('B', 11.1);
WITH CTE_AMOUNTS AS
(
SELECT 'T1' as [Type],
SUM(NetAmount) AS [NetAmount]
FROM #Table1
UNION ALL
SELECT 'T2', SUM(NetAmount)
FROM #Table2
)
SELECT
COALESCE([Type], 'Grand Total') AS [Type],
SUM([NetAmount]) AS [NetAmount]
FROM CTE_AMOUNTS
GROUP BY Type WITH ROLLUP
ORDER BY GROUPING_ID(Type), Type;
Returns:
Type NetAmount
T1 10,20
T2 20,20
Grand Total 30,40

Related

Select data from one table base on selection from another table in SQL

I have these 3 table
First contain the item with price on given dates
2nd is the table of items
3rd is the table of dates in which we want to show the price of 2nd table item on every date
if duration is not available on first table it should be 0
with myTable ( item,startdate,enddate,price) as
(
select 'AAAA' ,'16-3-2020','19-3-2020','50' union all
select 'AAAA' ,'16-4-2020','19-4-2020','70' union all
select 'BBB' ,'16-3-2020','19-3-2020','20' union all
select 'BBB' ,'16-4-2020','19-4-2020','90' union all
select 'CCC' ,'16-3-2020','29-3-2020','45' union all
select 'CCC' ,'16-4-2020','19-4-2020','120'
)
select item,startdate,enddate,price from myTable
GO
with itemTable ( item) as
(
select 'AAAA' union all
select 'BBB' union all
select 'CCC'
)
select item from itemTable
GO
with DateTable ( dateItem) as
(
select '16-3-2020' union all
select '19-4-2020' union all
select '20-3-2020'
)
select dateItem from DateTable
GO
and my desire result should be like this (above is dynamic data)
with mydesireTable (item, [16-3-2020],[19-4-2020],[20-3-2020]) as
(
select 'AAAA' ,'50','70','0' union all ---0 as its not on above data in duration
select 'BBB' ,'20','90','0' union all
select 'CCC' ,'45','120','45'
)
select item, [16-3-2020],[19-4-2020],[20-3-2020] from mydesireTable
I am not sure what to search for :) as i want to write query for it which return my desire table as data ( or as in temporary table )
One of many ways to do this. This is a static crosstab. You need to list out all the columns explicitly (twice)
If your columns are dynamic, you need to use a dynamic crosstab. You should also consider doing this in your "presentation" layer, i.e. excel or whatever you are handing this over in.
You should consider what you want when something in mytable appears against a bucket twice (this solution will add the prices)
with myTable ( item,startdate,enddate,price) as
(
select 'AAAA' ,CAST('2020-03-16' AS DATE),CAST('2020-03-19' AS DATE),50 union all
select 'AAAA' ,'2020-04-16','2020-04-19',70 union all
select 'BBB' ,'2020-03-16','2020-03-19',20 union all
select 'BBB' ,'2020-04-16','2020-04-19',90 union all
select 'CCC' ,'2020-03-16','2020-03-29',45 union all
select 'CCC' ,'2020-04-16','2020-04-19',120
),
itemTable ( item) as
(
select 'AAAA' union all
select 'BBB' union all
select 'CCC'
)
,DateTable ( dateItem) as
(
select CAST('2020-03-16' AS DATE) union all
select '2020-04-19' union all
select '2020-03-20'
)
SELECT item,
[2020-03-16],[2020-04-19], [2020-03-20]
FROM
(
select item, dateitem, price from myTable
inner join datetable on datetable.dateItem between mytable.startdate and myTable.enddate
) As Src
PIVOT
(
SUM(price)
FOR
dateitem IN ([2020-03-16],[2020-03-20],[2020-04-19])
) as P
IF OBJECT_ID('tempdb..#myTable', 'U') IS NOT NULL
DROP TABLE #myTable;
IF OBJECT_ID('tempdb..#itemTable', 'U') IS NOT NULL
DROP TABLE #itemTable;
IF OBJECT_ID('tempdb..#DateTable', 'U') IS NOT NULL
DROP TABLE #DateTable;
CREATE TABLE #myTable (
item VARCHAR(MAX) NOT NULL,
startdate DATE NOT NULL,
enddate DATE NOT NULL,
price INT NOT NULL DEFAULT(0)
);
INSERT #myTable (item, startdate, enddate, price) VALUES
('AAAA' ,CAST('2020-03-16' AS DATE),CAST('2020-03-19' AS DATE),50),
('AAAA' ,'2020-04-16','2020-04-19',70),
('BBB' ,'2020-03-16','2020-03-19',20),
('BBB' ,'2020-04-16','2020-04-19',90),
('CCC' ,'2020-03-16','2020-03-29',45),
('CCC' ,'2020-04-16','2020-04-19',120)
CREATE TABLE #itemTable (
item VARCHAR(MAX) NOT NULL
)
INSERT #itemTable (item) VALUES
('AAAA'),
('BBB'),
('CCC')
CREATE TABLE #DateTable (
dateItem DATE NOT NULL
)
INSERT #DateTable (dateItem) VALUES
(CAST('2020-03-16' AS DATE)),
(CAST('2020-04-19' AS DATE)),
(CAST('2020-03-20' AS DATE)),
(CAST('2020-03-21' AS DATE)),
(CAST('2021-03-21' AS DATE)),
(CAST('2022-03-21' AS DATE))
Declare #DynamicCol nvarchar(max),#DynamicColNull nvarchar(max)
,#Sql nvarchar(max)
SELECT #DynamicColNull=STUFF((SELECT DISTINCT ', '+'ISNULL('+QUOTENAME(dateItem),','+'''0'''+') As '+QUOTENAME(dateItem)
FROM #DateTable FOR XML PATH ('')),1,2,'')
SELECT #DynamicCol=STUFF((SELECT DISTINCT ', '+QUOTENAME(dateItem) FROM #DateTable FOR XML PATH ('')),1,2,'')
SET #Sql='SELECT [item], '+#DynamicColNull+' From
(
select item, dateitem, price from #myTable
inner join #datetable on #datetable.dateItem between #mytable.startdate and #myTable.enddate
)
AS Src
PIVOT
(
SUM(price) FOR [dateitem] IN ('+#DynamicCol+')
)AS Pvt'
PRINT #Sql
EXEC(#Sql)

Group/Summarize each row with removal row if total quantity is 0

How to group/summarize as example image below.
The same data will be grouped based on Date and Item columns.
The quantity will be sum.
If the negative quantity is more than total quantity of the min date (total qty = 0), that row will be removed.
This condition will continue for the next min date as well.
In this case 1-Jan-2020 and 2-Jan-2020 will be removed because it negative quantity is more than total of those 2 days.
In case you want sample table, please use script below.
CREATE TABLE #temp_table(
[id] [int] IDENTITY(1,1) NOT NULL,
[trans_date] [date] NOT NULL,
[item] [nvarchar](40) NOT NULL,
[qty] [int] NOT NULL,
)
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '1-Jan-2020', 'Item A', 2 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '2-Jan-2020', 'Item A', 4 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '3-Jan-2020', 'Item B', 1 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '3-Jan-2020', 'Item A', 3 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '4-Jan-2020', 'Item A', -1 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '5-Jan-2020', 'Item A', -6 )
INSERT INTO #temp_table ( trans_date, item, qty )
VALUES
( '6-Jan-2020', 'Item A', 4 )
SELECT * FROM #temp_table
DROP TABLE #temp_table
My 1st attempt was
select
trans_date
, item
, SUM(qty)
from temp_table
group BY
trans_date
, item
My 2nd attempt, this attempt is feel like I'm lacking of some condition to reduce the next row when I first row is 0.
select
temp_table.trans_date
, temp_table.item
, SUM(temp_table.qty) + SUM(neg_table.neg_qty)
from temp_table
OUTER APPLY (
SELECT ISNULL( SUM(neg.qty), 0) AS neg_qty FROM pca_temp_table neg
WHERE 1=1
and temp_table.item = neg.item
and neg.qty < 0
) as neg_table
WHERE qty > 0
group BY
trans_date
, item
Hope this query works fine for you:
select MAX(CASE WHEN Quantity>0 THEN DATE ELSE NULL END), ITEM, SUM(Quantity)
from #T
group BY ITEM

SQL number greater than select results

I'm struggling to think of a way to do this with T-SQL.
I have a table which is populated every 5 seconds with the prices of three currencies (GBP, EUR & USD)
I've created a trigger (after insert), which selects the last 5 records entered for a given currency:
SELECT TOP 5 Price from dbo.prices where coin='GBP' ORDER BY Date Desc
I want to determine if the last inserted currency price is greater than the selected 5 above, how do i do this?
Thanks
As I guess: there cant be two entries for the same currency at one time. Only one insert per currency per some time (5sec). So this should fit yours requirements:
declare #prices table ([Date] int IDENTITY(1,1) primary key, Price float, coin varchar(3));
insert into #prices (coin, Price) values
('GBP', 3.20),('EUR', 3.14),('USD', 3.14),
('GBP', 3.17),('EUR', 3.16),('USD', 3.11),
('GBP', 3.14),('EUR', 3.13),('USD', 3.16),
('GBP', 3.15),('EUR', 3.12),('USD', 3.17),
('GBP', 3.16),('EUR', 3.17),('USD', 3.11),
('GBP', 3.15),('EUR', 3.14),('USD', 3.12),
('GBP', 3.19),('EUR', 3.14),('USD', 3.16)
select
case
when NEW.Price > PREV.Price Then 'yes'
else 'No'
end as CURR_JUMP_UP
from
(
select top 1 COALESCE(Price,0) Price, [Date]
from #prices where coin='GBP' order by [Date] desc
) NEW
cross apply
(
select MAX(Price) Price from
(
select top 5 Price
from #prices
where coin='GBP' and [Date]<NEW.[Date]
order by [Date] desc
) t
) PREV
Try this query:
DECLARE #AmountLastFiveEntry DECIMAL= (SELECT TOP 5 SUM(Price) FROM dbo.prices WHERE
ID NOT IN (SELECT TOP 1 ID
FROM dbo.prices where coin='GBP' ORDER BY Date Desc) where coin='GBP' ORDER BY Date Desc)
IF #AmountLastFiveEntry<(SELECT TOP 1 Price
FROM dbo.prices where coin='GBP' ORDER BY Date Desc)
BEGIN
SELECT #AmountLastFiveEntry --To do task
END
Trigger part is confusing
This will report if the latest price is higher (or equal) to the largest of the prior 5.
declare #currency table (iden int IDENTITY(1,1) primary key, exchange smallint, coin tinyint);
insert into #currency (coin, exchange) values
(1, 1)
, (1, 2)
, (1, 3)
, (1, 4)
, (1, 5)
, (1, 6)
, (2, 1)
, (2, 2)
, (2, 3)
, (2, 4)
, (2, 5)
, (2, 3);
select cccc.coin, cccc.exchange
, case when cccc.rn = cccc.rne then 'yes'
else 'no'
end as 'high'
from ( select ccc.iden, ccc.coin, ccc.exchange, ccc.rn
, ROW_NUMBER() over (partition by ccc.coin order by ccc.exchange desc, ccc.rn) rne
from ( select cc.iden, cc.coin, cc.exchange, cc.rn
from ( select c.iden, c.coin, c.exchange
, ROW_NUMBER() over (partition by coin order by iden desc) as rn
from #currency c
) cc
where cc.rn <= 6
) ccc
) cccc
where cccc.rn = 1
order by cccc.coin

SQL query when result is empty

I have a table like this
USER itemnumber datebought (YYYYmmDD)
a 1 20160101
b 2 20160202
c 3 20160903
d 4 20160101
Now I have to show the total number of items bought by each user after date 20160202 (2 february 2016)
I used
SELECT USER, COUNT(itemnumber)<br/>
FROM TABLE<br/>
WHERE datebought >= 20160202<br/>
GROUP BY USER<br>
It gives me results
b 1
c 1
but I want like this
a 0
b 1
c 1
d 0
Please tell me what is the most quick method / efficient method to do that ?
Try like this,
DECLARE #table TABLE
(
[USER] VARCHAR(1),
itemnumber INT,
datebought DATE
)
INSERT INTO #TABLE VALUES
('a',1,'20160101'),
('b',2,'20160202'),
('b',2,'20160202'),
('b',2,'20160202'),
('c',3,'20160903'),
('d',4,'20160101')
SELECT *
FROM #TABLE
SELECT [USER],
Sum(CASE
WHEN datebought >= '20160202' THEN 1
ELSE 0
END) AS ITEMCOUNT
FROM #TABLE
GROUP BY [USER]
Use this
SELECT USER, COUNT(itemnumber)
FROM TABLE
WHERE datebought >= 20160202
GROUP BY USER
Though this query won't be a good idea for the large amount of data:
SELECT USER, COUNT(itemnumber)
FROM TABLE
WHERE datebought >= 20160202
GROUP BY USER
UNION
SELECT DISTINCT USER, 0
FROM TABLE
WHERE datebought < 20160202
USE tempdb
GO
DROP TABLE test1
CREATE TABLE test1(a NVARCHAR(10), ino INT, datebought INT)
INSERT INTO dbo.test1
( a, ino, datebought )
VALUES ( 'a' , 1 , 20160101)
INSERT INTO dbo.test1
( a, ino, datebought )
VALUES ( 'b' , 2 , 20160202)
INSERT INTO dbo.test1
( a, ino, datebought )
VALUES ( 'c' , 3 , 20160903)
INSERT INTO dbo.test1
( a, ino, datebought )
VALUES ( 'd' , 4 , 20160101)
SELECT * FROM dbo.test1
SELECT a, COUNT(ino) OVER(PARTITION BY a) FROM dbo.test1
WHERE datebought>=20160202
UNION ALL
SELECT a, 0 FROM dbo.test1
WHERE datebought<20160202
ORDER BY a

How to replace the 'Strings' with numerical values based on a group by clause

I have the below table (#temp1) where I need to replace the string in the column'Formula' with the matching input 'VALUE' column based on the group 'Yearmonth'.
The 'Formula' column may be of any mathematical expression for better understanding I have mentioned a simple example below.
IDNUM formula INPUTNAME VALUE YEARMONTH
---------------------------------------------------------------------
1 imports(398)+imports(399) imports(398) 17.000 2003:1
2 imports(398)+imports(399) imports(398) 56.000 2003:2
3 imports(398)+imports(399) imports(399) 15.000 2003:1
4 imports(398)+imports(399) imports(399) 126.000 2003:2
For eg :From the above table i need the output as
Idnum Formula Yearmonth
1. 17.00 +15.00 2003:1
2. 56.00 +126.00 2003:2
I tried with the below different query from various suggestions but coludnt achieve it. Could someone help me this out ?
Type1 :
SELECT
REPLACE(FORMULA, INPUTName, AttributeValue) AS realvalues,
yearmonth
FROM #temp1
GROUP BY yearmonth
TYPE2 :
USING XML PATH... In this case it got worked but I need to replace only the strings with the values and not to stuff the strings based on mathematcal operation.(Because the formula might be of any type).
SELECT
IDNUM = MIN(IDNUM),
FORMULA =
(SELECT STUFF(
(SELECT ' +' + CONVERT(VARCHAR(10), Value)
FROM #temp1
WHERE YEARMONTH = t1.YEARMONTH
FOR XML PATH(''))
,1, 2, '')),
YEARMONTH
FROM #TEMP1 t1
GROUP BY YEARMONTH
TYPE3:Using Recursions...This is returning only the null values...
;with t as (
select t.*,
row_number() over (partition by yearmonth order by idnum) as seqnum,
count(*) over (partition by yearmonth) as cnt
from #temp1 t
)
,cte as (
select t.seqnum, t.yearmonth, t.cnt,
replace(formula, inputname, AttributeValue) as formula1
from t
where seqnum = 1
union all
select cte.seqnum, cte.yearmonth, cte.cnt,
replace(CTE.formula1, T.inputname, T.AttributeValue) as formula2
from cte join
t
on cte.yearmonth = t.yearmonth
AND cte.seqnum = t.seqnum + 1
)
select row_number() over (order by (select null)) as id,formula1
from cte
where seqnum = cnt
This is full working example using recursive CTE:
DECLARE #DataSource TABLE
(
[IDNUM] TINYINT
,[formula] VARCHAR(MAX)
,[INPUTNAME] VARCHAR(128)
,[VALUE] DECIMAL(9,3)
,[YEARMONTH] VARCHAR(8)
);
INSERT INTO #DataSource ([IDNUM], [formula], [INPUTNAME], [VALUE], [YEARMONTH])
VALUES ('1', 'imports(398)+imports(399)', 'imports(398)', '17.000', '2003:1')
,('2', 'imports(398)+imports(399)', 'imports(398)', '56.000', '2003:2')
,('3', 'imports(398)+imports(399)', 'imports(399)', '15.000', '2003:1')
,('4', 'imports(398)+imports(399)', 'imports(399)', '126.000', '2003:2')
,('5', '(imports(391)+imports(392)-imports(393))/imports(394)', 'imports(391)', '5.000', '2003:3')
,('6', '(imports(391)+imports(392)-imports(393))/imports(394)', 'imports(392)', '10.000', '2003:3')
,('7', '(imports(391)+imports(392)-imports(393))/imports(394)', 'imports(393)', '3.000', '2003:3')
,('8', '(imports(391)+imports(392)-imports(393))/imports(394)', 'imports(394)', '-5.000', '2003:3');
WITH DataSource AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY [YEARMONTH] ORDER BY [IDNUM]) AS [ReplacementOrderID]
,[YEARMONTH]
,[formula]
,[INPUTNAME] AS [ReplacementString]
,[VALUE] AS [ReplacementValue]
FROM #DataSource
),
RecursiveDataSource AS
(
SELECT [ReplacementOrderID]
,[YEARMONTH]
,REPLACE([formula], [ReplacementString], [ReplacementValue]) AS [formula]
FROM DataSource
WHERE [ReplacementOrderID] = 1
UNION ALL
SELECT DS.[ReplacementOrderID]
,DS.[YEARMONTH]
,REPLACE(RDS.[formula], DS.[ReplacementString], DS.[ReplacementValue]) AS [formula]
FROM RecursiveDataSource RDS
INNER JOIN DataSource DS
ON RDS.[ReplacementOrderID] + 1 = DS.[ReplacementOrderID]
AND RDS.[YEARMONTH] = DS.[YEARMONTH]
)
SELECT RDS.[YEARMONTH]
,RDS.[formula]
FROM RecursiveDataSource RDS
INNER JOIN
(
SELECT [YEARMONTH]
,MAX([ReplacementOrderID]) AS [ReplacementOrderID]
FROM DataSource
GROUP BY [YEARMONTH]
) DS
ON RDS.[YEARMONTH] = DS.[YEARMONTH]
AND RDS.[ReplacementOrderID] = DS.[ReplacementOrderID]
ORDER BY RDS.[YEARMONTH]
Generally, you simply want to perform multiple replacements over a string in one statement. You can have many replacement values, just play with the MAXRECURSION option.
--Create sample data
DROP TABLE #temp1
CREATE TABLE #temp1 (IDNUM int, formula varchar(max), INPUTNAME varchar(max), VALUE decimal, YEARMONTH varchar(max))
INSERT INTO #temp1 VALUES
(1, 'imports(398)+imports(399)', 'imports(398)', 17.000, '2003:1'),
(2, 'imports(398)+imports(399)', 'imports(398)', 56.000, '2003:2'),
(3, 'imports(398)+imports(399)', 'imports(399)', 15.000, '2003:1'),
(4, 'imports(398)+imports(399)', 'imports(399)', 126.000, '2003:2')
--Query
;WITH t as (
SELECT formula, YEARMONTH, IDNUM
FROM #temp1
UNION ALL
SELECT REPLACE(a.formula, b.INPUTNAME, CAST(b.VALUE AS varchar(100))) AS formula, a.YEARMONTH, a.IDNUM
FROM t a
JOIN #temp1 b ON a.YEARMONTH = b.YEARMONTH AND a.formula LIKE '%' + b.INPUTNAME + '%'
)
SELECT MIN(IDNUM) AS IDNUM, formula, YEARMONTH
FROM t
WHERE formula not LIKE '%imports(%'
GROUP BY formula, YEARMONTH