Need single data from multiple similar DATA in database - sql

I want to find out the data in showing in multiple entries like this
5 296 19 7 887 DAAS GAGO {"password":""} 2017-06-24 13:33:15 2017-06-23 13:33:09 9 10 fuel 4 1 N125XP DAAS Requested
5 296 19 7 887 DAAS GAGO {"password":""} 2017-06-24 13:33:15 2017-06-23 13:33:09 9 10 fuel 4 1 N125XP DAAS Requested
I want to see this data once how to do it ?
I am using this Sql Statement to do so :
SELECT t1.trip_id,
t1.server_trip_id,
t1.aircraft_id,
t2.flight_id,
t3.server_fid,
t3.dep,
t3.dest,
t3.service_provider,
t3.flight_name,
t3.sta,
t3.std,
t4.booking_id,
t5.server_booking_id,
t5.type,
t5.trip_number,
t5.company_id,
t5.user_id,
t5.aircraft_registration,
t5.icao,
t6.status
FROM trip t1
JOIN trip_flight t2 ON t2.trip_id = t1.trip_id
JOIN flight t3 ON t3.fid = t2.flight_id
JOIN trip_flight_booking t4 ON t4.flight_id = t2.flight_id
JOIN booking t5 ON t5.booking_id = t4.booking_id OR t5.booking_id != t4.booking_id
JOIN booking_status t6 ON t6.booking_id = t5.booking_id OR t6.booking_id != t5.booking_id
where t1.aircraft_id="19"

Are you sure your joins are correct... this seems to match any row;
t5.booking_id = t4.booking_id OR t5.booking_id != t4.booking_id
as you have = or != with the same values....
That said, you can use DISTINCT
SELECT DISTINCT t1.trip_id , t1.server_trip_id, t1.aircraft_id,
t2.flight_id,
t3.server_fid,t3.dep,t3.dest,t3.service_provider,t3.flight_name,t3.sta,t3.std,
t4.booking_id,
t5.server_booking_id,t5.type,t5.trip_number,t5.company_id,t5.user_id,t5.aircraft_registration,t5.icao,
t6.status
FROM trip t1
JOIN trip_flight t2 ON t2.trip_id = t1.trip_id
JOIN flight t3 ON t3.fid = t2.flight_id
JOIN trip_flight_booking t4 ON t4.flight_id = t2.flight_id
JOIN booking t5 ON t5.booking_id = t4.booking_id OR t5.booking_id != t4.booking_id
JOIN booking_status t6 ON t6.booking_id = t5.booking_id OR t6.booking_id != t5.booking_id
where t1.aircraft_id="19"

Related

Nesting Queries to get multiple column results

Have two queries , one collects moves in based on property and unit type the other would collect based on Move Outs for the same data. when ran separately they yield the correct information (move outs are 6 and move ins are 11) Have tried nesting in select and from statements but not getting what i need. When nested within the select am getting the correct move outs per unit type, but each line for move ins is total move ins. I recall that the nesting here would only return one value but know there is a way to return the value for each row. Any assistance is appreciated.
SELECT
p.scode as PropNumber,
p.saddr1 propname,
ut.scode as UnitType,
COUNT(t.hmyperson) as Moveouts,
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t
JOIN unit u ON t.hunit = u.hmy
JOIN property p ON p.hmy = u.hproperty
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
t.dtmovein >= getdate() - 14
AND p.scode IN ('gsaff')
) mi
FROM
Property p
JOIN unit u ON u.hproperty = p.hmy
JOIN tenant t ON t.hunit = u.hmy
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
p.scode IN ('gsaff')
AND t.DTMOVEOUT >= getdate()- 14
GROUP BY
ut.scode,
p.scode,
p.saddr1
With this data is coming out like :
PropNumber Propname UnitType MoveOuts MoveIns
1 x tc2 1 11
1 x tc3 2 11
1 x tc4 1 11
1 x tc5 1 11
1 x tc6 1 11 <pre>
Move in column should display as
2
5
1
0
3
You need to correlate the subquery according to the record being processed in the outer query. This also requires that you use different table aliases in the subquery than in the outer query.
It is hard to tell without seeing sample data, however I would expect that you need to correlate with all non-aggregated columns in the outer query.
Try changing :
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t
JOIN unit u ON t.hunit = u.hmy
JOIN property p ON p.hmy = u.hproperty
JOIN unittype ut ON ut.hmy = u.HUNITTYPE
WHERE
t.dtmovein >= getdate() - 14
AND p.scode IN ('gsaff')
) mi
To :
(
SELECT COUNT(t.hmyperson) as MoveIns
FROM
tenant t1
JOIN unit u1 ON t1.hunit = u1.hmy
JOIN property p1 ON p1.hmy = u1.hproperty
JOIN unittype ut1 ON ut1.hmy = u1.HUNITTYPE
WHERE
t1.dtmovein >= getdate() - 14
AND p1.scode IN ('gsaff')
AND p1.scode = p.scode
AND p1.saddr1 = p.saddr1
AND ut1.scode = ut.scode
) mi

