Solved

add grand total to query FAO riteshshah

Posted on 2009-05-12
11
371 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

747 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

10 Experts available now in Live!

Get 1:1 Help Now