Yes you can do it ok with the sequel gem.
require 'sequel'
DB = Sequel.sqlite # memory database
DB.create_table :users do
primary_key :id
String :name
end
users = DB[:users] # Create a dataset
users.insert(:name => 'jim')
DB.create_table :contacts do
primary_key :id
String :name
end
contacts = DB[:contacts] # Create a dataset
contacts.insert(:name => 'first')
DB['select (select count(*) from users where name = "jim") as users,
(select count(*) from contacts where name = "first") as contacts'].each do |row|
puts row
end
#{:users=>1, :contacts=>1}
It should be noted however that it is not a great idea to include raw strings in a sequel method.
They can be extracted as follows:
DB['select (select count(*) from users where name = ?) as users,
(select count(*) from contacts where name = ?) as contacts, 'jim', 'first'].each do |row|
puts row
end
Also as noted in another answer you could express this query completely without resorting to SQL which is more in the spirit of the module. :
dataset = DB.select {[
DB[:users].where(name: 'jim').select { count('*') }.as(:users),
DB[:contacts].where(name: 'first').select { count('*') }.as(:contacts)
]}
dataset.sql
# SELECT (SELECT count('*') FROM `users` WHERE (`name` = 'jim')) AS 'users', (SELECT count('*') FROM `contacts` WHERE (`name` = 'first')) AS 'contacts'
dataset.first
# {:users=>1, :contacts=>1}