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: -
SELECT CustID, CustName, iif(Month(BDate)=11,Month(BDate),11) as BMonth, Sum(iif(Month(BDate)=11,Quatity,0)) as Total-MTD
-
-
FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
-
-
WHERE iif(Month(BDate)=11,Month(BDate),11) = 11
-
-
GROUP BY CustID, CustName, Month(BDate)
-
-
ORDER BY CustID, Month(BDate);
-
if I use this 2nd SQL below, all 0-Volume-Client will be disappeared -
SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
-
-
FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
-
-
WHERE Month(BDate) = 11
-
-
GROUP BY CustID, CustName, Month(BDate)
-
-
ORDER BY CustID, Month(BDate);
-
how to show those Client without using 1st SQL or losing performance ?
Edit:
I have idea using Subquery like this: -
SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
-
-
FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
-
-
WHERE Month(BDate) = 11
-
-
GROUP BY CustID, CustName, Month(BDate)
-
-
ORDER BY CustID, Month(BDate)
-
-
UNION ALL
-
-
SELECT CustID, CustName, 11 as BMonth, 0 as Total-MTD
-
-
FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
-
-
WHERE NOT EXIST
-
(SELECT CustID, CustName, Month(BDate) as BMonth, Sum(Quatity) as Total-MTD
-
-
FROM Table2 LEFT JOINT Table1 ON Table2.CustID = Table1.CID
-
-
WHERE Month(BDate) = 11
-
-
GROUP BY CustID, CustName, Month(BDate)
-
-
ORDER BY CustID, Month(BDate));
-
-
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.
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 ==
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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...
|
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...
| |
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |