I'd like to do an insert select where the select statement has aggregate columns for use by a "HAVING" clause, but where I do not actually want those columns to be inserted. A simple example:
INSERT INTO table1 ( a )
SELECT a, MAX (b) AS maxb FROM table2
GROUP BY a
HAVING maxb = 1
Of course, this won't work because there are a different number of columns in the INSERT and the SELECT. Is there as simple way to make this work? I was hoping I could define some sort of null column in the INSERT field list, or something. I was hoping to avoid a subquery in my SELECT statement, although I could probably do it that way if necessary.
INSERT INTO table1 ( a )
SELECT a FROM (SELECT a, MAX (b) AS maxb FROM table2
GROUP BY a
HAVING maxb = 1) t
You can rewrite the query like this
INSERT INTO table1 ( a )
SELECT a FROM table2
GROUP BY a
HAVING MAX (b) = 1
Related
I want to do something like this:
SELECT SUM(qty)
FROM table1
WHERE colum1 LIKE (
SELECT colum2
FROM table2
WHERE condition2 LIKE 'value2'
)
It will work if the subquery returns a single row. When you use a subquery as an express, it must return a single value.
A more general method is to use a JOIN. This will sum the quantities of rows that match any of the selected colum2 values.
SELECT SUM(qty)
FROM (
SELECT DISTINCT id, qty
FROM table1
JOIN table2 ON table1.colum1 LIKE table2.colum2
WHERE table2.condition2 LIKE 'value2'
) x
You need the subquery with DISTINCT in case colum1 matches multiple patterns, so you don't sum the same quantity twice repeatedly for each match.
If value2 or table2.colum2 contains fixed strings rather than wildcard patterns, you should use = rather than LIKE.
You can make it work using
select Sum(column) from
Table1 where column IN
( Select column from TableName where
Condition);
I need to join two tables in SQL. There are no common fields. But the one table have a field with the value krin1001 and I need it to be joined with the row in the other table where the value is 1001.
The idea behind the joining is i have multiple customers, but in the one table there customer id is 'krin1001' 'krin1002' and so on, in this table is how much they have sold. In the other table there customer is is '1001' '1002' and so on, and in this table is there name and adress and so on. So it will always be the first 4 charakters i need to strip from the field before matching and joining. It might not always be 'krin' i need it to work with 'khjo1001' also, and it still needs to join on the '1001' value from the other table.
Is that possible?
Hope you can help me.
You need to use substring:
ON SUBSTRING(TableA.Field, 5, 4) = TableB.Field
Or Right:
ON RIGHT(TableA.Field, 4) = TableB.Field
You can also try to use CHARINDEX function for join operation. If value from table1 contains value from table2 row will be included in result set.
;WITH table1 AS(
SELECT 'krin1001' AS val
UNION ALL
SELECT 'xxx'
UNION ALL
SELECT 'xyz123'
),
table2 AS(
SELECT '1001' AS val
UNION ALL
SELECT '12345'
UNION ALL
SELECT '123'
)
SELECT * FROM table1 AS t
JOIN table2 AS T2 ON CHARINDEX(T2.val, T.val) > 0
Use it as:
SELECT
*
FROM table t1
INNER JOIN table t2 ON RIGHT(t1.col1, 4) = t2.col1;
im using the IN() function to match againts some ids.
SELECT * FROM my_table WHERE id IN(id1,id2,id3)
the thing is, now i need to calculate a SUM() based on the matched id, and i would like to do it on the same query. Something like
SELECT *,(SELECT SUM() WHERE id = the_matched_id) FROM my_table WHERE id IN(id1,id2,id3)
¿is it possible? maybe i should consider to change my query, or do it separately. ¿ What a do you suggest?
Thanks!
The matched ID is just the ID of each row from the outer table. You can use different aliases to compare these IDs.
SELECT *, (SELECT SUM(summableColName) FROM my_table t2 WHERE t2.id = t1.id)
FROM my_table t1 WHERE id IN (id1, id2, id3)
Try something like
SELECT id,sum(field) FROM my_table WHERE id IN(id1,id2,id3)
GROUP BY id
Is there a way to write a single query to check if a set of rows matches a set of values? I have one row per set of values that I need to match and I'd like to know if all rows are matched or not. I could perform this via multiple queries such as:
select * from tableName where (value1, value2) = ('someValue1', 'someValue2')
select * from tableName where (value1, value2) = ('someOtherValue1', 'someOtherValue2')
...and so on, up to an arbitrary number of queries. How could this sort of thing be re-written as a single query where the query returns ONLY if all values are matched?
You could try something like:
select t.*
from tableName t
join (select 'someValue1' value1, 'someValue2' value2 union all
select 'someOtherValue1', 'someOtherValue2') v
on t.value1 = v.value1 and t.value2 = v.value2
where 2=
(select count(distinct concat(v1.value1, v1.value2))
from (select 'someValue1' value1, 'someValue2' value2 union all
select 'someOtherValue1', 'someOtherValue2') v1
join tableName t1
on t1.value1 = v1.value1 and t1.value2 = v1.value2)
If you have a large number of value pairs that you want to check, it may be easier to insert them into a temporary table and use the temporary table in the above query, instead of two separate hard-coded virtual tables.
What about:
SELECT *
FROM tableName
WHERE value1 IN ('someValue1', 'someOtherValue1') AND
value2 IN ('someValue2', 'someOtherValue2')
Match if exactly two records found
Select students who got q13 wrong and Q14 right
SELECT qa.StudentID FROM questionAnswer qa, Student s
WHERE qa.StudentID=s.StudentID AND
((QuestionID=13 AND Pass=0) OR (QuestionID=14 AND Pass=1))
GROUP BY qa.StudentID
HAVING COUNT(*)=2;
The Where clause matches any records where q14 is correct and q13 is incorrect
We then group by the StudentID
The having requires there to be two records
How would I go about counting values that appear in column 1, but not column 2. They are from the same table, without using subqueries or anything fancy. They may or may not share other common column values (like col 3 = col 4) but this doesnt matter.
I have it almost working with subqueries, but cannot figure how to do it without. The only problem (I think) is it will count something twice if the primary key (composed of col1,col3,col4) are different but col1 is the same.
SELECT DISTINCT COUNT(*)
FROM mytable t1
WHERE NOT EXISTS (
SELECT DISTINCT *
FROM mytable
WHERE t1.column1 = mytable.column2
);
But like I said, I'm trying to figure this without subqueries anyways
How about:
SELECT COUNT(*)
FROM mytable mt1
LEFT JOIN mytable mt2 ON mt1.column1 = mt2.column2
WHERE mt2.column IS NULL
Please see this:
SELECT
SUM(IF(column1 = column2, 0, 1)) as c
FROM
mytable