Solved

add grand total to query FAO riteshshah

Posted on 2009-05-12
11
372 Views
Last Modified: 2012-05-06
I wish to add the grand total of t0.linetotal to this query also?  I did try using hte same method as you but found a few errors unfortunately.
SELECT T1.[SlpName], T2.CardName

, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date

, sum(T0.[Quantity]) Total_fixes

, sum(T0.LineTotal) Invoicing_total

, sum(T3.[U_IIS_TIME]) TaskTime_Total

, T0.[u_iis_INS2], T0.[u_IIS_INS3]

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = '6'

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))

group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]

Open in new window

0
Comment
Question by:KeirMcCann
  • 6
  • 5
11 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24362862
try using this.



SELECT T1.[SlpName]

, T2.CardName

, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date

, sum(T0.[Quantity]) Total_fixes

, sum(T0.LineTotal) Invoicing_total

, sum(T3.[U_IIS_TIME]) TaskTime_Total

, T0.[u_iis_INS2], T0.[u_IIS_INS3]

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = '6'

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
 

union
 

SELECT 'Z Grand Total'

, ''

, NULL

, null

, sum(T0.LineTotal) Invoicing_total

, NULL

, NULL

, NULL

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = '6'

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
 
 

group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]

Open in new window

0
 

Author Comment

by:KeirMcCann
ID: 24362906
OSLP.slpname is invalid in the select list because it is not contained in either the aggregate function or group by clause

Thanks

Keir
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24362982
what about this?



SELECT T1.[SlpName]

, T2.CardName

, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date

, sum(T0.[Quantity]) Total_fixes

, sum(T0.LineTotal) Invoicing_total

, sum(T3.[U_IIS_TIME]) TaskTime_Total

, T0.[u_iis_INS2], 

T0.[u_IIS_INS3]

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = '6'

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))

group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]
 

union
 

SELECT 'Z Grand Total'

, ''

, NULL

, null

, sum(T0.LineTotal) Invoicing_total

, NULL

, NULL

, NULL

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = '6'

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))

Open in new window

0
 

Author Comment

by:KeirMcCann
ID: 24362990
works a treat again!!!
Many thanks!
0
 

Author Comment

by:KeirMcCann
ID: 24362993
will this go straight into the email code again?
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 31

Expert Comment

by:RiteshShah
ID: 24363028
YES, IT WILL, if you face any problem, let me know.
0
 

Author Comment

by:KeirMcCann
ID: 24363058
doesnt appear to work in sql agent with the email command unfortunately  :(
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24363061
can you show me your code here?
0
 

Author Comment

by:KeirMcCann
ID: 24363069
EXEC msdb.dbo.sp_send_dbmail
@recipients='keir@lala.com',
@body='labour report',
@subject ='Labour Costs',
@profile_name = 'Keir',
@query ='set nocount on; SELECT T1.[SlpName]
, T2.CardName
, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date
, sum(T0.[Quantity]) Total_fixes
, sum(T0.LineTotal) Invoicing_total
, sum(T3.[U_IIS_TIME]) TaskTime_Total
, T0.[u_iis_INS2],
T0.[u_IIS_INS3]
FROM DLN1 T0  
INNER JOIN OSLP T1
  ON T0.SlpCode = T1.SlpCode
INNER JOIN ODLN T2
  ON T0.DocEntry = T2.DocEntry
left outer join OSCN T3
  on t3.ItemCode = t0.ItemCode
 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)
WHERE T2.[U_IIS_JTYPE] = '6'
  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)
  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))
group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]
 
union
 
SELECT 'Z Grand Total'
, ''
, NULL
, null
, sum(T0.LineTotal) Invoicing_total
, NULL
, NULL
, NULL
FROM DLN1 T0  
INNER JOIN OSLP T1
  ON T0.SlpCode = T1.SlpCode
INNER JOIN ODLN T2
  ON T0.DocEntry = T2.DocEntry
left outer join OSCN T3
  on t3.ItemCode = t0.ItemCode
 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)
WHERE T2.[U_IIS_JTYPE] = '6'
  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)
  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)); set nocount off;',
@execute_query_database = 'advanced',
@attach_query_result_as_file = 0,
@query_result_header = 0,
@query_result_width = 1000,
@exclude_query_output = 0,
@query_result_separator = ';'
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24363096
look at this.
EXEC msdb.dbo.sp_send_dbmail

@recipients='keir@lala.com',

@body='labour report',

@subject ='Labour Costs',

@profile_name = 'Keir',

@query ='set nocount on; SELECT T1.[SlpName]

, T2.CardName

, CONVERT(varchar(10), T0.U_iis_ddate, 120) Date

, sum(T0.[Quantity]) Total_fixes

, sum(T0.LineTotal) Invoicing_total

, sum(T3.[U_IIS_TIME]) TaskTime_Total

, T0.[u_iis_INS2], 

T0.[u_IIS_INS3]

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = ''6''

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101))

group by T1.[SlpName], T2.CardName, CONVERT(varchar(10), T0.U_iis_ddate, 120), T0.[u_iis_INS2], T0.[u_IIS_INS3]

 

union

 

SELECT ''Z Grand Total''

, ''

, NULL

, null

, sum(T0.LineTotal) Invoicing_total

, NULL

, NULL

, NULL

FROM DLN1 T0  

INNER JOIN OSLP T1

  ON T0.SlpCode = T1.SlpCode

INNER JOIN ODLN T2

  ON T0.DocEntry = T2.DocEntry

left outer join OSCN T3

  on t3.ItemCode = t0.ItemCode

 and t3.Cardcode = SUBSTRING(t2.CardCode, 1, 8)

WHERE T2.[U_IIS_JTYPE] = ''6''

  and T0.[U_iis_ddate] >= (CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)) - 1)

  and T0.[U_iis_ddate] < CONVERT(datetime, CONVERT(varchar, GETDATE(), 101)); set nocount off;',

@execute_query_database = 'advanced',

@attach_query_result_as_file = 0,

@query_result_header = 0,

@query_result_width = 1000,

@exclude_query_output = 0,

@query_result_separator = ';'

Open in new window

0
 

Author Comment

by:KeirMcCann
ID: 24363107
thanks again!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now