I need a parse/obj-c alternative preferably server side or client side (iOS) to GROUP BY function in sql.
I understand that there is no GROUP BY function in parse. On SQL I would execute:
select count(*), name, organization from table GROUP BY name
And I would get the number of entries for each unique name.
Since I cant do that on Parse, I am stuck after querying the table.
I have an NSArray whose objects are NSDictionaries which I can access by [valueforKey:#"name"] etc
Name Organization
a b
a b
a b
c d
c d
I need a result table such as
Name Organization Count
a b 3
c d 2
Is it possible at all on parse? If not how can I get this done on obj-c?
Related
I am trying to compare a series of tables within an access database, 2 local and one linked.
Table A (local) contains UserID, Title, Position; Table B (linked) contains UserID, Title, and Position from the previous week (records could possibly change on a week to week basis); Table C (local) contains UNIQUE UserID's and Titles.
I need to ensure that all UserID's contained in Table C still exist
in Table A.
I need to ensure that all UserID's contained in Table C have not had
a change in Title or Position from the previous week. If so Add to a temp table.
I'd prefer to use Access VBA or SQL in accomplish this task and the information will be displayed in a report.
Basically the same logic for both examples. use a left join to to identify mismatches.
Identify missing users in A
Insert into TableA (userID,Title)
select TableC.UserID, TableC.Title
from TableC
left join TableA on TableC.UserID=TableA.UserID
where TableA.UserID is null
Identify changes from B to A
insert into temp (userID,title,position)
select c.userID,c.title,c.position
from TableA a
left join tableB b on b.userid=a.userID and b.title=a.title and b.position=a.position
where b.userID is null
I have 2 tables, clients and contact numbers. Each client has one or many contact number, its a one to many relationship. And I need to make an excel document that for each row it has one client and its contact numbers. For example:
client name | contact_number_1 | contact_number_2| ...
I want to make it in POSTGRESQL to be fast. Doesn't matter the way that I make the excel file. I just need the query to make the rest.
Thank you!
If you can parse the result and create the Excel file from there, the most flexible solution is to aggregate the numbers into an array:
select c.client_id,
c.client_name,
array_agg(cn.number) as contact_numbers
from client c
join concat_number cn on cn.client_id = c.client_id
group by c.client_id, c.client_name;
Another alternative is to use string_agg(cn.number, ',') to get a comma-separated list (but the array is more robust against embedded commas in the names).
If you really do need to get the numbers in separate columns, you need to decide on a sensible upper limit of columns, then you can use the first query and extract the array elements as columns:
select client_id,
client_name,
contact_numbers[1] as contact_number_1,
contact_numbers[2] as contact_number_2,
contact_numbers[3] as contact_number_3,
...
from (
select c.client_id,
c.client_name,
array_agg(cn.number) as contact_numbers
from client c
join concat_number cn on cn.client_id = c.client_id
group by c.client_id, c.client_name
) t
If you actually want a dynamic number of columns returned, it gets a bit complicated cause you have to know the maximum number of columns for the returned results, or you hard-code a set number for the highest number you think will exist.
If you can live with having one column represent all of the possible contacts, then you can aggregate them all into a single column:
select c.clientName, STRING_AGG(COALESCE(con.contact_number,''),'|') as contact_numbers
from clients c
left join contacts con on c.clientId = con.clientId
group by c.clientName
order by c.clientName
I'm working on a service type database. Customers can have many equipment types and many service calls.
I'm trying to make a query that selects the customers name, address, etc when certain parameters are met with relation to the equipment types.
A customer can have upto 5 different equipment types. Some only have 1, some have all 5.
My query is trying to find customers with equipment type 3 installed, but not equipment type 5. The equipment type 3 must also have a service date between to date fields on my search form.
I'm drawing a blank when it comes to writing this query. I was able to query the customers with equipment type 3 and specific service dates, but I can't seem to be able to eliminate the customers that also have equipment 5 installed, even if they also have equipment type 3 installed.
This is my SQL:
SELECT
tblCustomers.WCWF_ID,
tblCustomers.CustBusiness,
tblCustomers.CustLastName,
tblCustomers.CustFirstName,
tblCustomers.CustAddress,
tblCustomers.CustCity,
tblCustomers.CustST,
tblCustomers.CustZip5,
tblEquip.EquipResinDate,
tblEquip.EquipType,
tblCustomers.CustPostCard
FROM
tblCustomers
INNER JOIN
tblEquip ON tblCustomers.WCWF_ID = tblEquip.WCWF_ID
WHERE
(((tblEquip.EquipResinDate) Between [forms]![MailSearchSelect]![StartDate] And [forms]![MailSearchSelect]![EndDate])
AND ((tblEquip.EquipType)=3
AND (tblEquip.EquipType)<>5)
AND ((tblCustomers.CustPostCard)=True));
Any help would be greatly appreciated.
The problem with your query is that you are tying to use a single JOIN to filter in customers with equipment type 3 and filter out customers with equipment type 5 : you would need to split that logic into two distinct parts.
To pull out customers that have equipment type 3 installed, using a JOIN is fine (I just moved the related conditions from the WHERE clause to the JOIN, for more clarity). It would also have been possible to express this requirement as a WHERE EXISTS condition with a correlated subquery.
To exclude customers that have equipment type 5 installed, you can either :
use a NOT EXISTS condition in the WHERE clause with a correlated subquery (I chose that option)
or use a LEFT JOIN with WHERE ... IS NULL.
Query :
SELECT
c.WCWF_ID,
c.CustBusiness,
c.CustLastName,
c.CustFirstName,
c.CustAddress,
c.CustCity,
c.CustST,
c.CustZip5,
e.EquipResinDate,
c.EquipType,
c.CustPostCard
FROM
tblCustomers AS c
INNER JOIN tblEquip AS e
ON e.WCWF_ID = c.WCWF_ID
AND e.EquipType = 3
AND e.EquipResinDate
BETWEEN [forms]![MailSearchSelect]![StartDate]
AND [forms]![MailSearchSelect]![EndDate])
WHERE
c.CustPostCard = True
AND NOT EXISTS (
SELECT 1
FROM tblEquip AS e2
WHERE
e2.WCWF_ID = c.WCWF_ID
AND e2.EquipType = 5
)
PS : it is also a good idea to give alias to the table names ; it makes the query more readable and may avoid subtle bugs caused by name clashes when the same table is referenced more than once in the query.
I am a bit stuck and would appreciate some help.
I have the following data structure as shown in the image below.
So I want to link C and D basically apply the list to list. And if possible how would I do this in ef core. I did try a different query
SELECT * FROM A a
JOIN B b on a.PKA= B.PKB
But I only get the multiplication of list every record from table B got a pair for every record of table A. One more question if records in table C don't have the same number of record in table D it values should be null
I have a table of 1000 emails with unique identifiers and 5 other tables with matching unique identifiers. I am trying to email attachments from the first table the attachments from the remaining 5 table. one issue is that all information is store on the 5 tables. is there code I can write to "automate" the emailing without having to write thousands lines of code?
You did not provide sample data. I assume by 'emails' you mean email addresses.
Let's say the table with mail addresses is called 'accounts'. It is not clear why there are 5 other tables. Let's assume they are called a, b, c, d and e. The information in these tables is stored in field 'info'. You did not provide information on the type of database (access, mysql, postgres, oracle, ms-sql), so I am going to use as generic SQL as possible. Let's assume the id field in accounts is called 'id' and the corresponding field in the other tables is called 'accountid'. (Edit:)Let's assume the other tables do not all reference all 1000 accounts.
select accounts.email, a.info, b.info, c.info, d.info, e.info
from accounts left join a on accounts.id=a.accountid
left join b on accounts.id=b.accountid
left join c on and accounts.id=c.accountid
left join d on account.id=d.accountid
left join e on account.id=e.accountid;
Note that for some accounts some of the info fields may be empty (NULL) if the corresponding table does not have a row referencing the account.