SQL Join Query for Solution for A(Table) selected and B(table) all Record

enter image description here
SELECT
TR.UserKeyCode, TR.ProductID, TR.Line_No
FROM
TBLT_RESULTDTL_DONE TR
LEFT OUTER JOIN
TBLM_PRODUCT TP ON TR.ProductID = TP.ProductID
WHERE
TR.UserKeyCode = 'E01_00001' AND TR.QustionID = 1
Result:
E01_00001 11 1
E01_00001 36 2
E01_00001 16 3
Product table all results and Result record 3 only i need others will be null
SELECT
TR.UserKeyCode, TR.ProductID, TR.Line_No
FROM
TBLM_PRODUCT TP
LEFT OUTER JOIN
TBLT_RESULTDTL_DONE TR ON TR.ProductID = TP.ProductID and TR.UserKeyCode = 'E01_00001' AND TR.QustionID = 1

SQL statement merge two rows into one

In the results of my sql-statement (SQL Server 2016) I would like to combine two rows with the same value in two columns ("study_id" and "study_start") into one row and keep the row with higest value in a third cell ("Id"). If any columns (i.e. "App_id" or "Date_arrival) in the row with higest Id is NULL, then it should take the value from the row with the lowest "Id".
I get the result below:
Id study_id study_start Code Expl Desc Startmonth App_id Date_arrival Efter_op Date_begin
167262 878899 954 4.1 udd.ord Afbrudt feb 86666 21-06-2012 N 17-08-2012
180537 878899 954 1 Afsluttet Afsluttet feb NULL NULL NULL NULL
And I would like to get this result:
Id study_id study_start Code Expl Desc Startmonth App_id Date_arrival Efter_op Date_begin
180537 878899 954 1 Afsluttet Afsluttet feb 86666 21-06-2012 N 17-08-2012
My statement looks like this:
SELECT dbo.PopulationStam_V.ELEV_ID AS id,
dbo.PopulationStam_V.PERS_ID AS study_id,
dbo.STUDIESTARTER.STUDST_ID AS study_start,
dbo.Optagelse_Studiestatus.AFGANGSARSAG AS Code,
dbo.Optagelse_Studiestatus.KORT_BETEGNELSE AS Expl,
ISNULL((CAST(dbo.Optagelse_Studiestatus.Studiestatus AS varchar(20))), 'Indskrevet') AS 'Desc',
dbo.STUDIESTARTER.OPTAG_START_MANED AS Startmonth,
dbo.ANSOGNINGER.ANSOG_ID as App_id,
dbo.ANSOGNINGER.ANKOMSTDATO AS Data_arrival',
dbo.ANSOGNINGER.EFTEROPTAG AS Efter_op,
dbo.ANSOGNINGER.STATUSDATO AS Date_begin
FROM dbo.INSTITUTIONER
INNER JOIN dbo.PopulationStam_V
ON dbo.INSTITUTIONER.INST_ID = dbo.PopulationStam_V.SEMI_ID
LEFT JOIN dbo.ANSOGNINGER
ON dbo.PopulationStam_V.ELEV_ID = dbo.ANSOGNINGER.ELEV_ID
INNER JOIN dbo.STUDIESTARTER
ON dbo.PopulationStam_V.STUDST_ID_OPRINDELIG = dbo.STUDIESTARTER.STUDST_ID
INNER JOIN dbo.UDD_NAVNE_T
ON dbo.PopulationStam_V.UDDA_ID = dbo.UDD_NAVNE_T.UDD_ID
INNER JOIN dbo.UDDANNELSER
ON dbo.UDD_NAVNE_T.UDD_ID = dbo.UDDANNELSER.UDDA_ID
LEFT OUTER JOIN dbo.PERSONER
ON dbo.PopulationStam_V.PERS_ID = dbo.PERSONER.PERS_ID
LEFT OUTER JOIN dbo.POSTNR
ON dbo.PERSONER.PONR_ID = dbo.POSTNR.PONR_ID
LEFT OUTER JOIN dbo.KønAlleElevID_V
ON dbo.PopulationStam_V.ELEV_ID = dbo.KønAlleElevID_V.ELEV_ID
LEFT OUTER JOIN dbo.Optagelse_Studiestatus
ON dbo.PopulationStam_V.AFAR_ID = dbo.Optagelse_Studiestatus.AFAR_ID
LEFT OUTER JOIN dbo.frafaldsmodel_adgangsgrundlag
ON dbo.frafaldsmodel_adgangsgrundlag.ELEV_ID = dbo.PopulationStam_V.ELEV_ID
LEFT OUTER JOIN dbo.Optagelse_prioriteterUFM
ON dbo.Optagelse_prioriteterUFM.cpr = dbo.PopulationStam_V.CPR_NR
AND dbo.Optagelse_prioriteterUFM.Aar = dbo.frafaldsmodel_adgangsgrundlag.optagelsesaar
LEFT OUTER JOIN dbo.frafaldsmodel_stoettetabel_uddannelser AS fsu
ON fsu.id_uddannelse = dbo.UDDANNELSER.UDDA_ID
AND fsu.id_inst = dbo.INSTITUTIONER.INST_ID
AND fsu.uddannelse_aar = dbo.frafaldsmodel_adgangsgrundlag.optagelsesaar
WHERE dbo.STUDIESTARTER.STUDIESTARTSDATO > '2012-03-01 00:00:00.000'
AND (dbo.Optagelse_Studiestatus.AFGANGSARSAG IS NULL
OR dbo.Optagelse_Studiestatus.AFGANGSARSAG NOT LIKE '2.7.4')
AND (dbo.PopulationStam_V.INDSKRIVNINGSFORM = '1100'
OR dbo.PopulationStam_V.INDSKRIVNINGSFORM = '1700')
GROUP BY dbo.PopulationStam_V.ELEV_ID,
dbo.PopulationStam_V.PERS_ID,
dbo.STUDIESTARTER.STUDST_ID,
dbo.Optagelse_Studiestatus.AFGANGSARSAG,
dbo.Optagelse_Studiestatus.KORT_BETEGNELSE,
dbo.STUDIESTARTER.OPTAG_START_MANED,
Studiestatus,
dbo.ANSOGNINGER.ANSOG_ID,
dbo.ANSOGNINGER.ANKOMSTDATO,
dbo.ANSOGNINGER.EFTEROPTAG,
dbo.ANSOGNINGER.STATUSDATO
I really hope somebody out there can help.
Many ways, this will work:
WITH subSource AS (
/* Your query here */
)
SELECT
s1.id,
/* all other columns work like this:
COALESCE(S1.column,s2.column)
for example: */
coalesce(s1.appid,s2.appid) as appid
FROM subSource s1
INNER JOIN subSource s2
ON s1.study_id =s2.study_id
and s1.study_start = s2.study_start
AND s1.id > s2.id
/* I imagine some other clauses might be needed but maybe not */
The rest is copy paste

Simplify SQL request

I have a sql query i use in sqlite which in fact is a UNION of 3 SELECT statements.
I'd like to simplify it. Could someone help me?
Select *, "A" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_Implantation >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240))
UNION ALL
select *, "A" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240) EXCEPT
SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_042016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_042016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_042016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_042016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_042016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation)
FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240)
)
UNION ALL
select *, "S" as Status
FROM (SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_042016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_042016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_042016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_042016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_042016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240) EXCEPT
SELECT ADM_ID,
Dte_Implantation,
Dte_modif,
SUP_EMETTEUR_052016.EMR_ID as 'EMR_ID',
SUP_EMETTEUR_052016.STA_NM_ANFR as 'STA_NM_ANFR',
SUP_EMETTEUR_052016.EMR_LB_SYSTEME as 'EMR_LB_SYSTEME'
FROM SUP_STATION_052016
left outer join SUP_EMETTEUR_052016
ON SUP_STATION_052016.STA_NM_ANFR =
SUP_EMETTEUR_052016.STA_NM_ANFR
WHERE Dte_modif >
(SELECT MAX(Dte_Implantation)
FROM SUP_STATION_042016)
AND (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or
ADM_ID = 240)
)
It's a bit hard to help with simplifying without actual rewriting a query but i'll try to give general directions that may help.
1) You may unite first and second subquery using something like
WHERE (ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or ADM_ID = 240)
AND (Dte_Implantation > (select max ...)
OR (Dte_modif > (select max ...)
AND NOT EXISTS (SELECT 1 FROM SUP_STATION_052016 ...)))
Of course using WHERE NOT EXISTS or LEFT JOIN ... WHERE somefield IS NULL depends on your DBMS's optimizer, that choice is up to you
2) In addition to that you may also unite the third query by combining LEFT OUTER JOIN SUP_EMETTEUR_042016 ... with
CASE
WHEN OneTable.MandatoryField IS NULL AND AnotherTable.MandatoryField IS NOT NULL THEN 'S'
WHEN OneTable.MandatoryField IS NOT NULL AND AnotherTable.MandatoryField IS NULL THEN 'A'
WHEN ...
END AS Status
3) Also you may try to make a query a bit more readable by using table aliases (just to replace long terms like SUP_EMETTEUR_042016.EMR_LB_SYSTEME by something like SE4.EMR_LB_SYSTEME). May be replacing long condition ADM_ID = 6 or ADM_ID = 23 or ADM_ID = 137 or ADM_ID = 240 by a bit shorter ADM_ID IN (6, 23, 137, 240) will give a shot too.
4) To improve readability you may try to use common table expressions to get rid of multi-using (SELECT MAX(Dte_Implantation) FROM SUP_STATION_042016)
WITH max_di AS (SELECT MAX(Dte_Implantation) AS val FROM SUP_STATION_042016)
...(your select here)
FROM max_di
INNER JOIN SUP_STATION_052016 SS5
ON (..conditions here..
AND SS5.Dte_Implantation > max_di.val)
Hope something of this will help you somehow. Good luck in refactoring =)

