I've searched a lot, but still no chance on having a subquery to return multiple columns all at once. The following code works, but it sucks:
SELECT
(SELECT Column1 FROM dbo.fnGetItemPath(ib.Id)) AS Col1,
(SELECT Column2 FROM dbo.fnGetItemPath(ib.Id)) AS Col2,
(SELECT Column3 FROM dbo.fnGetItemPath(ib.Id)) AS Col3
FROM ItemBase ib
I actually have got no idea how to pass ib.Id to the function and get the entire Column1, Column2, Column3 columns without calling the fnGetItemPath function 3 times.
Thanks in advance
You can move ti to "FROM" part and use outer apply (or cross apply).
check syntax yourself, but it should look something like this:
SELECT Column1, Column2, Column3
FROM ItemBase ib
Outer Apply dbo.fnGetItemPath(ib.Id)
doesn't this work?
select
(select column1, column2, column3 from dbo.fnGetItemPath(ib.Id))
from ItemBase ib
or do you need something else?
Related
Table contains multiple columns and multiple redundant rows which I don't need to work on. Let's say I selected columns and rows which I need to work on.
select column1,
column2,
column3
from table
where column1 > something
and column2 == something;
Now how do I perform nested query on that selected data? I was thinking of doing something like.
select column1,
sum(column2) from (
select column1,
column2,
column3
from table
where column1>something
and column2 == something)
group by column1;
And I am getting error. Any help would be appreciated
I don't know if you really need the subquery or the column3 that you don't use in any condition , if you provide template datas and expected result would be better.
For your query to work you need an alias in the subquery, so it would be something like:
select t1.column1,
sum(t1.column2) from (
select column1,
column2,
column3
from table
where column1>something
and column2 == something) as t1
group by t1.column1;
I am trying to query 2 tables, return the values while combining duplicates. While UNION is supposed to remove duplicates, it seems to fail in this case. Note that this query is sent with PHP.
QUERY
<?php
$keywords = 'MOTOR';
$toSend = "SELECT part as Column1, '' as Column2 FROM soldParts WHERE part LIKE '%".$keywords."%' UNION SELECT part as Column1, vin as Column2 FROM vinData WHERE part LIKE '%".$keywords."%' ORDER BY column1 ASC";
?>
CURRENT OUTPUT
Column1 Column2
motor 1
motor 2
motor 2
CCDD44
motor 3
AABB1122
motor 3
DESIRED OUTPUT
Column1 Column2
motor 1
motor 2
CCDD44
motor 3
AABB1122
I have looked up several similar questions. However, my second column should just be merged (or concatenated), unlike other questions and/or they do not involve multiple tables. Also note that table soldParts Column2 has no value.
Your resultset actually has no duplicates. Duplicates are rows where all columns have equal values, and no row in your resultset complies to that definition.
Presumably, you want aggregation in the outer query:
select column1, max(column2) column2
from (
select part as column1, null as column2 from soldparts where part like ?
union all
select part as column1, vin as column2 from vindata where part like ?
)
group by column1
order by column1 asc
Note that I modified your query to use bind parameters (?); for the sake of security and efficiency, you should learn to use parameterized query rather than concatenating variables in the query string.
To get the result you want you'll need to perform an aggregation on top of the UNION (but before the ORDER BY).
For example:
select Column1, max(Column2) as Column2
from (
SELECT part as Column1, '' as Column2
FROM soldParts
WHERE part LIKE ?
UNION
SELECT part, vin
FROM vinData
WHERE part LIKE ?
) x
GROUP BY Column1
ORDER BY Column1
I have a table like this
Using the COALESCE() function...
SELECT COALESCE(column1, column2, column3) combinedColumn from t;
I get this...
However, I want this....
I found a work around using UNION ALL but this isn't very elegant. Is there a function that works like COALESCE() except includes all values? Thanks
You can't use a coalesce here as there might be more than one value to return
UNION is the best solution (not UNION ALL because blanks)
select column1 from mytable
UNION
select column2 from mytable
UNION
select column3 from mytable
That said, if you want to maintain duplicates (if any), it's UNION ALL, or joining all the columns into a single string then splitting them out again (avoiding the UNION ALL at any cost)
Let's say I have a table
with
Column1
A
A
A
B
B
C
C
C
Column2
CH
FH
FH
BW
CH
AW
Now: I want to have a select sth---> result 6 different combinations of column1 and column 2.
If i say select count(column2): I'll take 8.I don't want it.
If i say select(distinct column2): I'll take 5.I don't want it either.
The 6 different results I look for are: A-QW,A-CH,B-FH,C-BW,C-CH,C-AW
I'm looking for combinations.
Can you helpt me?
Use GROUP BY clause:
SELECT Column1,Column2 FROM tblname GROUP BY Column1,Column2
Grouping will do what you need. It will conveniently eliminate the duplicates for you.
select column1, column2
from <your_table>
group by column1, column2
Pretty sure this will do what you want. demo here
update based on comment
select count(*) from (
select column1, column2
from <your_table>
group by column1, column2
) q
updated demo
In some cases, we can use an expression which combines the two columns...
As an example:
SELECT COUNT(DISTINCT CONCAT(t.column1,'-',t.column2)) AS cnt_combinations
FROM mytable t
This only works in the special case, where we know that a dash character isn't included as a character in column1 or column2. Adding that delimiter character isn't strictly necessary either.
In the more general case, this wouldn't produce the "correct" results if the contents of the columns included values like.
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
A -CW A--CW
A- CW A--CW
Since the result of the expression is the same value, those won't be seen as distinct values.
The same thing happens with NULL values, because CONCAT() will return NULL whenever any of the argument values is NULL.
For example, using the string NULL to represent a NULL value
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
NULL CW NULL
A NULL NULL
I have a query:
(SELECT col1 AS table1 FROM table1 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1 AS table2 FROM table2 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1 AS table3 FROM table3 WHERE col3 IS NOT NULL)
However when I process this using PDO and the fetchAll(PDO::FETCH_ASSOC); command, the keys to the array generated all come out as table1 irrespective of the table they are actually from.
Is my syntax incorrect? Thanks!
Your query returns a single column. A single column can only have one name/alias. In a UNION query, the first subquery defines the resulting set's column names.
If you want to specify which table each value has come from, add another column, e.g. like this:
(SELECT col1, 'table1' AS src FROM table1 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1, 'table2' FROM table2 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1, 'table3' FROM table3 WHERE col3 IS NOT NULL)
That's the SQL specification: The column names of the result set are taken from the first select.
It's just the way it is.
When you think about it, having different column names in subsequent selects makes no sense, because a column name can't change part way through a result set - column names are defined (once) for the (entire) result set.
Yes, this the way it works, the unioned values will have the alias from the column in the first query. Quoted from UNION documentation page:
The column names from the first SELECT statement are used as the
column names for the results returned.
If you didn't need that, give that column the alias you want.