SQL query for similar attribute in three tables with no overlap - postgresql

In our data, users' languages are stored in three fields: UI language in "core user" table, preferred language in "core profile" table and spoken language "language" table.
I am trying to write a SQL query that calculates all Spanish speakers (ES) from UI, Spanish speakers from preferred language (ES) and Spanish speakers (SPA) with no duplicates.
I think the best way is users that are ES from "core user" UI who are NOT in "core profile" ES preferred language and are also NOT in "language" SPA, with ES preferred language AND SPA (all). But I am not sure.
Any tips on how to do this?

You should do something like this:
with spanish_users as(
select user_id from core_user
where "UI_language" ='ES'
union
select user_id from core_profile
where preferred_language ='ES'
union
select user_id from "language"
where spoken_language ='SPA')
select count(distinct user_id) from spanish_users;
working example here https://www.db-fiddle.com/f/d196Ht6fxqd1Lw71cUyYUS/1

Related

Full text search when words have the same start

I'm new to full text search engine, I have a table filled with french and english words, but I'm having wierd issues when I try to implement the requests. The goal is to do a search engine with auto completion. Currently there the where are made with ILIKE but I heard that it's not 'scalable'
Let's say that I have a book table :
CREATE TABLE t_books (
title TEXT
);
INSERT INTO t_books VALUES('Admin');
INSERT INTO t_books VALUES('Administratif');
INSERT INTO t_books VALUES('Adminare');-- no french words
INSERT INTO t_books VALUES('Admininids');
INSERT INTO t_books VALUES('Admin2');
When I run this query :
SELECT b_id
FROM (
SELECT title as b_id,
to_tsvector('french',title) as document
FROM t_books
) p_search
WHERE p_search.document ## to_tsquery('french','admin');
With the likes, I get all the 5 rows, but with this query this only returns "Admin".
However, it looks like it's works better with english words (those above are french and english, for example with the titles 'book','booking','booked'; when i'm looking up for book it returns the 3 rows.
http://rextester.com/RKNE72921
What is wrong there ? Is it a lack of french words in the dictionnary ?

Redshift: Max items within "IN clause"?

I have a query like:
SELECT count(id), pro.country_code
FROM profiles AS pro
WHERE id IN (SELECT profile_id FROM reports)
GROUP BY pro.country_code;
My questions:
How many items can you use in a Redshift IN CLAUSE? Storing the actual ids instead of the sub-sql statement has got to be faster for performing that outer query each time, right?
From what I know, there is no limit but if you going to bring a lot data you can use exists.
SELECT count(id),
pro.country_code
FROM profiles AS pro
WHERE exists (SELECT profile_id FROM reports where pro.id=reports.profile_id)
GROUP BY pro.country_code;
It should be much more faster
Also you can use intersect instead of in
As "user" already stated, your best performance will be with a WHERE EXISTS clause and subquery. Since you mentioned performance as an important consideration, I should also point out that the more important performance factor would like be your table distribution. In order for this to perform well, you'll want to double check that both tables have the column "profile_id" as the distribution key and that both tables have declared the column using the same data type.

SSRS 2005 Many to Many select

I have customers and programs. A customer can be in many programs and I have the CustomerPrograms table for the many to many data. I of course also have the Customers table and the Programs table. In SSRS the client wants to be able to Multi Select the programs parameter (query loaded from Programs table). They also want to see a comma delimited list of the programs in the grid. I wrote a UDF to handle the grid part but I am baffeled about how to handle the Multi Select parameter filtering customers who can be in none, one or many of the programs.
I actually figured it out and it turned out to be much easier than I thought - for those interested part ot the where clause now looks like this (#program is the multi select parameter):
and exists (select custprog_id from customers_programs (nolock)
where cust_id = customers.cust_id and program_id in (#program))

How to select values in oracle10g database?

For example my table contains a column 'skills'.
i want to search the name who having skills in java and oracle.
I know its very simple,i am very new to oracle10g,please help me.
i tried like
select name from table_name where skills='java' or skills='java';
the name 'c' having skills in java and html,how can i select ?
select name from table_name where skills='java,html';
i dont know which method i wanna use,please help me.
This will return any name that has java in its skills list.
select name from table_name where ','||skills||',' like '%,java,%'
select name from table_name where skills like '%java%' or skills like '%oracle%';
That will give you names who have skills in java or oracle. If you want names that have both at the same time, change the "or" for "and".
as shown in your table if you want to select names whose skills are either java or oracle you can do select name from table_name where skills like '%java%' or skills like '%oracle%' this will select all the names whose skills contains java or oracle

SQL query question

I have a table that has about 6 fields. I want to write a SQL statement that will return all records that do not have "England" in the country field, English in the language field & english in the comments field.
What would the sql query be like?
Well, your question depends a lot on what DBMS you're using and what your table set up looks like. This would be one way to do it in MySQL or TSQL:
SELECT *
FROM tbl
WHERE country NOT LIKE '%England%' AND language NOT LIKE '%english%'
AND comments NOT LIKE '%english%';
The way you word your question makes it sound like all these fields could contain a lot of text, in which case the above query would be the way to go. However, more likely than not you'd be looking for exact matches in a real database:
SELECT *
FROM tbl
WHERE country!='England' AND language!='english'
AND comments NOT LIKE '%english%';
Start with this and modify as necessary:
SELECT *
FROM SixFieldTable
WHERE Country <> 'England'
AND language <> 'english'
AND comments NOT LIKE '%english%'
Hope this helps.
Are you wanting something like
select * from myTableOfMadness
where country <> 'England'
and language <> 'English'
and comments not like '%english%'
Not sure if you want 'and's or 'or's, or all 'not' comparisons. Your sentence structure is somewhat misleading.
The above solutions do not appear to account for possible nulls in the columns. The likes of
Where country <> 'England'
will erroneously exclude entries where Country is null, under default SQL Server connection settings.
Instead, you could try using
IsNull(Country, '') <> 'England'
To ignore case:
SELECT *
FROM SixFieldTable
WHERE LOWER(Country) <> 'england' AND
LOWER(language) <> 'english' AND
LOWER(comments) NOT LIKE '%english%'
Try This
Select * From table
Where Country Not Like '%England%'
And Language Not Like '%English%'
And comments Not Like '%English%'