modified SQL query not showing all results - sql

i have a simple query that list clients that have more than one open service job.
select link_to_client, count(call_ref) 'Services'
from calls
where call_type='PM'
and last_event_status not in ('RD','C','X')
group by link_to_client
having count(call_ref)>1
this produces 11 results.
G838AH/24 2
G21/311 2
G328RE/18 29
G328RE 4
G849RJ/6 2
ML110PQ/2 2
G21/505 2
G15PR 2
G21/314 2
ML60BN/2 2
G214AE/12 2
i need to add another field to display (link_to_contract_header)
select link_to_client, link_to_contract_header, count(call_ref) 'Services'
from calls
where call_type='PM'
and last_event_status not in ('RD','C','X')
group by link_to_client, link_to_contract_header
having count(call_ref)>1
but when i add this to the query i only end up with only 9 results
ML60BN/2 CLYVAL-2-1 2
G849RJ/6 127-3 2
G21/505 NGLA-1 2
G21/311 NGLA-1 2
G15PR WOS-1 2
G214AE/12 NGLA-1 2
G328RE CTSP-1 4
G21/314 NGLA-1 2
G328RE/18 CTSP-1 29
so where am i going wrong?

in your 2 other line you haven't the same link_to_client, link_to_contract_header,
if you group by only link_to_client then you have the count >1 but if you group by link_to_client, link_to_contract_header, the two line deletet dont have count >1
Update
you can check the values of link_to_contract_header in the 2 line of G838AH/24 (you will find different value)

Related

Access SQL query select with a specific pattern

I want to select each 5 rows to be unique and the select pattern applies for the rest of the result (i.e if the result contains 10 records I am expecting to have 2 set of 5 unique rows)
Example:
What I have:
1
1
5
3
4
5
2
4
2
3
Result I want to achieve:
1
2
3
4
5
1
2
3
4
5
I have tried and searched a lot but couldn't find anything close to what I want to achieve.
Assuming that you can somehow order the rows within the sets of 5:
SELECT t.Row % 5, t.Row FROM #T t
ORDER BY t.Row , t.Row % 5
We could probably get closer to the truth with more details about what your data looks like and what it is you're trying to actually do.
This will work with the sample of data you provided
SELECT DISTINCT(thevalue) FROM theresults
UNION ALL
SELECT DISTINCT(thevalue) FROM theresults
But it's unclear to me if it's really what you need.
For instance :
if your table/results returns 12 rows, do you still want 2x5 rows or do you want 2x6 rows ?
do you have always in your table/results the same rows in double ?
There's a lot more questions to rise and no hint about them in what you asked.

Very simple BigQuery SQL script won't return "0" for Count rows with no results

I am trying to make this very simple SQL script work:
SELECT
DATE(SEC_TO_TIMESTAMP(created_utc)) date_submission,
COUNT(*) AS num_apples_oranges_submissions
FROM
[fh-bigquery:reddit_comments.2008]
WHERE
(LOWER(body) CONTAINS ('apples')
AND LOWER(body) CONTAINS ('oranges'))
GROUP BY
date_submission
ORDER BY
date_submission
The results look like this:
1 2008-01-07 3
2 2008-01-08 1
3 2008-01-09 2
4 2008-01-10 3
5 2008-01-11 2
6 2008-01-13 2
7 2008-01-15 2
8 2008-01-16 3
As you can see, for days where there were no submissions containing both "apples" and "oranges", instead of a value of 0 being returned, the entire row is simply missing (such as on the 12th and 14th).
How can I fix this? I'm at my wits end. Thank you.
Try below, it will return all submissions days
SELECT
DATE(SEC_TO_TIMESTAMP(created_utc)) date_submission,
SUM((LOWER(body) CONTAINS ('apples') AND LOWER(body) CONTAINS ('oranges'))) AS num_apples_oranges_submissions
FROM
[fh-bigquery:reddit_comments.2008]
GROUP BY
date_submission
ORDER BY
date_submission

Access "Not In" query not working while only In is working correctly

I have below given query which is working fine but I want to use "Not In" operator instead of "In" but its giving no results:
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) In (SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])))));
Desired Query:
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) Not In (SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])))));
Basically it is referencing a control on parent form and based on that in a subform I want to populate the priority numbers i.e 1,2,3 and if for that record 1 is entered I want to get only 2 and 3 as drop-down option.
ReocordID OrdID Brand Name Priority
2 1 Org 1 2
3 2 Org 2 1
4 1 Org 1 1
6 1 Org 1 3
7 3 Org 3 1
8 4 Org 1 1
9 5 Org 2 1
10 5 Org 2 2
11 6 Org 1 1
12 6 Org 2 2
If there is any other better approach for the same please suggest.
Thanks in advance for your help.
In all likelihood, your problem is that Priority can take on NULL values. In that case, NOT IN doesn't work as expected (although it does work technically). The usual advice is to always use NOT EXISTS with subqueries rather than NOT IN.
But, in your case, I would suggest conditional aggregation instead:
SELECT opd.Priority
FROM OrderProdDetails as opd
GROUP BY opd.Priority
HAVING SUM(IIF(opd.OrdID = [Forms]![UpdateOrder]![OdrID], 1, 0)) = 0;
The HAVING clause counts the number of times the forms OdrId is in the orders. The = 0 means it is never there. Plus, you no longer need a select distinct.
Thanks for your prompt answers however I figured out what the problem was and the answer to problem is.
SELECT DISTINCT OrderProdDetails.Priority
FROM OrderProdDetails
WHERE (((OrderProdDetails.Priority) Not In (SELECT OrderProdDetails.Priority
FROM OrderProdDetails WHERE (((OrderProdDetails.OrdID)=[Forms]![UpdateOrder]![OdrID])
and ((OrderProdDetails.Priority) Is not null) ))));
I realized that the problem was happening only to those where there was a null value in priority so I puth the check of not null and it worked fine.
Thanks

Counting instances of an entry in one table from another table in SQL

I have two sql tables site and sms
The structure is ROUGHLY like
SENEGAL_SITE:
siteID Lon Lat
1 11.232 12.32
2 12.232 12.42
3 11.232 12.62
4 11.232 11.42
ATA_SMS_Apr:
out_going_site_id inSite no_sms
4 1 65
2 4 21
3 4 54
i want to query out a result somthing like this
site id SMS_Site_count
1 5
2 3
3 1
So basically I want to count the number of sms through each site tower
The query which I used to do this is this
select * ,
count((select *
from ATA_SMS_Apr a
where s.site_id=a.out_going_site_id))
from SENEGAL_SITE s
Doing this I get a error as Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
you need a correlated subquery to get the sms count from ATA_SMS_Apr table for each site id
The count is moved inside the subquery from outside.
select * ,
(select count(*)
from ATA_SMS_Apr a
where s.site_id=a.out_going_site_id) as SMS_Site_Count
from SENEGAL_SITE s

removing duplicates in ms access

please tell me how to write this query
i have an access table
number
2
2
1
2
2
1
1
3
2
i want a query that gives
number count
2 5
1 3
3 1
any help appreciated
something like...
SELECT number, count(number) AS count
FROM table
GROUP BY number
It's a bad idea to have a column named number since it is a reserved keyword.
You probably want something like
select number_, count(*) as count from ... group by number_