Recent Entries 10
- snippet minor 112d agoHow to create alias in SQL Server 2022As per https://learn.microsoft.com/en-us/sql/relational-databases/native-client/sql-server-native-client?view=sql-server-ver16: The SQL Server Native Client (often abbreviated SNAC) has been removed from SQL Server 2022 (16.x) and SQL Server Management Studio 19 (SSMS). The SQL Server Native Client (SQLNCLI or SQLNCLI11) and the legacy Microsoft OLE DB Provider for SQL Server (SQLOLEDB) are not recommended for new development. Switch to the new Microsoft OLE DB Driver (MSOLEDBSQL) for SQL Server or the latest Microsoft ODBC Driver for SQL Server going forward. I used to create aliases using the SQL Server Configuration Manager, but since SQL Server 2022, the form to create alias has all the fields disabled: How do I create alias in SQL Server 2022, is that even possible?
- pattern minor 112d agoMySQL reuse select aliasesI currently have a query where I'm doing two subqueries to get X, Y data: ``` SELECT t.series AS week, ( ... ) X, ( ..., AND ... ) Y, ROUND(( ... ) * 100) / ( ..., AND ... ), 2) Z FROM series_tmp t ``` Y is kind of subset of X, since I apply just an additional condition to the existing ones, if X is: ``` SELECT COUNT(*) FROM t1 INNER JOIN t2 ON t2.id = t1.another_id WHERE t2.something = 1 AND t1.date BETWEEN t.series AND t.series + INTERVAL 6 DAY ``` Then Y has an additional AND condition: ``` SELECT COUNT(*) FROM t1 INNER JOIN t2 ON t2.id = t1.another_id WHERE t2.something = 1 AND t1.date BETWEEN t.series AND t.series + INTERVAL 6 DAY AND t1.some_state = 'x state' ``` And for the value of X I need to take those two results - X and Y and do some calculation. Since I can't use the aliases, I have to use a subquery, right? But in that case it seems too much. Is there a way to reuse those subqueries? It seems to be too much of the same. I'm using MySQL 5.6 so I'm not able to use CTEs :( PS: `series_tmp` comes from this wonderful idea (thanks to them).
- snippet minor 112d agoHow to select all columns plus one alias column in MySQLIt is a simple question, but i could not find the answer. I want to select all the columns in my table with a `SELECT *` statement, but i also need to alias a UUID column this way: `BIN_TO_UUID(ID) as ID`. I need something like this: ``` SELECT BIN_TO_UUID(ID) AS ID, * FROM TABLE_NAME ``` Also would be nice to remove the original column that was aliased, to do not have the same column twice with different names, but just if this do not add more text to the query, because i will have to repeat this for all my 16 tables.
- debug critical 112d agoUsing column alias in a WHERE clause doesn't workGiven a table `users` with two fields: `id` and `email`. ``` select id, email as electronic_mail from ( select id, email from users ) t where electronic_mail = '' ``` Postgres complains that: ``` ERROR: column "electronic_mail" does not exist ``` The example is just to demonstrate the arising problem. My actual case is more complex, I iterate though an array of elements in a json column, taking a single scalar value from each. (I can share some code if that helps.) I really don't get what would be the complication, probably I'm unaware of something. I was under the impression that aliased columns can be employed in a `WHERE` clause without problem?
- snippet minor 112d agoHow to use column alias to calculate other column valueMy query is ``` SELECT (`house_rent`+`conveyance`+`medical`+`dearness`+`others_allowances`) AS earnings ,(`income_tax`+`pro_tax`+`emp_state_insu`+`absence_fine`+`others_deductions`) AS deductions ,(earnings - deductions) AS net_salary FROM salary ``` and im getting an error that unknown column earnings and deductions because these are column alias not column name .. Any solution? Thanks in advance
- pattern minor 112d agoGetting SQL server to recognise a date columnI am trying to get a count of IDS on a orders table for yesterday (not hardcode) where the date in the table corresponds to when the order was placed. My table looks like this ``` orders ( order_id INT PRIMARY KEY , user_id INT , date_created DATE , order_value FLOAT , city_id INT ) ``` I have used this code to get todays date in the table - ``` Select *,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) as Today From orders ``` This works fine but when I try attempt a where clause below this ``` Select *,DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0) as Today From orders where Today -1 = date_created; ``` I receive this error "Invalid column name 'Today'" So my question is simply how can I get this table to recognise the the date I added as a new column and allow me to perform work on it? Thanks in advance
- pattern minor 112d agoSame table alias for two different tables (Oracle)This example has the same table alias used for two different tables. I don't understand why this is allowed by Oracle, and if allowed, how the results make any sense. ``` create table Table_A (x number); create table Table_B (x number); insert into Table_A values (1); insert into Table_A values (2); insert into Table_B values (2); insert into Table_B values (3); select * from Table_A ; X ---------- 1 2 2 rows selected. select * from Table_B ; X ---------- 2 3 2 rows selected. select * from Table_A T join Table_B T on T.x = T.x; X X ---------- ---------- 2 2 2 2 3 3 3 3 4 rows selected. ```
- pattern minor 112d agoTwo joins on same table in SQLI've got 2 SQL tables: `NAMES` and `RELATIONSHIPS` Currently `NAMES` just has 2 columns: `name` and `name_id`. The other is a list of relationships between people in the `NAMES` table. It has 3 columns: `primaryperson_id`, `relatedperson_id`, and `relationship_id`. The `primaryperson_id` and `related_person_id` are `name_id`s from the `NAMES` table. Each person in `NAMES` can have multiple entries in either the primary or related columns of `RELATIONSHIPS` (is that a many-to-many relationship?). This query works: ``` SELECT people.name AS 'primary', relationships.related_person_id AS relatedto FROM relationships JOIN people ON people.name_id=relationships.primary_person_id ORDER BY people.name_id; ``` But I'd like to show a name (i.e. text) in the `relatedto` column rather than the id number. How can I do that?
- pattern moderate 112d agoDisplay column title as in the query without 'AS' statementI query data from multiple tables using JOIN statements like this : ``` SELECT u.id,u.name,d.id,d.name FROM user u RIGHT JOIN dog d ON... ; ``` When I get the answer, in psql for example, I've got this kind of rows title : ``` | id | name | id | name | +-------+-------+------+--------+ ``` Instead, I'd like to get the original and precise notation : ``` | u.id | u.name | d.id | d.name | +---------+---------+--------+----------+ ``` For now I have to use `AS` statements, but it's quite repetitive to re-write and it add length to the query and makes it less readable. Is there a way to get this result with a SQL keyword or with a DBMS parameter? I searched but wasn't able to find anything on this specific problem.
- debug minor 112d agoINSERT INTO with UNION Alias ERRORThis question is derived from the following post UNION query not working Fails: ``` INSERT INTO AssetControl SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp; ``` Works: ``` INSERT INTO AssetControl SELECT U.* FROM (SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp) AS U ``` I don't understand why an alias `U` needs to be created in order for the records to be successfully input into the `AssetControl` table. Can anyone shed some light on this?