I have a table in Oracle Apex populated with contacts and their information. On the dashboard of my app, I'd like it to say how many contacts are in the database using the big list of values plug-in or something like that. I'm using the following code:
select contact_ID, count(*)
from
contacts
group by contact_id
However all I'm getting is the number of times each specific contact appears in the table (which is 1), instead of the total number of contacts. I understand what I did wrong in the code, but I'm not sure how to fix it.
For further information, the table columns are very basic, just 'Name', 'contact_id' (the primary key), 'Phone_Number', and so on. The table is called CONTACTS.
I'm also looking to eventually have the total number of organizations and comments on the dashboard as well.
Thanks!
Try
SELECT
COUNT(mycolumn) AS d,
COUNT(mycolumn) AS r
FROM
mytable
I think you just want count(*):
select count(*)
from contacts;
Presumably, contactID is unique in a table called contacts.
Related
I am building an application to manage an inventory and I have a problem when creating my tables for the database (I am using PostgreSQL). My problem is the following:
I have two tables, one called 'products' and one called 'users'. Each one with its columns (See image). I want to create a third table called 'product_act_register' , which will keep a record of activity of the products and has with it the columns id, activity_type, quantity, date. But, I want to add other columns which are taken from the table 'users' and 'products'.
It should look like this (Image)
Where product_id, product_name, product_category, product_unit are taken from the table 'products' and the column 'user_id' is taken from the table 'users'.
How can I do this with PostgreSQL ?
Your description and your precise goals are very unclear. You didn't tell us what should happen in the different cases (only a product exists or only a user exists or none exist or both exist). You also didn't tell us how the other columns not coming from these tables should be filled. You furthermore didn't tell us how the tables users and products depend on each other. Basically you con do something like this if you only want to do an insert if both tables have an entry:
INSERT INTO product_acts_register
SELECT 1,'ActivityType1',p.id, p.name, p.category,
p.unit, u.id, 100, CURRENT_DATE
FROM products p JOIN users u ON p.id = u.id;
(Since you didn't tell us how or if to join them, I assumed to join on their id column)
If you don't care about this, but want to insert an entry for any possible combination of users and products, you can just select both tables without joining:
INSERT INTO product_acts_register
SELECT 1,'ActivityType1',p.id, p.name, p.category,
p.unit, u.id, 100, CURRENT_DATE
FROM products p, users u;
You can replicate this here and try out other commands: db<>fiddle
Please be more precise and give us more information when asking the next question.
This sounds like a very simple query but I have never needed this calculation before. I'm using SQL Management Studio and SQL Server 2008.
I have a table ct_workers which contains individual employees and a second table cs_facilities which shows the sites that they work at.
The table ct_workers has a field person which is the primary ID for each employee and has a field facility which links the employees to cs_facilities via a field guid
I'm looking to display all workers that have 2 or more facilities.
I've though about using Excel or rownumber but surely that must be a simple efficient way of doing this?
Can anyone assist please?
Thanks,
You can use a GROUP BY with HAVING
SELECT cw.person
FROM ct_workers cw
GROUP BY cw.person
HAVING COUNT(DISTINCT cw.facility) >= 2
Your question suggests that you can use aggregation:
select w.person
from ct_workers w
group by w.person
having min(w.facility) <> max(w.facility); -- at least 2 values
However, if the person is the unique key in ct_workers, then a person can only be in one facility. So, your question would not make sense. You should actually have a junction table with one row per person and per facility.
I'm using Access to create a report. I have several duplicate records in my Avail_Amt field. I used the Query Wizard to de-dup records from a table, like this.
In (SELECT [DEALLOCALBAL] FROM [TBL_DATA] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
What I really want to do, though, is de-dupe the records in my Query, not in my Table. I think it should look like the script below, but it's not working.
In (SELECT [DEALLOCALBAL] FROM [dbo_LIMIT_HIST.Avail_Amt] As Tmp GROUP BY [DEALLOCALBAL] HAVING Count(*)=1 )
dbo_LIMIT_HIST is the Table in SQL Server and Avail_Amt is the Field that I am trying to de-dupe. Any idea what could be wrong here?
My data set looks like this:
Basically, for each Contact ID I could have multiple DEALLOCALBAL amounts. I want to capture each unique amount for each Contact ID, but I don't want dupes.
Thanks!!
CREATE TABLE users
()
CREATE TABLE avatars
()
CREATE TABLE weapons
()
I need two queries for these tables that I created and I am having trouble doing it, any help would be greatly appreciated.
For all Avatars produce a list showing the avatar name, the total number of weapons it currently holds and the total cost of all weapons currently held. I want no more extra information shown apart from these three main pieces, so I need to get information from table 2 and 3.
For any given User email (i.e. entered at runtime) list the owned Avatar names in reverse order of date of birth and the weapons (including weapon name, range and cost) held by that Avatar.
Any help would be greatly appreciated. I am using SQL*Plus. The tables are not very efficient so any improvements would be welcomed but mainly I need help with these queries.
select
max(a.name),
count(1),
sum(w.cost)
from
avatars a
inner join weapons w on w.weapon_id=a.no_wp_id
group by
a.no_wp_id
;
select
a.name,
w.weapon_name,
w.range,
w.cost
from
users u
inner join avatars a on a.user_id=u.id
inner join weapons w on w.weapon_id=a.no_wp_id
where
u.email='given#email.com'
order by
a.dateOfBirth desc
;
If your queries depend on email being unique, you should have a unique key on it.
Your weapons table does not appear to have a range column on it, but you implied it does in your requirement for query #2. I've included w.range anyway.
In avatars, no_wp_id should probably be named something like avatar_id or just id, and in weapons, weapon_id should be named avatar_id.
You should go through an SQL tutorial. Here's one: http://www.tutorialspoint.com/sql/sql-overview.htm.
I have a very simple MS Access User Table (USER_TABLE) consisting of 3 fields: Customer_Number, User_Name, and Email_Address. I have another table (NEW_USERS) that consist of new requests for Users. It has a User_Status field that is blank by default, and also has the Customer_Number, User_Name, and Email_Address fields.
Half of the new requests that come through are users already existing, so I want to set up a query that will check the USER_TABLE to determine if a new request exists or not, using the Email_Address field checked vs. the Customer_Number field. Complicating this is the fact that 1) Customer_Number is not unique (many Users exists for a single Customer Number) and 2) Users can have multiple accounts for different Customer Numbers. This results in 4 scenarios in the NEW_USERS table when checking vs. the USER_TABLE:
Email_Address does not exist for Customer Number in USER_TABLE (New)
Email_Address exists for Customer Number in USER_TABLE (Existing)
Email_Address does not exist for Customer Number in USER_TABLE, but exists for other Customer Numbers (New-Multi)
Email_Address does exist for Customer Number in USER_TABLE, and also exists for other Customer Numbers (Existing-Multi)
What I would like to do is run these checks and enter the corresponding result (New, Existing, New-Multi or Existing-Multi) into the User_Status field.
This seems like it would be possible. Is it possible to run 4 separate queries to make the updates to NEW_USERS.User_Status?
When you're working in Access, you really need a field that uniquely identifies each record. At the very least, some combination of field, like customerid and email.
Beyond that, since you have a few criteria to satisfy, the easiest way is probably to make a single select statement that compares data between the results of multiple select statements. Look into outer joins for picking the results from one table that are not found in another. Something like -
insert into user_table select customerid, email_address from
(select customerid, email_address from new_users inner join user_table on ...) as expr1,
(select customerid, email_address from new_users outer join user_table on ...) as expr2
where expr1.customerid = expr2.customerid and expr1.new_users = expr2.new_users
I recommend trying out the free stanford course on sql, theres a handy lesson on nesting your select statements - its a good way to get results that fit a lot of criteria. http://class2go.stanford.edu/
As an aside, they do a lot using syntax of 'specifying joins in the where claus' which is increasingly frowned upon, but much easier to understand.