Query Not Working

Moxioron

Active Member
Joined
Mar 17, 2008
Messages
436
Office Version
  1. 2019
Hey all.

I have a query that is fed by two other queries. SQL is at the bottom.

What I am trying to do is list the Paid Items Grouped next to the Returned Item Items Grouped. Although the individual queries work great, I get a count by Processed By. However, when I bring the queries together into one I get the following result. User allen4 did in fact have 131 paid items and returned 3...but why it jumps to 5, 10, 13, 16...and then repeats by user...I don't know. Any assistance you can provide would be appreciated. Thank you. <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:lock aspectratio="t" v:ext="edit"></o:lock><v:shape id=_x0000_s1027 style="MARGIN-TOP: 233.55pt; Z-INDEX: 1; MARGIN-LEFT: 77.1pt; WIDTH: 230.3pt; POSITION: absolute; HEIGHT: 282.6pt; mso-position-horizontal: absolute; mso-position-horizontal-relative: text; mso-position-vertical: absolute; mso-position-vertical-relative: text" type="#_x0000_t75"><v:imagedata cropright="45466f" cropbottom="26952f" croptop="5734f" o:title="" src="file:///C:\DOCUME~1\carpean\LOCALS~1\Temp\msohtml1\01\clip_image001.png"></v:imagedata><?xml:namespace prefix = w ns = "urn:schemas-microsoft-com:office:word" /><w:wrap type="square"></w:wrap></v:shape><TABLE cellSpacing=0 bgColor=#ffffff border=1><CAPTION></CAPTION><THEAD><TR><TH borderColor=#000000 bgColor=#c0c0c0>ProcessedBy</TH><TH borderColor=#000000 bgColor=#c0c0c0>CountOfAmount</TH><TH borderColor=#000000 bgColor=#c0c0c0>CountOfTranType</TH></TR></THEAD><TBODY><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>3</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>5</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>10</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>13</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>16</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>18</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>19</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>30</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>51</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>allent4</TD><TD borderColor=#c0c0c0 align=right>131</TD><TD borderColor=#c0c0c0 align=right>66</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>3</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>5</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>10</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>13</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>16</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>18</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>19</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>30</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>51</TD></TR><TR vAlign=top><TD borderColor=#c0c0c0>dd72psc</TD><TD borderColor=#c0c0c0 align=right>141</TD><TD borderColor=#c0c0c0 align=right>66</TD></TR></TBODY><TFOOT></TFOOT></TABLE>
<v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock><v:shape id=_x0000_s1026 style="MARGIN-TOP: 233.55pt; Z-INDEX: 1; MARGIN-LEFT: 77.1pt; WIDTH: 230.3pt; POSITION: absolute; HEIGHT: 282.6pt; mso-position-horizontal: absolute; mso-position-horizontal-relative: text; mso-position-vertical: absolute; mso-position-vertical-relative: text" type="#_x0000_t75"><v:imagedata cropright="45466f" cropbottom="26952f" croptop="5734f" o:title="" src="file:///C:\DOCUME~1\carpean\LOCALS~1\Temp\msohtml1\01\clip_image001.png"></v:imagedata><w:wrap type="square"></w:wrap></v:shape>
SELECT [Qry_Account Not Found - Paid Items Grouped].ProcessedBy, [Qry_Account Not Found - Paid Items Grouped].CountOfAmount, [Qry_Account Not Found - Returned Items Grouped].CountOfTranType
FROM [Qry_Account Not Found - Paid Items Grouped], [Qry_Account Not Found - Returned Items Grouped]
GROUP BY [Qry_Account Not Found - Paid Items Grouped].ProcessedBy, [Qry_Account Not Found - Paid Items Grouped].CountOfAmount, [Qry_Account Not Found - Returned Items Grouped].CountOfTranType;
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi

You have a cross-join so you just need to restrict the join by linking common fields from one table to the other (this is missing at the moment). Assuming you have a ProcessedBy field in both tables you can probably do this with:

Rich (BB code):
SELECT [Qry_Account Not Found - Paid Items Grouped].ProcessedBy, [Qry_Account Not Found - Paid Items Grouped].CountOfAmount, [Qry_Account Not Found - Returned Items Grouped].CountOfTranType
FROM [Qry_Account Not Found - Paid Items Grouped] Left Join [Qry_Account Not Found - Returned Items Grouped] On [Qry_Account Not Found - Paid Items Grouped].ProcessedBy = [Qry_Account Not Found - Returned Items Grouped].ProcessedBy 
GROUP BY [Qry_Account Not Found - Paid Items Grouped].ProcessedBy, [Qry_Account Not Found - Paid Items Grouped].CountOfAmount, [Qry_Account Not Found - Returned Items Grouped].CountOfTranType;

Relevant bits are in red.
 
Upvote 0
Perfect...thank you.

What happens though if I have an operator who has paid everything and returned nothing? They wouldn't be in the query that counts the returned items.
 
Upvote 0
Because I used a Left Join (with the Paid table on the Left) it will include all records that have been paid but have no returns.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top