I am using a database with two tables:
HEADING:(ID, CAPTION, CUSTOMER_ID,DATE, TDOC, ID_PAR) -- ID is PK
-- ID_PAR is FK Related
to HEADING.ID
LINES : (ID, NDOC,QTE) -- ID is PK NDOC is FK RELATED TO
HEADING.ID
HEADING has a recursive query to get all childs of a HEADING ID
for example an invoice with many delivery notes.
In that system the lines of an invoice are not typed we type only delivery notes
How to get all the lines of an invoice in case of the datasets are related as master-detail?
Heading dataset
ID PARENT_ID DOC_DATE DOC_TYPE
01 NULL A
02 01 B
03 02 C
04 02 C
Lines Dataset
ID_PK NDOC_FK CODE_PROD QTE
Heading
01 03 P1 5
02 03 P10 20
03 03 P67 65
04 04 P61 34
SQL LINES RESULT for heading.ID=01
PRODUCT QTE
P1 5
P10 20
P67 65
P61 61
Related
I need to create a summary table Which is a summary of the targets and sales.
Here are the tables.
target_table
customer_id
month
target
101
05
60
101
06
60
102
05
80
102
05
85
selse_table
customer_id
month
selse
101
05
40
101
06
70
102
05
90
102
05
60
Here is my query
CREATE TABLE SUMMERY
AS SELECT SALSE.customrt_id, SALSE.month, SALSE.selse, targets.target
FROM SALSE
Inner join TARGETS
on SALSE.CUSTOMER_ID=TARGETS.customer_ID
I try to use INNER JOIN but the problem is that it is not enough for me just the customer_id column because the same customer has different destinations every month.
Is my query correct? How to do the INNER JOIN right?
Don't understand your question very well,like this?
on SALSE.CUSTOMER_ID=TARGETS.customer_ID and SALSE.[MONTH]=TARGETS.[MONTH]
I have two tables:
Table 1: Input
Namecode | Number |
01 50
01 49
02 10
03 40
04 100
Table 2: Output
Namecode | Number |
01 15
02 25
This is my SQL query:
SELECT *
FROM Input
LEFT JOIN Output ON Input.Namecode = Output.Namecode
This is the result:
Namecode | Number | Namecode | Number
01 50 01 15
01 49 01 15
02 10 02 25
03 40 NULL NULL
04 100 NULL NULL
I want the result to be like this:
Namecode | Number | Namecode | Number
01 50 01 15
01 49 NULL NULL
02 10 02 25
03 40 NULL NULL
04 100 NULL NULL
How can I get this output? I want the Namecode 01 only show 1 row, the row left have no value because in Output table Namecode 01 only have 1 row
What query do I need to get the result I want?
You seem want to match max(Namecode) value if there are duplicate Namecode
You can try this query, write a subquery and left join
SELECT t1.*,t3.*
from Input t1
LEFT JOIN (SELECT Namecode,MAX(Number) Number FROM Input GROUP BY Namecode ) t2
ON t1.Number = t2.Number
LEFT JOIN Output t3
on t2.Namecode = t3.Namecode1
sqlfiddle
Result
Namecode Number Namecode1 Number1
01 50 01 15
02 10 02 25
01 49 null null
03 40 null null
04 100 null null
Select drl.id, drl.ap, drl.sqn, drl.date
from srs_drl drl
this will out output something like this:
14000001 01 01 05/11/2015
14000001 01 01 06/11/2015
14000001 01 01 01/12/2015
14000001 01 01 04/01/2016
15000234 01 02 05/11/2015
15000234 01 03 06/11/2015
15000234 01 03 01/12/2015
15000234 01 04 04/01/2016
For every unique first 3 columns I need to retrieve the earliest date. So for the above table I wish to return:
14000001 01 01 05/11/2015
15000234 01 02 05/11/2015
15000234 01 03 06/11/2015
15000234 01 04 04/01/2016
Any help with this query would be much appreciated. I've tried using TOP but that only returns the first record for the entire table rather than the first record grouped by the first 3 columns.
Thanks in advance
Do a GROUP BY combined with MIN:
Select drl.id, drl.ap, drl.sqn, MIN(drl.date)
from srs_drl drl
group by drl.id, drl.ap, drl.sqn
Alternatively, a NOT EXISTS to return a row if no one with same drl.id, drl.ap, drl.sqn is even earlier:
Select drl.id, drl.ap, drl.sqn, drl.date
from srs_drl drl
where not exist (select 1 from from srs_drl d2
where d2.id = drl.id
and d2.ap = drl.ap
and d2.sqn = drl.sqn
and d2.date < drl.date)
Note that date is a reserved word in ANSI SQL, so you may need to write "date".
In SQL Server I have 2 tables that looks like this:
TEST SCRIPT 'a collection of test scripts'
(PK)
ID Description Count
------------------------
A12 Proj/Num/Dev 12
B34 Gone/Tri/Tel 43
C56 Geff/Ben/Dan 03
SCRIPT HISTORY 'the history of the aforementioned scripts'
(FK) (PK)
ScriptID ID Machine Date Time Passes
----------------------------------
A12 01 DEV012 6/26/15 16:54 4
A12 02 DEV596 6/28/15 13:12 9
A12 03 COM199 3/12/14 14:22 10
B34 04 COM199 6/30/13 15:45 12
B34 05 DEV012 6/30/15 13:13 14
B34 06 DEV444 6/12/15 11:14 14
C56 07 COM321 6/29/14 02:19 12
C56 08 ANS042 6/24/14 20:10 18
C56 09 COM432 6/30/15 12:24 4
C56 10 DEV444 4/20/12 23:55 2
In a single query, how would I write a select statement that takes just one entry for each DISTINCT script in TEST SCRIPT and pairs it with the values in only the TOP 1 most recent run time in SCRIPT HISTORY?
For example, the solution to the example tables above would be:
OUTPUT
ScriptID ID Machine Date Time Passes
---------------------------------------------------
A12 02 DEV596 6/28/15 13:12 9
B34 05 DEV012 6/30/15 13:13 14
C56 09 COM432 6/30/15 12:24 4
The way you describe the problem is almost directly as cross apply:
select h.*
from testscript ts cross apply
(select top 1 h.*
from history h
where h.scriptid = ts.id
order by h.date desc, h.time desc
) h;
Please try something like this:
select *
from SCRIPT SCR
left join (select MAX(SCRIPT_HISTORY.Date) as Date, SCRIPT_HISTORY.ScriptID
from SCRIPT_HISTORY
group by SCRIPT_HISTORY.ScriptID
) SH on SCR.ID = SH.ScriptID
This is a little complicated so bear with me. Below is a table named "list" with 2 columns. The table has data of each member and the films that they like. i.e member 01 likes film 02, 05, 14, 21 and 25. What I want to find out is how many similar films does each member have with another member. For example, member 01 and member 02 have one film in common (film 14). Is there any way to write this in SQL?
List
------ ------
member film
------ ------
01 02
01 05
01 14
01 21
01 25
02 03
02 09
02 14
03 01
03 05
03 17
03 21
You can write a general query for this using a self-join and aggregation. The following summarizes the results for each pair of users:
select l1.member, l2.member, count(*) as NumFilmsInCommon
from list l1 join
list l2
on l1.member < l2.member and l1.film = l2.film
group by l1.member, l2.member;
The < condition just ensures that each pair of members only appears once in the result set.