Select every columns, but rename one of them - sql

Let's assume I have a table named Alphabet. Now let's say this table has the following columns:
a, b, c, d, e ..., z
Now I want to select everything from the mentioned table (but select f as aaa), but I don't want to do this:
select a, b, c, d, e, f as aaa, g ..., z
from Alphabet;
Clearly the above is cumbersome if the are a lot of columns. So I am thinking if it is possible to do something like
select f as aaa, *
from Alphabet;
This is probably something there is a lot info about on the net, but I'm not sure what to search for. Has this kind of select got an expression or name?
Thanks in advance

You can write this:
select a.f as aaa, a.*
from Alphabet a;
Note that the output set will contain both f and aaa. It is unclear whether this is what you want.
To remove f from the result set, you need to list all the columns that you do want.

Related

Checking which of two column values is closest to a calculated value

Absolute rookie at SQL so apologies upfront if not possible or absurd.
Single table in SQL-Lite
First of all I want to filter the table to only return rows where the difference between decimal in column A and decimal in column B is more than 3
Then for each row I want to subtract integer in column C from integer in column D to give result E. And then I want to know whether the decimal in column A or decimal in column B is closer to result E
Thanks!
The code below basically uses a subquery to keep all the needed values handy, a CASE operator to make the decision, and the ABS() function to determine absolute distance.
select A, B, C, D, E,
case when ABS(A-E) < abs(B-E) then 'A' else 'B' end [Closer_Value]
from (
select A, B, C, D, (C-D) as [E]
from YourTable
where abs(A-B) > 3
) as Temp

Find rows that contain all words in any order

My application is built in vb.net with SQL Server Compact as the database so I'm unable to use a full-text index.
Here's my data...
MainTable field1
A B C
B G C
X Y Z
C P B
Search term = B C
Expected Results = any combination of the search term = Rows 1, 2, 4
Here's what I'm currently doing...
I'm permuting the search term B C into an array containing %B%C% and %C%B% and inserting those values into field1 of tempTable.
So my SQL looks like this:
SELECT * FROM MainTable INNER JOIN tempTable ON MainTable.field1 LIKE tempTable.field1
In this simple example, it does return the expected results correctly. However, my search term can contain more values. For example 6 search terms B C D E F G when permuted has 720 different values and as more search terms are used, the permutations grow exponentially...which is not good.
Is there a better way to do this?
The following will work for your example above:
Select * from table where field1 like '%[BC]%'
But it will also return strings that contain ONLY "B" or "C". Do you need both characters in any order or one or more?
EDIT: Then the following would work:
Select * from test_data where col1 LIKE '%Apple%' and col1 like '%Dog%'
See the demo here: http://rextester.com/edit/LNDQ49764

SQL count summation query across multiple sets of Access tables

