How do i list all entries where a specific value in a column does not exist? - sql

Basically, I have a table called Client Entity that contains an Entity Type column and I have an entity type called BEN. I want to show all records where this particular type does not exist. The issue is that the record itself may not exist if the entity type has not been appointed so I am basically asking the query to show me where no 'rows' exists because of this value...
Basically I'm looking for a code to highlight the absence of data, not NULL values.
Can anyone help ?

If you have a table of "records", then you would use not exists:
select r.*
from records r
where not exists (select 1
from cliententity ce
where ce.record_id = r.record_id and
ce.entity_type = 'BEN'
);
I don't know what your real table or column names are, but this gives the idea on how to approach the problem.

Related

Create column name based on value without execute

I need to create a column name based on the value of other columns. I need to return a value from a column, but the specific name depends on the value insert on other table.
From intance:
Table A
Column1 | Column2
1 2
Base on that values I need to go to the table B to the column "VE12".
I need this dynamiclly, so the execute(#query) is my last option and I would like to avoid CASE WHEN statments because I have more than 50 options.
My query will be something like:
select case when fn.tab=8 and fo.pais=3 then cp.ve83 end
FROM fn
INNER JOIN fo ON fo.stamp = fn.stamp
INNER JOIN cp
If the value in the column tab is 8 and the value in column pais is 3 I should return the value in column ve83.
Thanks for all the help!
The only sensible option is to go back to the business meaning of the data and redesign the database according to that, instead of according to "technique-oriented abstractions" such as these that SQL was never intended to support.
The main reason for this is that SQL was founded on FIRST order logic, and this precludes supporting stuff like varying domains. Which you are doing (or at least seeking to do) because ve12 could be a DATETIME and ve83 could be a VARCHAR and ve56 coulb be a BLOB etc. etc. So there is just no way for you [or anyone else] to determine the data type of the results in your query, and it is even more impossible to attach meaning to what comes out of your desired query precisely because of this varying-domain and varying-source characteristic.

How can I pull off an INSERT, SELECT, JOIN, and CAST query with Microsoft SQL Server Management Studio?

I have been trying to set up a test database but I keep running into issues with pulling in the data from its normalized form.
Below is the latest version of the SQL query I've been working on.
INSERT INTO TestData.dbo.Info (Name,Did)
SELECT DISTINCT a.Name, b.Did
FROM StageDB.dbo.MockData a INNER JOIN Testdata.dbo.Dinfo b
ON a.Name = CAST(b.Did as varchar(10))
The output I get is the following:
(0 row(s) affected)
I've been trying to monkey around with it on my own but can't seem to make it work the way I want to.
My objective here is to pull data (the primary key from a table with data already in my database, Did from TestData.dbo.Dinfo that is of int type) and merge it with data from my staging table (a particular column from the table in the staging database, StageDB.dbo.MockData, Name of type varchar(10)), then inserting into a new table on my main database. The database table I'm trying to put these things into is all set up with the correct fields and types (primary key column, auto generated as rows are added, Name column that is varchar(10), and Did column that is int).
EDIT: Table Definitions, Sample Data, Desired Result
Destination Table:
TestData.dbo.Info
Columns: Iid (int, primary key of table set to auto increment as new records are added), Name (varchar(10)), Did (int, foreign key from TestData.dbo.Dinfo).
StageDB.dbo.MockData
Columns: Many columns exist in this table that are not relevant to what I am trying to pull off. The only one I am interested in is the column containing names that I want to tie together with information from the Dinfo table. Name (nvarchar(255),null).
TestData.dbo.Dinfo
Columns: Did (int, primary key), Donor (varchar(20)).
Sample of Data
From Dinfo:
Did Donor
01 Howard L
From MockData:
Name
Smith J
Desired Results
Iid Name Did
01 Smith J 01
Any help or advice would be much appreciated. I would really like it if someone can show me the correct SQL syntax for this as I think it may just be a matter of writing it correctly. Additionally, any tips or websites that can help me learn more SQL would be appreciated.
Thank you!
Change this:
ON a.Name = b.Did
To this:
ON a.Name = CAST(b.Did as varchar(10))
I suspect there's a lot more wrong with your query in terms of getting the results you want, but this should fix your error.
You need to figure out where the error is occurring. There are three possibilities:
mockdata.name is a string and NInfo.data is an integer
dinfo.did is a string and NInfo.did is an integer
mockdata.name is a string and dinfo.did is an integer (or vice versa)
Based on the naming conventions, the third is the most likely. When a number is compared to a string, the string is converted to a number. However, you need to be careful whenever you use implicit type conversions.
If the third option, then you can convert the integer to a string (as other answers propose). However, I would ask why you are doing such a comparison.
Error is in ON a.Name = b.Did Name column may contains alphabets,number or special characters. Did column contains only number or integers only.

PostgreSQL match string to dynamically entered string

I have a varchar field in my database table A let's call it store_name, this field gets its value from entity A, now entity B enters store_name into a different database table B now I want to get all records in table A where the store_name matches the values in table B.
How would you recommend me doing the query as I don't control the values of those 2 fields?
What do you think about PostgreSQL fuzzystrmatch?
The tables contain thousands of records.
Thanks
Assuming that both table A and table B are in the same database. And I guess since you don't control insertion of data, you are not sure if the values are of same case or there may be a spelling mismatch.
Case 1: If the problem is only of case-mismatch, you can use ilike:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name
Case 2: If you also want to check for spelling mismatch, but words sound similar, then after installing postgresql-contrib package and creating extension fuzzystrmatch, you can use:
Select a.store_name
from a, b
Where a.store_name ilike b.store_name OR
soundex(a.store_name) = soundex(b.store_name)
If you are dealing with names, which may not always be in English, it may be more appropriate to use metaphone or dmetaphone function instead of soundex.
Documentation: Fuzzystrmatch
If you want matching you can use a straight up join.
Select a.store_name
from a
join b on a.store_name = b.store_name;
If you want to use fuzzy matching just use the various functions available in the join criteria. Documentation here
Note: there are some limitations to Fuzzy string matching so i would advise testing each out on values that you either know match or don't.

Access 2010 SQL - UPDATE query not working

I need to create a query for updating a column in a table with values taken from another table and matching a field.
These are the 2 tables:
tblMain
ID Autonumbering
Key Text
Stat1 Integer
tblStat1
ID Autonumbering
Key Text
Freq Integer
I want to UPDATE the tblMain.Stat1 column with tblStat1.Freq value on each record in which tblMain.Key = tblStat1.Key.
I tried this syntax (found somewhere as an example)
UPDATE tblMain
SET tblMain.Stat1 = tblStat1.Freq
WHERE tblMain.Key = tblStat1.Key;
This doesn't work and returns an error on the 2nd row.
After some trials I found that the correct syntax (built with the Access query generator) is this:
UPDATE (tblMaibn INNER JOIN tblStat1 ON tblMain.Key = tblStat1.Key)
SET tblMain.Stat1 = tblStat1.Freq;
In this 2nd syntax, there is no trace of the WHERE condition.
Can someone help me to understand what's wrong with the 1st syntax.
Since I'm building a new table (the join), how can it work on tblMain?
As I said, I found the wrong syntax as an example of UPDATE statement.
Thank you in advance.
Bye,
Ivano
What is happening in your first query on the 2nd row, is that Access isn't aware of what tblStat1 represents in your query.
The reason your 2nd query is working is because it uses an inner join on the relevant key. In order for SQL to be aware of what record in tblMain relates to which record in tblStat1, you need to use a join.
You can see in the generated code that it is updating your desired table, but joining onto the second table. The where condition is redundant as you're updating every record.
In 1st syntax, you can change:
UPDATE tblMain
SET tblMain.Stat1 = (SELECT Freq
FROM tblStat1
WHERE tblMain.Key = tblStat1.Key)

Typecheck SQL query

Is there any relational database that can output the return type of a query before running it? As an example, a query like this GIVE_TYPES SELECT name, age FROM person would give a result like VARCHAR(255), INTEGER without actually executing the query. If this is not a possibility, why is that the case?
EDIT
The first comment made me realize that I need to give a slightly more complicated use case. Imagine if the query were something like this:
SELECT parent_name, COUNT(name) FROM person GROUP BY parent_name;
To select the names of all parents and the number of children they have. I would expect something like VARCHAR(255), INTEGER as the result for this as well, but a column inspection would not let me know about COUNT's return type.
Count's return type always is int.
http://msdn.microsoft.com/en-us/library/ms175997.aspx
If you are running on top of persistent and esqueleto, then I don't think you're going to have simple access to this sort of information. In particular, I assume any unrecognized PostgreSQL types just get mapped to a Haskell String or Text.
About the best you can do is:
CREATE TEMP TABLE t1 AS LIMIT 0
SELECT * FROM information_schema.columns WHERE table_name='t1' AND schema_name=...
DROP TABLE t1;
You'll want to identify the temporary schema-name for your table t1 (in the format pg_temp_xxx).
This (plus perhaps some follow-up queries on the information-schema for type details) should give you details on all columns of your result-set.