Need help on this query - email

I want to have a output count as 2 for the user_mail logged as test1#gmail.com for a query like this,
SELECT Count(user_refemai) from Table_users where userref_mail = user_mail
but, I'm getting the output as 0! What am I doing wrong?
My table_users looks like:
user_id user_mail user_refemail
1 test1#gmail.com NULL
2 test2#gmail.com test1#gmail.com
3 test3#gmail.com test1#gmail.com

you are getting 0 as in your query the condition is false all the time
for this purpose you have to use the cursor or inner queries and then get the count
or pass the parameter to the query for which you want to get the counts.

Shafqat is correct. To build on that, you'd pass in the query parameter like this:
SELECT count(*)
FROM
table_users
WHERE
userref_email = ?
If you need a reporting query instead of for a particular email address, you could use a self join.

Related

Use postgresql query results to form another query

I am trying to select from one table using the select result from another table. I can run this in two queries but would like to optimize it into just one.
First query.. Select ids where matching other id
select id from lookuptable where paid = '547'
This results in something like this
6316352
6316353
6318409
6318410
6320468
6320469
6320470
6322526
6322527
6324586
6324587
6326648
I would like to then use this result to make another selection. I can do it manually like below. Note, there could be many rows with these values so I've been using a IN statement
select * from "othertable" where id in (6316352,6316353,6318409,6318410,6320468,6320469,6320470,6322526,6322527,6324586,6324587,6326648);
select
ot.*
from
"othertable" as ot
join
lookuptable as lt
on
ot.id = lt.id
where
lt.paid = '547'
The IN operator supports not just value lists but also subqueries, so you can literally write
select * from "othertable" where id in (select id from lookuptable where paid = '547');

Getting a BIT value as to whether a non-Aggregated Value is in a constant list

Given the following tables:
Logs
-Id
-SuperLogId
-ScanMode
Super Log
-Id
-Name
How do I write the following query to get 1 for the Logs from SuperLogs where the Log has a ScanMode of "Navigational", "Locatable" or "Automatic" and 0 otherwise?
SELECT Name,
CAST
(
CASE WHEN
Logs.ScanMode IN
(
'Navigational',
'Locatable',
'Automatic'
)
THEN
1
ELSE
0
END
AS BIT
) HasLogsWithGpsData
FROM SuperLogs
INNER JOIN Logs ON Logs.SuperLogId = SuperLogs.Id
GROUP BY SuperLogs.Id
The above query just gives me this error instead of working:
Column 'Logs.ScanMode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I want to check whether any of the ScanModes are in that list, not some accumulated version of them, so I don't want to pre-aggregate these values.
The Logs and SuperLogs tables have a one-to-many relationship. This means that for each record in SuperLogs there can be many records in Logs. This is why your query doesn't make sense and can't work as is.
The way I understand the question is this: If Any Log that belongs to the current SuperLog
have a ScanMode that is either one of the values in the list, you want to get 1. If none of them have a ScanMode that fits the list, you want to get 0.
If that is correct, a simple solution would be using conditional aggregation:
SELECT Name,
CAST
(
MAX(
CASE WHEN
Logs.ScanMode IN
(
'Navigational',
'Locatable',
'Automatic'
)
THEN
1
ELSE
0
END
)
AS BIT
) AS HasLogsWithGpsData
FROM SuperLogs
INNER JOIN Logs ON Logs.SuperLogId = SuperLogs.Id
GROUP BY SuperLogs.Id

oracle: grouping on merged columns

