473,487 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to do this in group by query without lost performance ?

215 New Member
Hi everyone,

After "Decade" from beginning, I have to ask this question:

how to show up zero volume in group by query w/o losing performance.

I have 2 tables like this:

(Raw data) Table1 = {CID, Name, ItemID, BDate, Quantity, Price}
(List Client) Table2 = {CustID, CustName}

ok, my current SQL to show up 0 quantity Client below:

Expand|Select|Wrap|Line Numbers
  1. SELECT CustID, CustName, iif(Month(BDate)=11,Month(BDate),11) as BMonth, Sum(iif(Month(BDate)=11,Quatity,0)) as Total-MTD
  2.  
  3. FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
  4.  
  5. WHERE iif(Month(BDate)=11,Month(BDate),11) = 11
  6.  
  7. GROUP BY CustID, CustName, Month(BDate)
  8.  
  9. ORDER BY CustID, Month(BDate);
  10.  
if I use this 2nd SQL below, all 0-Volume-Client will be disappeared
Expand|Select|Wrap|Line Numbers
  1. SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
  2.  
  3. FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
  4.  
  5. WHERE Month(BDate) = 11
  6.  
  7. GROUP BY CustID, CustName, Month(BDate)
  8.  
  9. ORDER BY CustID, Month(BDate);
  10.  
how to show those Client without using 1st SQL or losing performance ?

Edit:
I have idea using Subquery like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
  2.  
  3. FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
  4.  
  5. WHERE Month(BDate) = 11
  6.  
  7. GROUP BY CustID, CustName, Month(BDate)
  8.  
  9. ORDER BY CustID, Month(BDate)
  10.  
  11. UNION ALL
  12.  
  13. SELECT CustID, CustName, 11 as BMonth, 0 as Total-MTD
  14.  
  15. FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
  16.  
  17. WHERE NOT EXIST
  18. (SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
  19.  
  20. FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
  21.  
  22. WHERE Month(BDate) = 11
  23.  
  24. GROUP BY CustID, CustName, Month(BDate)
  25.  
  26. ORDER BY CustID, Month(BDate));
  27.  
  28.  
Nov 13 '15 #1
3 689
Rabbit
12,516 Recognized Expert Moderator MVP
It's impossible to not lose some performance when you need to make a query more complicated.

That being said, there are different ways of writing the same query that lose less performance. But you won't ever find a way of making a simple query more complicated without losing some performance.

A couple of other ways of writing the query would be to use the Nz function instead of the IIf function that you used in the first code block. You could also try an Or Is Null instead. In your third block of code, for the bottom half of the union all, instead of a subquery, you could move the criteria into a join and return only the nulls on the outer table.

Try all 3 different methods and see which one is the fastest.
Nov 13 '15 #2
hvsummer
215 New Member
0.0 I did say 2nd method not qualify haha

I want to sum the volume and show up that record even volume = 0
2nd SQL will ignore any 0 volume (whether does exit or does not)..

i already test 1st method and 3rd method, not really improve anything ==
Nov 13 '15 #3
Rabbit
12,516 Recognized Expert Moderator MVP
0.0 I did say 2nd method not qualify haha
I didn't say anything about your second method. I know it's not what you need.

I want to sum the volume and show up that record even volume = 0
2nd SQL will ignore any 0 volume (whether does exit or does not)..
Yes, I know what you're looking for. You described this clearly enough in the first post.

i already test 1st method and 3rd method, not really improve anything ==
You mean you tested the 1st and 3rd methods in your first post. But you haven't tried the 3 variations that I suggested in my post. The methods you posted are not the same as the 3 variations I suggested.
Nov 13 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
2281
by: Steven Stern | last post by:
I'm stuck on a host that is still running MYSQL version 3. I need to flatten out a relationship to AND a set of criteria. In a nutshell User UserID UserEmail UserOther
0
517
by: Petre Agenbag | last post by:
Hi List Can anyone help me to do the following without the need of subselects: I have a table that contains rows where mostly, the only common field is the "name". The rest of the data...
0
565
by: krystoffff | last post by:
Hi all For speed reasons, I would like to rewrite the following query without the subquery : SELECT * FROM table1 WHERE field1 NOT IN (SELECT field1 FROM table2 where field2=X); I thought...
1
2603
by: sks | last post by:
Hi, I have a table which logs for sake of argument page hits: id, timestamp, ipaddress Now, I'm running a query to get an activity report and I want to group together all the hits from a...
4
1696
by: Gordon White | last post by:
I'm working on a DB for a local 5K race. I want to make the DB as flexible as possible and to avoid hard coding anything that I don't need to so that we can reuse this for other races. As such, I...
4
8157
by: uspensky | last post by:
I have a table (cars) with 3 fields: VIN, Class, sell_price 101, sports, 10000 102, sports, 11000 103, luxury, 9000 104, sports, 11000 105, sports, 11000 106, luxury, 5000 107, sports, 11000
0
2619
by: Radek | last post by:
Hi I'm wondering how big impact on performance would have turning on the general query log. I read in manual it should be turned off in production environment to increase the speed but I didn't...
3
9182
by: nitinpatel1117 | last post by:
Hi, I've got a slight issue which i'm trying to resolve and was wondering if anyone would help. Basically, I was building a web application, and while it was being built I was connecting to...
0
2601
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi, thanks, mister The code string rutaConfig = tbRutaConfigServicioBase.Text; '// Map to the application configuration file. ExeConfigurationFileMap configFile = New...
1
2843
by: baburmm | last post by:
Hi all, Can any one tell me, Export datas to a Excel file using query without openrowset method is there any other methods are posible for creating an Excel file to export it Thanks,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.