Apply filter to casted column - sql

I would like to apply a filter to a column I've casted as DATE, as the example below:
select A, B, CAST(C as DATE) as D, E from MY_TABLE where D>'2018-12-12'
Which doesn't work. I get the error: "Invalid column name 'D'."
I've also tried:
select A, B, CAST(C as DATE) as D, E from MY_TABLE where C>'2018-12-12'
Getting the same error. Any idea how I could do that?

First, if C is already a date/time type, you can simply do:
select A, B, CAST(C as DATE) as D, E
from MY_TABLE
where C > DATEADD(DAY, 1, '2018-12-12')
If not, you can use a lateral join, which in SQL Server uses the APPLY keyword:
select A, B, v.D, E
from MY_TABLE t cross apply
(values (CAST(C as DATE))) v(D)
where v.D > '2018-12-12';
If C is a string, I strongly recommend TRY_CAST() or fixing the data in-place.

Related

Avoid duplicate columns select on nested select

In CrateDB is there a way to avoid to re-select the same column in nested SELECT statement, to show the value in the results?
e.i. in the following query, is there any way to avoid re-selecting A and B through the nested SELECT? Ideally would be nice to select just once in the first
SELECT
A,
B,
AB,
A * B * AB AS ABAB,
A / AB::DECIMAL AS AAB,
FROM (
SELECT
A,
B,
(A + B) AS AB,
FROM (
SELECT
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_1')) AS A,
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_2')) AS B,
) alias_for_subquery_01
) alias_for_subquery_02
Thanks

select from multiple tables with the same column

I want to select the same columns from different tables that look the same
(daily tables).
I saw this SELECT from multiple tables with the same structure answer but if I'm going according to this I'm ending up with a huge query.
this code is similar to what I have, according to answer above I need to do the following:
select a, b, c
from (
select a, b, c, d, e from hourly.16
union all
select a, b, c, d, e from hourly.15
)
isn't there an option to do something like:
select a, b, c
from (
select a, b, c, d, e from (hourly.16 union all hourly.15)
)
so I won't end up with huge queries?
#standardSQL
SELECT a, b, c
FROM (
SELECT a, b, c, d, e
FROM `project.hourly.*`
WHERE _TABLE_SUFFIX BETWEEN '15' AND '16'
)
Above is assuming that hourly is your dataset

Combine two tables in SQL server 2008

I need to ask something is there any way combine two tables different count of columns like:
Select a,b,c, from x
union
Select d, e from y
you need to do something like this
Select a,b,c from x
union all -- ALL doesn't filter dups and is faster
Select d, e, '' as f from y
I used '' but you might want to use NULL or 0, NULL will be compatible will all data types, '' will not be
I also used UNION ALL not UNION since it will perform better because it doesn't have to do a sort operation to get rid of dups
the alias f is not needed here either because the top query determines the name of the columns in the resultset
Note that
select a, b, c from x
union
select d, e, '' as f from y;
and
select d, e, '' as f from y
union
select a, b, c from x;
will yield different results i.e. the attribute names from the first-appearing table will be used.
Perhaps better to be unequivocal by explicitly renaming the columns in the second table e.g.
select a, b, c from x
union
select d AS a, e AS b, '' as c from y;
select col1, col2, col3, col4
from mytable1
union all
select col5, col6, null as col7, '' as col8
from mytable2
first table
id
name
second table
name
seatno
if you want to join on name & there are some duplicate names in both table the use ROW_NUMBER in join query!

Select Sum and multiple columns in 1 select statement

Is there a way to select the sum of a column and other columns at the same time in SQL?
Example:
SELECT sum(a) as car,b,c FROM toys
How about:
select sum(a) over(), b, c from toy;
or, if it's required:
select sum(a) over(partition by b), b, c from toy;
try add GROUP BY
SELECT sum(a) as car,b,c FROM toys
GROUP BY b, c
Since you do not give much context, I assume you mean one of the following :
SELECT (SELECT SUM(a) FROM Toys) as 'car', b, c FROM Toys;
or
SELECT SUM(a) as Car, b, c FROM Toys GROUP BY b, c;
SELECT b,
c,
(SELECT sum(a) FROM toys) as 'car'
FROM toys

SQL insert error

Below code saying error
incorreect syntax near "Main"
INSERT INTO tbl (
'Week',
Main,
a,
b,
c,
d,
e
)
Select 'Week',
Main,
a,
b,
c,
d,
e
FROM tbl_link
INSERT INTO tbl (
'Week',
You should insert actual field name here.
If you field is called Week, just get rid of the quotes:
INSERT INTO tbl (
Week,
Main,
a,
b,
c,
d,
e
)
Select 'Week',
Main,
a,
b,
c,
d,
e
FROM tbl_link
, otherwise substitute the field name.
To clarify the other answers:
SQL INSERT syntax with SELECT statements is as followings:
INSERT INTO Table(Column1Name, Column2Name, ...)
SELECT
Column1Data, Column2Data, ...
FROM Table
The parenthesized list after the table name you intend to update is the ordered list of columns on that table that you intend to populate. It does not represent data that will make its way to the table.
INSERT INTO tbl (
[Week],
Main,
a,
b,
c,
d,
e
)
Select [Week],
Main,
a,
b,
c,
d,
e
FROM tbl_link
there are two possible issues here:
Your column "week" is a reserved word, so use square braces: [week] when you refer to it in any query.
Based on your INSERT attempt, your tables tbl and tbl_link must should both have columns named: [week],Main,a,b,c,d, and e. If not you must rethink what you are trying to do. The first list of columns have to all exist within table tbl, and the second list of columns must all be within table tbl_link. If there are column in tbl that are not in tbl_link, you can just eliminate them from the INSERT:
INSERT INTO tbl (
[Week],
a,
b,
c
)
Select [Week],
a,
b,
c
FROM tbl_link