I have a SQL Server table. Now this table has columns like primary key Id, A, B, C, D, E, F, G
Now I want to select rows from this table like this
A=A, B=B, C=C, D=D and G > 132
So I am trying to select rows from this table which rows A,B,C,D columns has same data and G column data > 132.
So how can I do that ? Thank you.
I tried this query but returning same Id rows
SELECT TableA.Id,TableA.UserId,TableA.MaximumHp,TableA.Attack,TableA.Defense,TableA.SpAttack,TableA.SpDefense,TableA.Speed
FROM myTable as TableA
Inner Join myTable as TableB on
TableA.MaximumHp = TableB.MaximumHp
AND TableA.Attack = TableB.Attack
AND TableA.Defense = TableB.Defense
AND TableA.SpAttack = TableB.SpAttack
AND TableA.SpDefense = TableB.SpDefense
AND TableA.Speed = TableB.Speed
AND TableA.Id != TableB.Id
SQL Server 2008 R2
Sounds like you want to join the table to itself
SELECT *
FROM Table t1
Inner Join Table t2 on t1.A = t2.A
AND t1.B = t2.B
AND t1.C = t2.C
AND t1.D = t2.D
AND t1.G > 132
AND t1.ID <> t2.ID
I THINK what you mean is duplicates. Tell me if this is what you are looking for.
SELECT [Table].A, [Table].B, [Table].C, [Table].D, [Table].E, [Table].F, [Table].G
FROM [Table] LEFT JOIN (SELECT A, B, C, D FROM [Table]
GROUP BY A, B, C, D
HAVING count(*) > 1)
AS sub ON ([Table].A=sub.A) AND ([Table].B=sub.B) AND ([Table].C=sub.C) AND ([Table].D=sub.D)
WHERE G>132 and sub.A is not null;
This will give you all the rows where a,b,c, and D are equal to another row in the table...and G > 132
Related
I've got multiple tables
I made my query like this :
SELECT a.creation, b.caseno, c.instanceno
FROM TableB b
JOIN TableA a
ON a.caseno = b.caseno
JOIN TableC c
ON c.caseno = b.caseno
WHERE a.creation BETWEEN '2021-01-01' AND '2021-12-31'
I've got TableD who contains the following column
| InstanceNo | Position | Creation | TaskNo |
The idea is to add a new colum (result) on my query.
If instance from c.instanceno exist on tableD and taskno is 30 or 20, in that case i would like the d.creation but for the max(position).
If not the value null is enough for the column result.
SELECT a.creation, b.caseno, c.instanceno, d.creation
FROM TableB b
JOIN TableA a
ON a.caseno = b.caseno
JOIN TableC c
ON c.caseno = b.caseno
LEFT JOIN (SELECT MAX(position) position, instanceno, creation, taskno FROM TableD GROUP BY instanceno, creation, taskno) d
ON d.instanceno = c.instanceno
AND d.taskno in (20,30)
WHERE a.creation BETWEEN '2021-01-01' AND '2021-12-31'
I have two tables in Heidi SQL, t1 and t2. I am trying to select the first 15 rows from the two tables 2 columns, column1 (t1) and column2 (t2) when two conditions are met: id1 (t1) = id1 (t2) and id2 (t1) = id2 (t2).
I tried:
SELECT a.column1, a.id1, a.id2, b.column2, b.id1, b.id2
FROM t1 a, t2 b LIMIT 15;
WHERE a.id1 = b.id1 AND a.id2 = b.id2
The selected rows do not follow the condition and I get the following error:
Error: SQL Error (1064): You have an error in your SQL syntax; check
the manual that corresponds to your MySQL Server version for the right
syntax to use near "WHERE a.id1 = b.id1 AND a.id2 = b.id2"
Any ideas?
You have to move the WHERE clause between the FROM clause and the LIMIT clause:
SELECT a.column1, a.id1, a.id2, b.column2, b.id1, b.id2
FROM t1 a, t2 b
WHERE a.id1 = b.id1 AND a.id2 = b.id2
LIMIT 15;
Lets assume we have the following table1:
1 2 3
a x m
a y m
b z m
I want to do an inner join on the table
INNER JOIN tabel2 ON table1.2 = table2.2
Somehow like this, but additional a condition that the value of table1.1 not unique. Thus on table1.1 = b no inner join will occure in this example.
What is the best way to achieve this?
Using a an aggregate in a sub query is how I would do it
SELECT *
FROM table1
JOIN table2
ON table1."2" = table2."2"
JOIN (
SELECT "1"
FROM table1
GROUP BY "1"
HAVING COUNT(*) > 1
) AS sub_q
ON sub_q."1" = table1."1";
Another option might be a cte or temporary table to hold the rows you're joining on
WITH _cte AS
(
SELECT "1"
FROM table1
GROUP BY "1"
HAVING COUNT(*) > 1
)
SELECT *
FROM table1
JOIN table2
ON table1."2" = table2."2"
JOIN _cte AS cte
ON cte."1" = table1."1";
temp table:
CREATE TEMPORARY TABLE _tab
(
"1" varchar
);
INSERT INTO _tab
SELECT "1"
FROM table1
GROUP BY "1"
HAVING COUNT(*) > 1;
SELECT *
FROM table1
JOIN table2
ON table1."2" = table2."2"
JOIN _tab AS tab
ON tab."1" = table1."1";
I have a query like this:
Select * from
(Select a, b, c from table1 where some condition) as Result1,
(Select d, e from table2 where some another condition) as Result2
everything is OK until one of the nested selects returns nothing, then another select returns nothing too in finally select.
please tell me what is wrong with me?
As per my comment above, the following should work how you expect:
select
*
from
(select a, b, c from table1 where predicate1) Result1
full outer join
(select d, e from table2 where predicate2) Result2 on
1 = 1
Try:
Select (
(Select a, b, c from table1 where some condition) as Result1,
(Select d, e from table2 where some another condition) as Result
)
Try an inner or self join
Post some result samples to get a better understanding of the issue.
In postgresql I got 4 tables
Table A:
-----------
a_id
a_date
Table B
-----------
a_id b_id
Table C:
-------------------
c_id
b_id
invoice_number
Table D
-------------------
d_id
invoice_number
value_D
Multiple records have value_D
I would like to select Table A, Table B, Table C and Table D, where a_date BETWEEN X AND Y.
However, I would also like to select all the other value_D that are not included in my selection (so A innerjoin B innerjoin C full outerjoin D)
my code
SELECT
Table A, Table B, Table C, Table D
FROM
Table A
JOIN
Table B ON A.a_id = B.a_id
JOIN
Table C ON B.b_id = C.b_id
FULL OUTER JOIN
Table D ON C.invoice_number = D.invoice_number
WHERE
A.a_date BETWEEN X AND Y;
It only shows D.value_d for the A.a_id, where A.a_date BETWEEN X and Y.
I would like however that D.value_d would also be shown for A.a_id, where A.a_date is also other.
I am kinda a newbie, so hopefully it is understandable and you could help me.
Thanks in advance
You can also add more conditions to the Where clause, for example:
"a_date BETWEEN X AND Y OR a_date > '2015-04-21'". This will retrieve the union of both conditions.
Regards
I think I solved it.
SELECT Table A, Table B, Table C, Table D
FROM Table A
JOIN Table B ON A.a_id = B.a_id
JOIN Table C ON B.b_id = C.b_id
JOIN Table D ON C.invoice_number = D.invoice_number
WHERE A.a_date BETWEEN X AND Y OR
D.value_D IN (SELECT D.value_D
FROM Table D
JOIN Table C ON D.invoice_number on C.invoice_number
JOIN Table B ON C.b_id = B.b_id
JOIN Table A ON B.a_id = A.a_id
WHERE A.a_date BETWEEN X AND Y);
Thank you all for the help guys!