I have a 2 tables FIRST
id,rl_no,adm_date,fees
1,123456,14-11-10,100
2,987654,10-11-12,30
3,4343,14-11-17,20
and SECOND
id,rollno,fare,type
1,123456,20,bs
5,634452,1000,bs
3,123456,900,bs
4,123456,700,bs
My requirement is twofold,
1, i first need to get all columns from both tables with common rl_no. So i used:
SELECT a.ID,a.rl_no,a.adm_date,a.fees,b.rollno,b.fare,b.type FROM FIRST a
INNER JOIN
SECOND b ON a.rl_no = b.rollno
The output is like this:
id,rl_no,adm_date,fees,rollno,fare,type
1,123456,14-11-10,100,123456,20,bs
1,123456,10-11-12,100,123456,900,bs
1,123456,14-11-17,100,123456,700,bs
2,Next i wanted to get the sum(fare) of those rollno that were common between the 2 tables and also whose fare >= fees from FIRST table group by rollno and id.
My query is:
SELECT x.ID,x.rl_no,,x.adm_date,x.fees,x.rollno,x.type,sum(x.fare) as "fare" from (SELECT a.ID,a.rl_no,a.adm_date,a.fees,b.rollno,b.fare,b.type FROM FIRST a
INNER JOIN
SECOND b ON a.rl_no = b.rollno) x, FIRST y
WHERE x.rollno = y.rl_no AND x.fare >= y.fees AND x.type IS NOT NULL GROUP BY x.rollno,x.ID ;
But this is throwing in exceptions.
ORA-00979: not a GROUP BY expression
00979. 00000 - "not a GROUP BY expression"
The expected output will be like this:
id,rollno,adm_date,fare,type
1,123456,14-11-10,1620,bs
So could someone care to show an oracle newbie what i'm doing wrong here?
It looks like there's a couple different problems here;
Firstly, you're trying to group by an x.ID column which doesn't exist; it looks like you'll want to add ID to the selected columns in your sub-query.
Secondly, when aggregating with GROUP BY, all selected columns need to be either listed in the GROUP BY statement or aggregated. If you're grouping by rollno and ID, what do you want to have happen to all the extra values for adm_date, fees, and type? Are those always going to be the same for each distinct rollno and ID pair?
If so, simply add them to the GROUP BY statement, ie,
GROUP BY adm_date, fees, type, rollno, ID
If not, you'll need to work out exactly how you want to select which one to be output; If you've got output like your example (adding in an ID column here)
ID,adm_date,fees,rollno,fare,type
1,14-11-10,100,123456,20,bs
1,10-11-12,100,123456,900,bs
1,14-11-17,100,123456,700,bs
Call that result set 'a'. If I run;
SELECT a.ID, a.rollno, SUM(a.fare) as total_fare
FROM a
GROUP BY a.ID, a.rollno
Then the result will be a single row;
ID,rollno,total_fare
1,123456,1620
So, if you also select the adm_date, fees, and type columns, oracle has no idea what you mean to do with them. You're not using them for grouping, and you're not telling oracle how you want to pick which one to use.
You could do something like
SELECT a.ID,
FIRST(a.adm_date) as first_adm_date,
FIRST(a.fees) as first_fees,
a.rollno,
SUM(a.fare) as total_fare,
FIRST(a.type) as first_type
FROM a
GROUP BY a.ID, a.rollno
Which would give the result;
ID,first_adm_date,first_fees,rollno,total_fare,first_type
1,14-11-10,100,123456,1620,bs
I'm not sure if that's what you mean to do though.

Have to find a count of rows from my postgresql query

Hi I tried to get a count of rows from my below query:
select count(substring(wsresult_question FROM '[0-9]+') as pumporder) AS totals,
job_id,
job_siteid,
job_completed
from webserviceresults w, jobs s
where job_siteid = '1401'
and job_id = wsresult_jobid
and job_completed is not null
and wsresult_question LIKE 'job.job_site_data.site_meters.pump.%'
and wsresult_category = 'Job'
group by pumporder,job_id,job_siteid,job_completed order by job_completed desc
I tried this and i got the error like
There was an SQL error:
ERROR: syntax error at or near "as" LINE 1: ... count(substring(wsresult_question FROM '[0-9]+') as pumpord... ^
In this line substring(wsresult_question FROM '[0-9]+') as pumporder I just tired to get only a number from some concatenate strings. The concatenate string is being like
1.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtofast
2.job.job_site_data.site_meters.pump.0.meter_calibration_record.meter_adjustedtoslow
3.job.job_site_data.site_meters.pump.1.meter_calibration_record.meter_adjustedtofast
So substring(wsresult_question FROM '[0-9]+') as pumporder is return the numbers like 0,1 in array. I need to total the count of rows now. So Kindly help me on this.
Please let me know if you have any queries.
Thanks in advance!
your error means you should not create an alias for the function - only for the column, so if you remove as pumporder from count(substring(wsresult_question FROM '[0-9]+') as pumporder) , error will go away
Your approach though is very doubtful. If you want to count number of rows with substring(wsresult_question FROM '[0-9]+'), you better instead:
select count(1) AS totals,
job_id,
job_siteid,
job_completed
from webserviceresults w, jobs s
where job_siteid = '1401'
and job_id = wsresult_jobid
and job_completed is not null
and wsresult_question ~ '^(job.job_site_data.site_meters.pump.)[0-9]'
and wsresult_category = 'Job'
group by pumporder,job_id,job_siteid,job_completed order by job_completed desc
and lastly the string job.job_site_data.site_meters.pump.0 looks like json path, so it would be more appropriate using json array length function, not count on rows

OrientDB Equal Query not returning results

I ran a query like this:
select urlName from User where urlName like 'Nkj-Fm20%';
Which returns one record.
But when I run a query like this:
select * from User where urlName = 'Nkj-Fm20'
No record is found.
Can you help me?
the like operator allow usage of % jolly character, while the = doesn't. So with your second query it returns only the users with the exact urlName Nkj-Fm20.
Ivan