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

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

Related

Count Instances Of Occuring String With Unique IDs

I need to count the number of times that a specific string occurs but they when one ID has the same string more than once, only count it once. Basically, I need to count the number of occurrences of a string that occur uniquely to an ID. I believe this should be a simple thing to do but I don't know what I'm doing. Here is my current code:
SELECT
RXNAME as Name,
DUPERSID as ID,
COUNT(RXNAME) as Number
FROM
`OmniHealth.PrescriptionsMEPS`
GROUP BY
ID,
Name
ORDER BY
Number
When run, it says everything was counted as 1. Thanks for the help!
UPDATE:
Dataset: https://storage.googleapis.com/omnihealth/MepsPrescriptionData.csv
OUTPUT when run with code above:
Row Name ID Number
1 SUMATRIPTAN 68896102 1
2 IBUPROFEN 65063102 1
3 PENICILLN VK 66179101 1
4 FUROSEMIDE 63217102 1
5 HYSINGLA ER 70373101 1
6 FUROSEMIDE 76090101 1
7 SKELETAL MUSCLE RELAXANTS 78414101 1
8 AMOXICILLIN 69467103 1
9 TRAMADOL HCL 67667101 1
10 PANTOPRAZOLE 60737102 1
11 CARBAMIDE PEROXIDE 6.5% OTIC SOLN 63990104 1
12 PROMETH/COD 68433101 1
13 AZITHROMYCIN 79045102 1
14 METRONIDAZOL 75414101 1
15 DEXILANT 69625101 1
16 TRAMADOL HCL 66890203 1
17 AZITHROMYCIN 73838101 1
18 COLCRYS 63856102 1
19 PERMETHRIN 62103107 1
20 ACETAMINOPHEN TAB 500 MG 62456102 1
not sure if it is what you asked - but if you are looking for DISTINCT COUNT - go with below:
#standardSQL
SELECT
RXNAME AS Name,
COUNT(DISTINCT DUPERSID) AS Number
FROM `OmniHealth.PrescriptionsMEPS`
GROUP BY 1
ORDER BY Number DESC
Try this...You are grouping on a different field than you are counting. I think you are meaning to group by RXNAME.
SELECT
RXNAME as Name,
DUPERSID as ID,
COUNT(RXNAME) as Number
FROM
`OmniHealth.PrescriptionsMEPS`
GROUP BY
ID,
RXNAME
ORDER BY
Number
I think you want:
SELECT DUPERSID as ID, COUNT(DISTINCT RXNAME) as Number
FROM `OmniHealth.PrescriptionsMEPS`
GROUP BY ID
ORDER BY Number;
This assumes that "same string" means "same value for RXNAME".

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.

Dividing summed field by another summed field in the same query

How to divide a summed field by another summed field in the same query.
Example: lets have the query "querySummary" which its field have been grouped already
SID SumOfCredits SumOfMarks
1 3 18
2 2 20
3 4 40
Group By Sum Sum
I want to add another field named "FAvg" to the same query that builds up of dividing "SumOfMarks" by SumOfCredits, so the the result should be as following
SID SumOfCredits SumOfMarks FAvg
1 3 18 6
2 2 20 10
3 2 40 20
Any help please ? many Thanks
Replace "Sum" in the "Total" row by "Expression" and in the "Field" row use the expression:
FAvg: Sum(Mark)/Sum(Credit)
You'll get something like this:
(The other Sum columns are not required for the FAvg expression)
The SQL looks like this:
SELECT
Table1.SID,
Sum(Table1.Credit) AS SumOfCredit,
Sum(Table1.Mark) AS SumOfMark,
Sum([Mark])/Sum([Credit]) AS FAvg
FROM
Table1
GROUP BY
Table1.SID;

modified SQL query not showing all results

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)

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_