i need some help to make a query in sql
suppose i have: table name: register
**id_r | name | text |**
1 | name 1 | text 1 |
2 | name 2 | text 2 |
and table name: details
**id_d | id_r | text_d |**
1 | 1 | text 1a |
2 | 1 | text 1b |
3 | 2 | text 2a |
and i need the result like:
**id_r | name | text | text_d_a | text_d_b |**
1 | name 1 | text 1 | text 1a | text 1b |
2 | name 2 | text 2 | text 2a
how can i do the sql statement to have something like this ??
It's possible:
SELECT
R.id_r, R.name, R.[text],
D1.text_d AS 'text_d_a',
D2.text_d AS 'text_d_b'
FROM
register R
LEFT JOIN details D1 ON R.id_r = D1.id_r AND D1.text_d LIKE '%a%'
LEFT JOIN details D2 ON R.id_r = D2.id_r AND D2.text_d LIKE '%b%'
I think it is bad idea to use this structure, because your query will depends tables data. You can implode text_d values with comma, for example:
SELECT t1.*,
(SELECT GROUP_CONCAT(t2.`text_d` SEPARATOR ',') FROM `details` as t2
WHERE t2.`id_r`=t1.`id_r`) as `text_d`
FROM `register` as t1
Related
I currently have a view in SQL Server, something like this:
Table1:
Id
Desc
Mex
Table2:
Id
IdTab1
Desc
The view select everything from Table1 left joined on Table2 on Id - IdTab1
Now I have a table 3 joined with Table2 that has like these fields:
Table3:
Id
IdTab2
Code (VarChar(3))
I would like to have in the select of the view a new field Code that contains every code in table 3 concatenated with the char ' ' without changing the record displayed from the old query (so like doing a group by concat) every Code that matches the join.
I saw some other posts but neither of them used this kind of approach.
For example using this:
declare #result varchar(500)
set #result = ''
select #result = #result + ModuleValue + ', '
from TableX where ModuleId = #ModuleId
But I have faced two problems.
I could not use declare in the view (probably because of wrong syntax), and also I have to do this group by and I can't figure out how.
Example result basic view
ID | IDTAB2 | DESC1 | DESC2 | MEX
1 | 2 | aa | bb | 4
2 | 1 | ab | cc | 2
2 | 2 | bb | bc | 2
Example result joined Table3
ID | IDTAB2 | DESC1 | DESC2 | MEX | CODE
1 | 2 | aa | bb | 4 | CS
1 | 2 | aa | bb | 4 | NN
2 | 1 | ab | cc | 2 | AF
2 | 2 | bb | bc | 2 | DC
2 | 2 | bb | bc | 2 | KK
2 | 2 | bb | bc | 2 | JD
Example result needed
ID | IDTAB2 | DESC1 | DESC2 | MEX | CODENEW
1 | 2 | aa | bb | 4 | CS NN
2 | 1 | ab | cc | 2 | AF
2 | 2 | bb | bc | 2 | DC KK JD
Considering your output from "Example result joined Table3", you can try this below option based on your SQL server version-
For MSSQL-2016 and earlier- Demo
SELECT DISTINCT A.ID,A.IDTAB2,A.DESC1,A.DESC2,A.MEX,
SUBSTRING(
(
SELECT ' '+ B.CODE AS [text()]
FROM your_table B
WHERE B.ID = A.ID
AND B.IDTAB2 = A.IDTAB2
AND B.DESC1 = A.DESC1
AND B.DESC2 = A.DESC2
AND B.MEX = A.MEX
ORDER BY B.ID,B.IDTAB2,B.DESC1,B.DESC2,B.MEX
FOR XML PATH ('')
), 2, 1000) [C_Name]
FROM your_table A
For MSSQL-2017 or newer- Demo
SELECT ID,IDTAB2,DESC1,DESC2,MEX,
STRING_AGG ( CODE, ' ' )
FROM your_table
GROUP BY ID,IDTAB2,DESC1,DESC2,MEX
There is a flag in mysql which enable/disable full group by (ONLY_FULL_GROUP_BY) But I am searching for same flag or connection string param to disable full group by in H2 database.
Here is my sample data.
+----+---------------+-----------------+
| ID | SYNC_ID | LIFECYCLE_EVENT |
+----+---------------+-----------------+
| 3 | 41 | 2 |
+----+---------------+-----------------+
| 2 | 41 | 1 |
+----+---------------+-----------------+
| 4 | 69 | 1 |
+----+---------------+-----------------+
| 5 | 69 | 3 |
+----+---------------+-----------------+
Here is my desired output:
+----+---------------+-----------------+
| ID | SYNC_ID | LIFECYCLE_EVENT |
+----+---------------+-----------------+
| 3 | 41 | 2 |
+----+---------------+-----------------+
| 5 | 69 | 3 |
+----+---------------+-----------------+
And Here is my query which I am trying to use for desired output
select id, sync_id, max(lifecycle_event)
from asset
where asset_type = 1
GROUP BY sync_id;
But it gives me ErrorCode: 90016 which means all non aggregate columns should be selected in group by. If I do so I get the wrong data results.
So Clearly I do not want to group by on id and sync_id . Please guide me how can I achieve this in H2 database.
There is no such thing in almost any other database -- because group by is not broken in those databases. You can phrase the query like this:
select a.*
from asset a
where asset_type = 1 and
lifecycle_event = (select max(lifecycle_event) from asset a2 where a2.sync_id = a.sync_id);
you can try below -
select max(id),sync_id, max(lifecycle_event)
from asset
where asset_type = 1
GROUP BY sync_id
I don't see any point in trying to emulate MySQL's broken group by implementation.
You can rewrite your query to
select a1.id, a1.sync_id, a2.max_even
from asset a1
join (
select sync_id, max(lifecycle_event) as max_event
from asset
group by sync_id
) a2 on a2.sync_id = a1.sync_id
and a2.max_event = a1.lifecycle_event;
my application get books and insert each word of this books to database, it may have more than 100 million words in books and inserted to database.
Now I want get specific word with previous and next words.
time to get result is very important.
for example : "The book words insert here in this table..."
------------------------------
| ID | word |
------------------------------
| 1 | the |
| 2 | book |
| 3 | words |
| 4 | insert |
| 5 | here |
| 6 | in |
| 7 | this |
| 8 | table |
| . | . |
| . | . |
| . | . |
------------------------------
or in other example:
------------------------------
| ID | word |
------------------------------
| 1 | my |
| 2 | name |
| 3 | is |
| 4 | joseph |
| 5 | and |
| 6 | my |
| 7 | father |
| 8 | name |
| 9 | is |
| 10 | brian |
------------------------------
I want to get previous and next value of same word
For example I want get previous and next word of "name":
--------------------------
| my | name | is |
--------------------------
| father | name | is |
--------------------------
in other related post friends write codes but this code take long time to get result, I want get the result table quickly:
related post: [question] Get previous and next row from rows selected with (WHERE) conditions
Use Join to get the expected result for SQL Server 2005 plus.
create table words (id integer, word varchar(20));
insert into words
values
(1 ,'my'),
(2 ,'name'),
(3 ,'is'),
(4 ,'joseph'),
(5 ,'and'),
(6 ,'my'),
(7 ,'father'),
(8 ,'name'),
(9 ,'is'),
(10,'brian');
SELECT A.Id , C.word AS PrevName ,
A.word AS CurName ,
B.word AS NxtName
FROM words AS A
LEFT JOIN words AS B ON A.Id = B.Id - 1
LEFT JOIN words AS C ON A.Id = C.Id + 1
WHERE A.Word = 'name'
Result:
Fiddler Demo
I create index on my words column and set this code to get result quickly:
WITH CTE AS
(SELECT * FROM WordsTable WHERE word=N'Name')
SELECT
t2.word AS previousWord,
t1.word,
t3.word AS nextWord
FROM
WordsTable AS t2,
CTE AS t1,
WordsTable AS t3
WHERE
(t2.ID + 1)= t1.ID AND
(t3.ID - 1) = t1.ID
I have a table in access with the following format:-
ID | Name | Qty
----+--------+------
1 | A1 | 10
2 | A2 | 20
3 | A1 | 30
4 | A2 | 40
5 | A1 | 50
----+--------+------
I want to run a query which will return the sum of Qty for each row above it where the Name matches. So, the Output will be :-
ID | Name | Output
----+--------+---------
1 | A1 | 0
2 | A2 | 0
3 | A1 | 10
4 | A2 | 20
5 | A1 | 40
----+--------+----------
I am not being able to write the query. I think I need some kind of recursive query, but I'm not very well versed in SQL/Databases.
Access does not support recursion. The following query should do what you want (i called your table NameQty):
SELECT t1.Id,t1.name,sum(t2.Qty)
FROM NameQty t1
LEFT JOIN NameQty t2 ON t1.name=t2.name AND t1.Id>t2.Id
GROUP BY t1.Id,t1.name
ORDER BY t1.Id
I think you should also use some other column than ID for the definition of "above".
I have 2 tables, "table1" have 1 column to store "table2" column name. Table1 data as below:
ID | Desc | Table2ColName | Active
-------------------------------------
1 | 1 Day | D1 | Yes
2 | 2 Days | D2 | No
3 | 3 Days | D3 | Yes
Table2 data as below:
ID | ShopName | D1 | D2 | D3
----------------------------------
1 | Sp1 | 100 | 80 | 120
Then I want to join 2 table and just display the Active data, How do I using linq to query the result as below:
ID | ShopName | D1 | D3
---------------------------
1 | Sp1 | 100 | 120
I have try whole day but get noting, hope can help. Thanks
I assume you already got the answer you needed for this, but I figured id post it anyway. Your query should look something like this.
var results = from a in data.table1
join b in data.table2
on a.ID equals b.ID
where a.Active =='Yes'
select new
{
a.ID,
b.ShopName,
b.D1,
b.D2
};