I made a select using the WITH query something like this:
WITH test AS (select id, sum(value) as value from test group by id)
SELECT name, t.value from names JOIN test t ON (t.id = names.id)
But I need to use 2 WITH queries, is there any way to make it?
WITH
name1 AS (SELECT ...),
name2 AS (SELECT ...)
SELECT ...
Related
SELECT *
FROM car_t
JOIN ( SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male');
I get an error for not using Alias. Can someone help me with that ? Thank you!
I need to do a join statement with a subquery.
It is a common requirement in SQL that every derived table must have an alias (although some databases are lax about it).
Your subquery produces a derived table, that you hence need to alias:
SELECT *
FROM car_t c
JOIN (
SELECT driver_id, gender, first_name,last_name FROM driver_t WHERE gender = 'Male'
) d ON ???
--^ table alias
Notes:
your query is clearly missing a JOIN condition to relate the two tables; it should appear after the ON keyword, that I added to the query
the subquery is actually not necessary here; you could just join the table, then select the needed columns
SELECT
c.*,
d.driver_id,
d.gender,
d.first_name,
d.last_name
FROM car_t c
JOIN driver_t d ON ???
WHERE d.gender = 'Male'
Well the error messages refers to your "derived table" (alias "subquery") which requires a name
SELECT *
FROM car_t
JOIN (
SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male'
) as x --<< the alias
But that will just lead you to the next error because you have no join condition.
So you need something like:
SELECT *
FROM car_t
JOIN (
SELECT driver_id, gender, first_name,last_name
FROM driver_t
WHERE gender = 'Male'
) as x on x.??? = car_t.???
You need to replace the ??? with the columns that link the two tables together.
But you don't really need the derived table anyway. You can simplify this to
SELECT
FROM car_t
JOIN driver_t as x on x.??? = car_t.???
WHERE x.gender = 'Male';
Can someone help me with this TSQL syntax? I am trying to do
Select Count(Distinct Value) AS OpenPO
FROM OpenPOs
AND
Select Count(Distinct Value) As OpenSho
From OpenSho
etc.
How do I make this into one neat query with results like its all part of a single table or view?
Try:
SELECT (Select Count(Distinct Value) FROM OpenPOs) AS OpenPO,
(Select Count(Distinct Value) From OpenSho) AS OpenSho
You will get as result something like:
OpenPO OpenSho
---------------
3 5
Is there a query that would push previous ids into an array, that would result in:
id c_array
-----------------
1 {}
2 {1}
3 {1,2}
4 {1,2,3}
I think you want to use the array_agg aggregate as a window function.
SELECT
id,
array_agg(id) OVER (ORDER BY id) AS c_array
FROM the_table
Try Like This
select id, array(select l.id from table1 l where l.id< i.id order by id) as
c_Arr from table1 i
I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There are some duplicate ProductNumbers. How can I count the distinct number of products, then print out the outcome like "() products was backup." ? Because this is inside a stored procedure, I have to use a variable #numrecord as the distinct number of rows. I put my codes like this:
set #numrecord= select distinct ProductNumber
from ProductNumberDuplicates_backups where COUNT(*) > 1
group by ProductID
having Count(ProductNumber)>1
Print cast(#numrecord as varchar)+' product(s) were backed up.'
obviously the error was after the = sign as the select can not follow it. I've search for similar cases but they are just select statements. Please help. Many thanks!
Try
select #numrecord= count(distinct ProductNumber)
from ProductNumberDuplicates_backups
Print cast(#numrecord as varchar)+' product(s) were backed up.'
begin tran
create table ProductNumberDuplicates_backups (
ProductNumber int
)
insert ProductNumberDuplicates_backups(ProductNumber)
select 1
union all
select 2
union all
select 1
union all
select 3
union all
select 2
select * from ProductNumberDuplicates_backups
declare #numRecord int
select #numRecord = count(ProductNumber) from
(select ProductNumber, ROW_NUMBER()
over (partition by ProductNumber order by ProductNumber) RowNumber
from ProductNumberDuplicates_backups) p
where p.RowNumber > 1
print cast(#numRecord as varchar) + ' product(s) were backed up.'
rollback
I am trying to run the following SQL
DELETE FROM T_ATH_POSHLD WHERE T_ATH_POSHLD.A_INSID IN (SELECT T_ATH_POSHLD.A_INSID FROM T_ATH_POSHLD LEFT JOIN T_ATH_INS ON T_ATH_POSHLD.A_INSID = T_ATH_INS.A_INSID WHERE T_ATH_INS.A_INSCLSCDE1 = 'CASH' AND T_ATH_POSHLD.A_INSID NOT IN (SELECT A_INSID FROM T_ATH_CCY) AND A_ACCID IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS'))
and in particular, am trying to check whether an ACCID is in a set of values, some coming from a table and two hardcoded. How would I achieve this?
IN (SELECT A_ACCID FROM T_ATH_EXTACC, '1212OEIC', '5667033ZS')
Doesn't work, I get an 'Incorrect Syntax error'.
Thanks
You need to use UNION to add the 2 hardcoded values to the resultset that you are passing to the in clause.
IN (SELECT A_ACCID FROM T_ATH_EXTACC UNION ALL SELECT '1212OEIC' UNION ALL SELECT '5667033ZS')
IN (SELECT '1212OEIC', '5667033ZS', A_ACCID FROM T_ATH_EXTACC )