logo
down
shadow

SQL - using an alias in a where clause in a subquery


SQL - using an alias in a where clause in a subquery

Content Index :

SQL - using an alias in a where clause in a subquery
Tag : sql , By : yossi
Date : November 24 2020, 05:44 AM


Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

MySQL - Using an alias in a subquery with WHERE clause


Tag : mysql , By : Amit Battan
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I have a feeling I am completely borking this MySQL query but I'll ask anyway. I am wondering why I get the warning Unknown column 'FOO', and how I can get this query to work properly, in 'where clause' when I run the following:
SELECT sample_id 
FROM   tbl_test outter 
WHERE  sample_id = 521 
       AND sample_id IN (SELECT sample_id 
                         FROM   tbl_test 
                         WHERE  sample_id = outter.sample_id 
                         GROUP  BY sample_id) 

Use an Alias in Where Clause Subquery in Oracle


Tag : development , By : Sandip
Date : March 29 2020, 07:55 AM
To fix this issue You can use an identifier defined in an external query in only one level deep queries. You have to rethink your strategy. My suggestion is to remove the subquery from the select list and put it in the FROM clause. And add another rownumber column like this:
(SELECT 
    ID_PEGAWAI,
    CONCAT(TO_CHAR(abs(sysdate - TO_DATE(TMT_JABATAN))/360,'9,999,999.9'),' TAHUN') MASA_KERJA, 
    ROW_NUMBER() OVER (PARTITION BY ID_PEGAWAI ORDER BY TMT_JABATAN DESC) rownumber
FROM SIMPEG_JABATAN) xxx
ON ANGGARAN.SIMPEG_PEGAWAI = xxx.ID_PEGAWAI
WHERE
     ....
     AND xxx.rownumber = 1
SELECT
  ANGGARAN.SIMPEG_PEGAWAI.ID_PEGAWAI AS KODE,
  ANGGARAN.SIMPEG_PEGAWAI.NAMA,
  ANGGARAN.SIMPEG_PEGAWAI.NIP,
  ANGGARAN.SIMPEG_ESELON_JABATAN.JABATAN,
  ANGGARAN.SIMPEG_KODE_GOLONGAN_PANGKAT.GOLONGAN,
  ANGGARAN.SIMPEG_KODE_GOLONGAN_PANGKAT.PANGKAT
FROM 
  ANGGARAN.SIMPEG_PEGAWAI
    INNER JOIN ANGGARAN.SIMPEG_ESELON_JABATAN 
      ON ANGGARAN.SIMPEG_PEGAWAI.ESELON_JABATAN = ANGGARAN.SIMPEG_ESELON_JABATAN.ID_ESELON_JABATAN
    INNER JOIN ANGGARAN.SIMPEG_KODE_GOLONGAN_PANGKAT 
      ON ANGGARAN.SIMPEG_PEGAWAI.PANGKAT = ANGGARAN.SIMPEG_KODE_GOLONGAN_PANGKAT.ID_GOLONGAN_PANGKAT
    INNER JOIN (
      SELECT 
        ID_PEGAWAI,
        CONCAT(TO_CHAR(abs(sysdate - TO_DATE(TMT_JABATAN))/360,'9,999,999.9'),' TAHUN') MASA_KERJA, 
        ROW_NUMBER() OVER (PARTITION BY ID_PEGAWAI ORDER BY TMT_JABATAN DESC) rownumber
      FROM SIMPEG_JABATAN
    ) xxx
      ON ANGGARAN.SIMPEG_PEGAWAI.ID_PEGAWAI = xxx.ID_PEGAWAI
WHERE
  ANGGARAN.SIMPEG_PEGAWAI.ST_AKTIF = 1 AND
  ANGGARAN.SIMPEG_PEGAWAI.ESELON2 <> 1 AND
  ANGGARAN.SIMPEG_PEGAWAI.PANGKAT >= 12 AND 
  ANGGARAN.SIMPEG_ESELON_JABATAN.STATUS = 1 AND
  ANGGARAN.SIMPEG_ESELON_JABATAN.ID_ESELON2=2 AND
  xxx.rownumber = 1
ORDER BY ANGGARAN.SIMPEG_KODE_GOLONGAN_PANGKAT.SORT DESC

ORACLE ALIAS in WHERE Clause Subquery


