473,750 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I can't seem to get Group-By & Calculations to work

99 New Member
Below is the SQL to a query that combines "Sum", "Group By" and "Count". In the case below, the field "ad group" is supposed to be counted and appear in a new field called "CountOfadGroup Id". The value it returns 170 and that is incorrect. It should be 14. When I switch the "count" to "group by", the query returns a list of the 14 rows I expected. How come I can't get the "count" to work?

Also, if you can offer any advice or links so I can learn to get queries like this to work in the future, I would really appreciate it. Specifically, I'm talking about queries that use "group-by", calculations and involve joins.

Thanks
Adam
Expand|Select|Wrap|Line Numbers
  1. SELECT adGroupHistoryMain.blastId, M_mainBlast.blastName, Sum(adGroupHistoryMain.clicks) AS SumOfclicks, Sum(adGroupHistoryMain.impression) AS SumOfimpression, Format((Sum([clicks])/Sum([impression])),"0.00%") AS CTR, Sum(adGroupHistoryMain.cost) AS SumOfcost, Count(adGroupHistoryMain.adGroupId) AS CountOfadGroupId, M_mainBlast.Created, DateDiff("d",[Created],Now()) AS Days, Sum([cost])/DateDiff("d",[Created],Now()) AS CPD
  2. FROM adGroupHistoryMain INNER JOIN M_mainBlast ON adGroupHistoryMain.blastId = M_mainBlast.blastId
  3. WHERE (((M_mainBlast.blastStatusId)=2) AND ((M_mainBlast.constructStatusId)=3))
  4. GROUP BY adGroupHistoryMain.blastId, M_mainBlast.blastName, M_mainBlast.Created, DateDiff("d",[Created],Now());
Sep 29 '08 #1
2 1587
NeoPa
32,571 Recognized Expert Moderator MVP
Before we start let's make your SQL legible.
Expand|Select|Wrap|Line Numbers
  1. SELECT aGHM.blastId,
  2.        MmB.blastName,
  3.        Sum(aGHM.clicks) AS SumOfclicks,
  4.        Sum(aGHM.impression) AS SumOfimpression,
  5.        Format((Sum([clicks])/Sum([impression])),'0.00%') AS CTR,
  6.        Sum(aGHM.cost) AS SumOfcost,
  7.        Count(aGHM.adGroupId) AS CountOfadGroupId,
  8.        MmB.Created,
  9.        DateDiff('d',[Created],Now()) AS Days,
  10.        Sum([cost])/DateDiff('d',[Created],Now()) AS CPD
  11. FROM adGroupHistoryMain AS aGHM INNER JOIN
  12.      M_mainBlast AS MmB
  13.   ON aGHM.blastId = MmB.blastId
  14. WHERE ((MmB.blastStatusId=2)
  15.    AND (MmB.constructStatusId=3))
  16. GROUP BY aGHM.blastId,
  17.          MmB.blastName,
  18.          MmB.Created,
  19.          DateDiff('d',[Created],Now());
Sep 29 '08 #2
NeoPa
32,571 Recognized Expert Moderator MVP
Below is the SQL to a query that combines "Sum", "Group By" and "Count". In the case below, the field "ad group" is supposed to be counted and appear in a new field called "CountOfadGroup Id". The value it returns 170 and that is incorrect. It should be 14. When I switch the "count" to "group by", the query returns a list of the 14 rows I expected. How come I can't get the "count" to work?
The first thing that springs to mind is that simply by removing the field from the GROUP BY list you have changed the specification of what the results are grouped by.

Unfortunately we have no view or understanding of your data so cannot say how much your results have been effected by this.
Also, if you can offer any advice or links so I can learn to get queries like this to work in the future, I would really appreciate it. Specifically, I'm talking about queries that use "group-by", calculations and involve joins.

Thanks
Adam
I'm sorry Adam. I have no specific links for that. Perhaps you can find something useful from Links to useful sites.
Sep 29 '08 #3

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

Similar topics

3
26721
by: Greg Teets | last post by:
I used the Report Wizard to create a report that is generally working pretty well. However, it set up the structure as follows: Report Page Header Group 1 Header - Header for the group Group 2 Header - This has my column headings for the details. Details
2
8604
by: MJB | last post by:
I know I can get the current username through the System.Environment class, but I can't seem to find where I can retrieve the user's role / permission level (i.e. Admin, power user, etc). If anyone has done this and can shed some light on the issue I would appreciate it. TIA, Matt
2
1524
by: ME | last post by:
I need to know how to create a MULTI page report. All I can seem to create is a report with only one page, even though the datasource it is bound to returns multiple records. I have posted an example of my source code here: http://mreprogramming.com/currentrelease/WindowsApplication25.zip What am I doing wrong? Thanks, Matt
10
1516
by: musosdev | last post by:
I've got a function that needs to work slightly differently on the development server from the production server, but of course I don't want to be changing code just before compiling a release version!? I'm thinking I could use some #if #else #endif type statements to change certain things, depending on the build type. I'm thinking of something like this in my .cs file... #if DEBUG private string MyFunc()
1
1108
by: Kyote | last post by:
I'm using vs.net 2005 I've changed the setting on one of my previous projects. Changed the borderstyle to prevent resizing of my form. But I can't seem to find it in my current projections form. Does this problem sound familiar? Can someone help me? ------- Kyote
3
1455
by: Michel Firholz | last post by:
Hi Group experts, i would like to tweak a javascript diashow with a bit SEO. No spamming, just ensuring the images are found. If the visitor is googlebot, it should not present the diashow, but all images in one file... Now i have a doubt: can the cooglebot even "see" anything from javascript generated pages? Thanks for your help.
34
29892
by: Tom | last post by:
I'd greatly appreciate advice and code snippets on how to create a ram disk within a C/C++ program. I also need to be able to determine the free space. Thanks in advance for any help.
4
2049
by: magedsql | last post by:
I am MCDBA, I like your group, and i need to join it as a member but i do NOT know How . Please help Thank You maged.
6
1922
by: Doug Laidlaw | last post by:
I was in the middle of a discussion about my page at http://www.douglaidlaw.net/boykett/ when my computer trashed its hard drive. I have the printout of the main reply, but I had just answered a query about the OBJECT tag being put there only to keep a Validator happy. I would like to know what the response was. Google groups doesn't seem to have this group. Doug. -- Ugliness is only skin deep.
1
1676
by: =?Utf-8?B?bW9uaWNhX2ZyYXppZXI=?= | last post by:
When I import contacts from excel using the import wizard, even after matching fields to field for some reason I can only get the first three to five from my list of 190 to import the phone numbers althought other fields all seem to be there. I have even tried coping the phone numbers to other fields and re importing and still it can only give me the first few in the list. It almost seems like when it hits the first field that contains no...
0
8836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9338
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4712
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2223
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.