This is what I am working with:
Select fields 1-24 dbo.tablename where multiple !=2;
However, in the original table, tablename, some fields are titled differently. This is a work in progress so pardon some of the inefficiencies. One example is the 6th column/field, which is titled xacct in tablename, but titled xaccount in result.
In short: Can one set up a command such as above and still account for differing field names all the while leaving the data and it's type unchanged?
if you are doing an insert/select, column names are irrelevant, only order is important. If you are trying to rename the columns in a select statement, use the standard SQL:
SELECT field1 AS column1
, field2 AS column2
, field3 AS column3
, multiple AS multiply
FROM dbo.tablename
WHERE multiple != 2;
Where 'FIELD1' is original column name, 'COLUMN1' is new name you are providing.
You don't have to specify new names for all of the columns, only those you are changing:
SELECT field1 AS tedd_e_bear
, field2
, field3 AS spin_fidget
, multiple AS multiply
FROM dbo.tablename
WHERE multiple != 2;
In the above example, field 2 still has the name field2.
Related
I have a database table with four columns:
Column 1 | Column 2 | Column 3 | Column 4
The value in each of those four columns will either be a price such as ($3.99) or will have the value 'Listed'.
I would like to know the best SQL query to find the following:
Get all rows where all four rows equal 'Listed'
Get all rows where some of the columns have prices and some have the value 'Listed'
Try this Query !
When all four rows equals to 'Listed' :
SELECT *
FROM [table name]
WHERE
Column1='Listed'
AND
Column2='Listed'
AND
Column3='Listed'
AND
Column4='Listed'
When some of the columns have prices and some have the value 'Listed' :
SELECT *
FROM [table name]
WHERE
(
Column1='Listed'
OR
Column2='Listed'
OR
Column3='Listed'
OR
Column4='Listed'
)
AND
(
Column1 LIKE '$%.%'
OR
Column2 LIKE '$%.%'
OR
Column3 LIKE '$%.%'
OR
Column4 LIKE '$%.%'
)
These are actually fairly simple queries
Select where all 4 columns are listed.
SELECT * FROM {table}
WHERE Column1='Listed' AND Column2='Listed' AND Column3='Listed' AND Column4='Listed'
Select where at least one column is listed and at least one column is a price(numeric)
SELECT * FROM {table}
WHERE (Column1='Listed' OR Column2='Listed' OR Column3='Listed' OR Column4='Listed')
AND (ISNUMERIC(Column1) OR ISNUMERIC(Column2) OR ISNUMERIC(Column3) OR ISNUMERIC(Column4))
Something like these should give you what you want, no guarantees though since different database engines use some slightly different SQL.
I think you should do something like this:
SELECT * FROM `testing` WHERE Col1='Listed' AND Col2='Listed' AND Col3='Listed' AND Col4='Listed'
And for the "OR" statement:
SELECT * FROM `testing` WHERE Col1='Listed' OR Col2='Listed' OR Col3='Listed' OR Col4='Listed'
Of cours you'll need to replace Col1, Col2, Col3 and Col4 with your Column names. Also change your table name.
I have a table with, let's say, 100 records. The table has two columns. The first column (A) has unique values. The second column (B) has NULL values
For 4 elements from column A I'd like to associate some earlier defined values, and they are unique as well.
I don't care about which value from column B will be associated with the value from column A. I'd like to associate 4 unique values with another 4 unique values. Basically, like I'd cut and paste a block of values from one column to another in excel.
How can I do it without using cursors?
I'd like to use one Update statement for ALL rows instead one Update statement for EVERY row as I do now.
Try this:
UPDATE t
SET ColumnB = BValue
FROM Table t
INNER JOIN
(
SELECT 1 AValue, 'Mouse' BValue UNION
SELECT 2, 'Cat' UNION
SELECT 3, 'Dog' UNION
SELECT 4, 'Wolf'
) PreDefined ON(t.ColumnA = PreDefined.AValue)
Use any number you want in the 'PreDefined' table, as long as they are unique and within the range of values in columnA of your original table.
If you are only trying to fill a table for testing purposes, I guess you could:
A) Use the value from Column A itself (as it is already unique).
B) If they are to be different, use some function on the column A's value to obtain a column B value (something simple, like (ColumnA * 10), and this would give youA)
C) Create a temp table with a "dictionary" setting a B value for each possible A value, and then update the rows desired on your table looking up from values on this dictionary table.
Anyway, if you explain a little further your purpose it will be easier to try suggesting you a solution.
if your animal data is already in a database table, then you can use a single update statement like this:
update target_table t4
set columnb = (
select animal_name
from (select columna, animal_name
from (select rownum rowNumber, animal_name from animal_table) t1
join (select rownum rowNumber, columna from target_table t1 where columnb is null) t2
on t1.rowNumber = t2.rowNumber
) t3
where t4.columna = t3.columna
)
;
this works by selecting a sequence number and animal name from the source table, then selecting a sequence number and columna value from your target table. by joining those records on the sequence number you guarantee you get exactly 1 animal name for each columna value. you can then join those columna-to-animal records to your target table to do an update of columnb.
for more background on updating one table from values in another, you might consider the solutions presented here: Update rows in one table with data from another table based on one column in each being equal. the only difference is that in your example, you do not have any column that matches between your target table and your animal names table, so you need to use the rownum to create an arbitrary 1-to-1 matching of records.
if your unique options are in a text file or spreadsheet, then you can format them into a fixed-width space-padded string and pick the one you want using the rownum index like so:
update table_name
set columnb = trim(substr('mouse cat dog wolf ', rownum*6-6, 6))
where columnb is null;
I have a a large customer database where customers have been added multiple times in some circumstances which is causing problems. I am able to use a query to identify the records which are an exact match, although some records have slight variations such as different addresses or given names.
I want to query across 10 fields, some records will match all 10 which is clearly a duplicate although other fields may only match 5 fields with another record and require further investigation. Therefore i want to create a results set which has field with a count how many fields have been matched. Basically to create a rating of the likely hood the result is an actual match. All 10 would be a clear dup but 5 would only be a possible duplicate.
Some will only match on POSTCODE and FIRSTNAME which is generally can be discounted.
Something like this helps but as it only returns records which explicitly match on all 3 records its not really useful due the sheer amount of data.
SELECT field1,field2,field3, count(*)
FROM table_name
GROUP BY field1,field2,field3
HAVING count(*) > 1
You are just missing the magic of CUBE(), which generates all the combinations of columns automatically
DECLARE #duplicate_column_threshold int = 5;
WITH cte AS (
SELECT
field1,field2,...,field10
,duplicate_column_count = (SELECT COUNT(col) FROM (VALUES (field1),(field2),...,(field10)) c(col))
FROM table_name
GROUP BY CUBE(field1,field2,...,field10)
HAVING COUNT(*) > 1
)
SELECT *
INTO #duplicated_rows
FROM cte
WHERE duplicate_column_count >= #duplicate_column_threshold
Update: to fetch the rows from the original table, join it against the #duplicated_rows using a technique that treats NULLs as wildcards when comparing the columns.
SELECT
a.*
,b.duplicate_column_count
FROM table_name a
INNER JOIN #duplicated_rows b
ON NULLIF(b.field1,a.field1) IS NULL
AND NULLIF(b.field2,a.field2) IS NULL
...
AND NULLIF(b.field10,a.field10) IS NULL
You might try something like
Select field1, field2, field3, ... , field10, count(1)
from customerdatabase
group by field1, field2, field3, ... , field10
order by field1, field2, field3, ... , field10
Where field1 through field10 are ordered by the "most identifiable/important" to least.
This is as close I've got to what i'm trying to achieve, which will return all records which have any duplicate fields. I want to add a column to the results which indicate how many fields have matched any other record in the table. There are around 40,000 records in total.
select * from [CUST].[dbo].[REPORTA] as a
where exists
(select [GIVEN.NAMES],[FAMILY.NAME],[DATE.OF.BIRTH],[POST.CODE],[STREET],[TOWN.COUNTRY]
from [CUST].[dbo].[REPORTA] as b
where a.[GIVEN.NAMES] = b.[GIVEN.NAMES]
or a.[FAMILY.NAME] = b.[FAMILY.NAME]
or a.[DATE.OF.BIRTH] = b.[DATE.OF.BIRTH]
or a.[POST.CODE] = b.[POST.CODE]
or a.[STREET] = b.[STREET]
or a.[TOWN.COUNTRY] = b.[TOWN.COUNTRY]
group by [GIVEN.NAMES],[FAMILY.NAME],[DATE.OF.BIRTH],[POST.CODE],[STREET],[TOWN.COUNTRY]
having count(*) >= 1)
This query will return thousands of records but I'm generally interested in the record with a high count of exactly matching fields
Below is a table im working with. I would like to take the field from [Field1] column and move directly across to [Field2] column.
Results would be like this
Field2
Browser
com.android.browser
com.android.browser.BrowserActivity
Email
com.android.email
com.android.email.activity.Welcome
Phone
com.android.contacts
com.android.contacts.activities.PCUDialtactsActivity
Gallery
com.android.gallery3d
com.android.gallery3d.app.Gallery
Tắt mà n hình
com.katecca.screenofflockdonate
com.katecca.screenofflockdonate.MainHelper
Messaging
com.android.mms
com.android.mms.ui.ConversationList
Simple mode
com.pantech.app.skysettings.simplemode
com.pantech.app.skysettings.simplemode.simplemodesettingshrotcutActivity
This might do what you need:
SELECT iif(isnull(Field2), Field1, Field2) as [yourColumnTitle]
FROM yourTableName
Aside from this you'll need to add a column to the table datatype=YesNo and set it to true on the rows you want to affect, then you can
SELECT iif(NewColumn <> 0, Field1, Field2) as [yourColumnTitle]
FROM yourTableName
I have one mdb table with the following structure:
Field1 Field2 Field3 Field4
A ...
B ...
I try to use a query to list all the different fields of row A and B in a result-set:
SELECT * From Table1
WHERE Field1 = 'A'
UNION
SELECT * From Table1
WHERE Field1 = 'B';
However this query has two problems:
it list all the fields including the
identical cells, with a large table
it gives out an error message: too
many fields defined.
How could i get around these issues?
Is it not easiest to just select all fields needed from the table, based on the Field1 value and group on the values needed?
So something like this:
SELECT field1, field2,...field195
FROM Table1
WHERE field1 = 'A' or field1 = 'B'
GROUP BY field1, field2, ....field195
This will give you all rows where field1 is A or B and there is a difference in one of the selected fields.
Oh and for the group by statement as well as the SELECT part, indeed use the previously mentioned edit mode for the query. There you can add all fields (by selecting them in the table and dragging them down) that are needed in the result, then click the 'totals' button in the ribbon to add the group by- statements for all. Then you only have to add the Where-clause and you are done.
Now that the question is more clear (you want the query to select fields instead of records based on the particular requirements), I'll have to change my answer to:
This is not possible.
(untill proven otherwise) ;)
As far as I know, a query is used to select records using for example the where clause, never used to determine which fields should be shown depending on a certain criterium.
One thing that MIGHT help in this case is to look at the database design. Are those tables correctly made?
Suppose you have 190 of those fields that are merely details of the main data. You could separate this in another table, so you have a main table and details table.
The details table could look something like:
ID ID_Main Det_desc Det_value
This way you can filter all Detail values that are equal between the two main values A and B using something like:
Select a.det_desc, a.det_value, b.det_value
(Select Det_desc, det_value
from tblDetails
where id_main = a) as A inner join
(Select Det_desc, det_value
from tblDetails
where id_main = a) as B
on A.det_desc = B.det_desc and A.det_value <> B.det_value
This you can join with your main table again if needed.
You can full join the table on itself, matching identical rows. Then you can filter on mismatches if one of the two join parts is null. For example:
select *
from (
select *
from Table1
where Field1 = 'A'
) A
full join
(
select *
from Table1
where Field1 = 'B'
) B
on A.Field2 = B.Field2
and A.Field3 = B.Field3
where A.Field1 is null
or B.Field1 is null
If you have 200 fields, ask Access to generate the column list by creating a query in design view. Switch to SQL view and copy/paste. An editor with column mode (like UltraEdit) will help create the query.