Tag : sql , By : user98986
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You can only use colums that are in your FROM clause. So, your query should be something like:
SELECT TSR.Merchant_id, SD.denomination, SUM (SD.no_of_cards)
  FROM sales_details SD
  JOIN sales_header TSR on SD.invoice_od = TSR.invoice_id
 WHERE entered_by = (
                  (SELECT account_code
                     FROM bk_dsr_account_codes
                    WHERE user_name = 'C'
                      AND PASSWORD = 'D'
                      AND mobile_no = '8994035090213391259'))
           AND entered_date BETWEEN (TO_DATE ('2013/04/01', 'yyyy/mm/dd')
                                    )
                                AND (TO_DATE ('2013/06/30', 'yyyy/mm/dd')
                                    )
GROUP BY SD.denomination

Using alias in subquery's WHERE clause in PostgreSQL


Tag : postgresql , By : afds
Date : March 29 2020, 07:55 AM
help you fix your problem In postgresql, I have a users table with name and meta_data columns. Meta_data column is jsonb, I store data like: , Then use the same logic in the where like this:
SELECT jsonb_array_elements(users.meta_data->'likes')->>'id' AS like_id,
(SELECT count(*) FROM users WHERE users.meta_data->'likes'->>'id' = jsonb_array_elements(users.meta_data->'likes')->>'id' ) AS count_users
FROM users
SELECT jsonb_array_elements(users.meta_data->'likes')->>'id' AS like_id,
count(jsonb_array_elements(users.meta_data->'likes')->>'id') AS count_users
FROM users
GROUP BY jsonb_array_elements(users.meta_data->'likes')->>'id'
SELECT t.like_id,
       (SELECT count(*) FROM users WHERE users.meta_data->'likes'->>'id' = t.like_id ) AS count_users FROM (
SELECT jsonb_array_elements(users.meta_data->'likes')->>'id' AS like_id,
FROM users) t

Using Subquery in having clause using alias


Tag : mysql , By : toma
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • Way to alter one table if another was altered
  • Proper use of semicolons in functions and procedures
  • Cursor Not looping all the Records
  • Select inner join returns wrong result
  • Group By showing duplicates
  • Error in using subquery in access join query
  • How do I use a composite pk in a stored procedure?
  • Oracle Unique Indexes
  • Spooling data to CSV truncates
  • How do views reduce code duplication?
  • SQL: Gather right hand values from a join
  • SQL Agent Logon - What is going on?
  • How do I combine two rows of same part, but add quantities?
  • How to optimize this SQL query for a rectangular region?
  • Pass data from ssrs subreport to parent
  • how to enter values in rowguid column?
  • Read multiple tables from dataset in Powershell
  • In SQL how to I exclude a record if there are more than 3 characters after a dash
  • What is the difference between check and foreign key?
  • How to return a record from function, executed by INSERT/UPDATE rule (trigger)?
  • SQL order by oldest from Unix Timestamps
  • How to convert decode function in oracle to standard big query
  • Postgresql: An alternative to subqueries to make the query more efficient?
  • Use returned values from a query in the same procedure
  • Sum all numeric columns in database and log results
  • Release item in current year
  • Conditional function not working as intended postgresql
  • SQL trigger, results not Grouped by
  • getting more records than expected with simple query in postgresql
  • get first occurrence of a certain combination of columns
  • Converting string timestamp to local-time zone in Oracle
  • Redshift - Filter out rows that are blank to left or right of a character
  • Teradata - Know the load volume by times of a table
  • How do I replace duplicate data with random data of same length in SQL
  • How to group by using pivot table
  • How can I achieve a Join for merging two tables FULL OUTER JOIN and avoid duplicates?
  • Multi Keyword search on multi-column sql
  • Computing the median of salaries under each manager in BigQuery SQL
  • How can insert value by selecting from another
  • Retrieving only the latest item for a set of data
  • update column with concatenating for that group only
  • SQL `SELECT 'some string value' AS fieldname` equivalent in Powershell?
  • How to determine the AVERAGE number of associated records
  • In Django, how can I build a set of criteria from a list?
  • Using replace() inside a loop
  • select user on create view
  • Calculate and aggregate column depending on a value in another colum (SQL)
  • Trigger to track all the insert or update on Oracle Table
  • Update column - remove first and last characters
  • T-SQL - Adding FK to table, make it do drop/null/efault automagically?
  • Get constraints and indexes on materialized views
  • SQL perform AVG after MAX
  • How do you join two tables A and B in order to get a non join column in A with data not in B?
  • Move Non-NULL Values Up Within Partition
  • Selecting rows based on two many-to-many relations
  • I have a table on which i want to do some advance calculation and get the table in a new shape
  • How to convert decimal to time in Oracle SQL?
  • Combine numbers to 1 row
  • Error: column does not exist when in fact it does? why?
  • How to fix: "Operation must use an updateable query" on update statement in Access VBA
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com