Update Query with inner join ....?

I have this Table Named SubscriptionCharity, and its as the following :
SubscriptionId ChannelURI CharUserID
1 AX 12
2 ZA 7
3 AX 10
I want to to check for a specfic CharUserId if any record in the table with the same channel uri and set it to null , so in this table "Subcriptionid =1 " should be set to null
I have two queries for this , one works in MS SQL Managment m but doesn't work with LINQ :
(it doesn't affect the rows )
UPDATE SubscriptionCharProg
Set ChannelURI = null
FROM SubscriptionCharProg as t1
INNER JOIN SubscriptionCharProg as t2
on t1.ChannelURI = t2.ChannelURI
and t1.DeviceId = t2.DeviceId
WHERE CharUserId! = 5
and Another One that works for LINQ but not with MS SQL Managment :
(MS SQL Managment Error : Invalid Object Name "t1")
context.ExecuteStoreCommand("UPDATE t1 Set t1.ChannelURI = 1 FROM SubscriptionCharProg as t1 INNER JOIN
SubscriptionCharProg as t2 on t1.ChannelURI = t2.ChannelURI WHERE t1.CharUserId! = "
+ charuserID + "", null);
Also , the main problem is , both queries nulls every channelURi Whether its equal or not
Any ideas please?
Edit : in case if the a record with a specific charuserID is exist twice or more it keeps and doesn't set it to null
Change the first line from:
UPDATE SubscriptionCharProg
to
UPDATE t1
Try this:
UPDATE t1
SET ChannelURI = null
FROM SubscriptionCharProg as t1
INNER JOIN SubscriptionCharProg as t2
on t1.ChannelURI = t2.ChannelURI
and t1.DeviceId = t2.DeviceId
and t1.CharUserID <> t2.CharUserID
WHERE t2.CharUserID = 5
update quiz_subgroup as sg
inner join (select qq_qsg_id, count(qq_id) as cnt_ques from quiz_questions group by qq_qsg_id) as ques on(ques.qq_qsg_id = sg.qsg_id)
set sg.qsg_count_of_questions = ques.cnt_ques;