I have a MS Access query (created with SQL) that counts the occurrences of non-NULL values in a date column in an equipment status table. The table contains all of the equipment for a particular manufacturing plant. The query also counts the total equipment, with our without NULL values. There are a total of 12 different plants, each with its own identical set of Access tables. I need to create a consolidated SQL query that creates a summation of each of the counts into a master count for all of the plants.
The structure of the status table, named '_review_status' is:
equip_number, text
review_a_analysis, date
review_b_analysis, date
review_c_analysis, date
review_d_analysis, date
review_e_analysis, date
review_f_analysis, date
review_g_analysis, date
The results of the working query (on one table) look like this.
a b c d e f g equip_count
17 31 0 94 13 12 44 1249
The new results should look exactly like the above, except that the number will all be larger because the query is looking at all 12 sets of tables.
Here is the working one-table query:
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment
LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number =
dept1_review_status.dept1_equip_number;
The join on the dept1_equipment table is used to get the full count of all of the equipment in each department.
Many thanks.
Bob
Consider running a union query of all 12 group by aggregate queries and then run another final aggregate query using the saved union query:
UNION
SELECT
Count(dept1_review_status.review_a_analysis) AS a,
Count(dept1_review_status.review_b_analysis) AS b,
Count(dept1_review_status.review_c_analysis) AS c,
Count(dept1_review_status.review_d_analysis) AS d,
Count(dept1_review_status.review_e_analysis) AS e,
Count(dept1_review_status.review_f_analysis) AS f,
Count(dept1_review_status.review_g_analysis) AS g,
Count(dept1_equipment.dept1_equip_number) AS equip_count
FROM dept1_equipment LEFT JOIN dept1_review_status
ON dept1_equipment.dept1_equip_number = dept1_review_status.dept1_equip_number;
UNION ALL
SELECT
Count(dept2_review_status.review_a_analysis) AS a,
Count(dept2_review_status.review_b_analysis) AS b,
Count(dept2_review_status.review_c_analysis) AS c,
Count(dept2_review_status.review_d_analysis) AS d,
Count(dept2_review_status.review_e_analysis) AS e,
Count(dept2_review_status.review_f_analysis) AS f,
Count(dept2_review_status.review_g_analysis) AS g,
Count(dept2_equipment.dept2_equip_number) AS equip_count
FROM dept2_equipment LEFT JOIN dept2_review_status
ON dept2_equipment.dept2_equip_number = dept2_review_status.dept2_equip_number;
UNION ALL
SELECT
Count(dept3_review_status.review_a_analysis) AS a,
Count(dept3_review_status.review_b_analysis) AS b,
Count(dept3_review_status.review_c_analysis) AS c,
Count(dept3_review_status.review_d_analysis) AS d,
Count(dept3_review_status.review_e_analysis) AS e,
Count(dept3_review_status.review_f_analysis) AS f,
Count(dept3_review_status.review_g_analysis) AS g,
Count(dept3_equipment.dept3_equip_number) AS equip_count
FROM dept3_equipment LEFT JOIN dept3_review_status
ON dept3_equipment.dept3_equip_number = dept3_review_status.dept3_equip_number;
...other 9 tables...
FINAL
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM unionqry
Alternatively, you can incorporate all in one query if MS Access allows depending on complexity:
SELECT Sum(unionqry.a),
Sum(unionqry.b),
Sum(unionqry.c),
Sum(unionqry.d),
Sum(unionqry.e),
Sum(unionqry.f),
Sum(unionqry.g),
Sum(unionqry.h),
Sum(unionqry.equip_count)
FROM (...above union query)... As unionqry

Produce new data dynamically based on existing data

Having a hard time trying to explain what I'm trying to achieve just using words...
I'm working with two tables here. One has a list of customers with their unqiue reference. The second a table (datatable) which holds some generic data which is linked to the customer by their reference.
What I'd like to is copy each existing row in the datatable and replace the customer reference with every customer.
Example.
CustomerTable
ID, Name, Address
001, ABC, ABC
002, DEF, DEF
003, GHI, GHI
DataTable (before)
ID, Detail1, Detail2, Detail3
001, A, B, C
001, D, E, F
001, G, H, I
DataTable (after)
ID, Detail1, Detail2, Detail3
001, A, B, C
001, D, E, F
001, G, H, I
002, A, B, C
002, D, E, F
002, G, H, I
003, A, B, C
003, D, E, F
003, G, H, I
This will update thousands of rows so I don't know what the most efficient way to do this. Looking at it like a programmer I want to use arrays and loops but this probably isn't the best approach in T-SQL.
In SQL you are actually just looking for a cross joined set of results which can then created into a table.
Based on your example:
SELECT CustomerTable.ID, DataTable.Detail1, DataTable.Detail2, DataTable.Detail3
INTO New_DataTable
FROM CustomerTable
CROSS JOIN DataTable
This will create a new table (New_DataTable) with the correct data. You could then just rename away the old table and rename the new one to the original name. The sp_rename stored procedure can be used to do this.
sp_rename DataTable, Original_DataTable
sp_rename New_DataTable, DataTable
You must guarantee that no one will be updating the existing DataTable to do the switch out.

Oracle - Use value in first cell to determine value of second cell

I have an interesting requirement - I need to use the value of the first cell in a row to determine the value of the fourth cell in a row. Normally this would be handled at the application level or within a function, but I'm stuck doing it in a normal select query.
Here are the details.
1) I have a simple query (select A, B, C from D) returns the following correctly
1 | 2 | 3
2) I have a function that leverages the values returned in the first query and returns a value
select function_x('1') from dual
accurately returns 'Z'
I want to concatenate all of them so I get the following:
1 | 2 | 3 | Z
I tried something like this query but it doesnt work:
select A, B, C, (select function_x(A) from dual)
from D
It works when I hard code a value into the function, but doesn't work when I try to leverage the first returned value.
Are there any solutions available without me creating a function?
select A, B, C, function_x(A) from D
I figured it out, I had to use a subquery:
select A, B, C, function_x(A) from (select A, B, C from D)