I was trying to extract some info from the database using this query,
SELECT dbo.AfterHoursParameterSetup.Customer_id
, dbo.tblCustomer.Company
, dbo.tblDevices.device_id
, dbo.AfterHoursParameterSetup.DeviceMasterID
, dbo.tblDriverType.DriverType
, dbo.tblGroups.GroupName
, dbo.AfterHoursParameterSetup.ReportBy
, dbo.AfterHoursParameterSetup.TripTimeFilter
, dbo.AfterHoursParameterSetup.chkWeekEndTravel
, dbo.AfterHoursParameterSetup.WeekdayStart
, dbo.AfterHoursParameterSetup.WeekDayEnd
, dbo.AfterHoursParameterSetup.WeekEndFrom
, dbo.AfterHoursParameterSetup.WeekEndTo
, dbo.AfterHoursParameterSetup.ReportType
FROM dbo.AfterHoursParameterSetup
FULL JOIN dbo.tblCustomer
ON dbo.AfterHoursParameterSetup.Customer_id = dbo.tblCustomer.Customer_id
FULL JOIN dbo.tblDriverType
ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblDriverType.DriverType_id
FULL JOIN dbo.tblGroups
ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblGroups.Group_id
FULL JOIN dbo.tblDevices
ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblDevices.device_id
ORDER BY dbo.AfterHoursParameterSetup.Customer_id ASC
Is there another way to structure this query so that I can eliminate the NULL values? dbo.AfterHoursParameterSetup.DeviceMasterID links everything together, but that column uses values from three different tables, so where there is no matching value, it returns NULL for all of the other columns selected where no matching join value was present.
If I understand your question, you want intersection of tables. If so user inner join.
SELECT dbo.AfterHoursParameterSetup.Customer_id
, dbo.tblCustomer.Company
, dbo.tblDevices.device_id
, dbo.AfterHoursParameterSetup.DeviceMasterID
, dbo.tblDriverType.DriverType
, dbo.tblGroups.GroupName
, dbo.AfterHoursParameterSetup.ReportBy
, dbo.AfterHoursParameterSetup.TripTimeFilter
, dbo.AfterHoursParameterSetup.chkWeekEndTravel
, dbo.AfterHoursParameterSetup.WeekdayStart
, dbo.AfterHoursParameterSetup.WeekDayEnd
, dbo.AfterHoursParameterSetup.WeekEndFrom
, dbo.AfterHoursParameterSetup.WeekEndTo
, dbo.AfterHoursParameterSetup.ReportType
FROM dbo.AfterHoursParameterSetup
INNER JOIN dbo.tblCustomer ON dbo.AfterHoursParameterSetup.Customer_id = dbo.tblCustomer.Customer_id
INNER JOIN dbo.tblDriverType ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblDriverType.DriverType_id
INNER JOIN dbo.tblGroups ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblGroups.Group_id
INNER JOIN dbo.tblDevices ON dbo.AfterHoursParameterSetup.DeviceMasterID = dbo.tblDevices.device_id
ORDER BY dbo.AfterHoursParameterSetup.Customer_id ASC
Related
Starting from the last two queries, how can I use the first one?.
select t.columna1,(sum(v.val::float) as suma from t
cross join lateral unnest (string_to_array(columna2,',') as v(val)
group by t.columna1;
in the first one, together with the user_id and I see who is in the pos_payment table
SELECT DISTINCT pos_payment.pos_order_id, pos_order.user_id, SUM(pos_order.amount_paid) FROM
pos_order
LEFT JOIN pos_payment ON pos_order.id = pos_payment.pos_order_id
GROUP BY pos_payment.pos_order_id , pos_order.user_id;
SELECT DISTINCT pos_payment.pos_order_id, pos_order.user_id , pos_order.amount_paid FROM
pos_order
LEFT JOIN pos_payment ON pos_order.id = pos_payment.pos_order_id
GROUP BY pos_payment.pos_order_id , pos_order.user_id , pos_order.amount_paid ;
I have the following query. The rank in the ‘causing the query to run a very long time. I am trying to reduce the time.
Can anyone help me with this? Thanks
SELECT
REC.INPATIENT_DATA_ID
, RANK() over (Partition by PATS.PAT_ENC_CSN_ID, MEAS.FLO_MEAS_ID order by RECORDED_TIME) 'VITALS_RANK'
, MEAS.RECORDED_TIME
, PATS.PAT_ENC_CSN_ID
, PATS.PAT_ID
, PATS.CONTACT_DATE
, MEAS.FLO_MEAS_ID
, PATS.DEPARTMENT_ID
, PAT.IS_TEST_PAT_YN
, PATS.HOSP_DISCH_TIME
, PATS.HOSP_ADMSN_TIME
FROM CLARITY.DBO.IP_FLWSHT_REC REC
LEFT OUTER JOIN CLARITY.DBO.PAT_ENC_HSP PATS ON PATS.INPATIENT_DATA_ID = REC.INPATIENT_DATA_ID
LEFT OUTER JOIN CLARITY.DBO.CLARITY_DEP AS DEP ON PATS.DEPARTMENT_ID = DEP.DEPARTMENT_ID
LEFT OUTER JOIN CLARITY.DBO.PATIENT_3 PAT ON PAT.PAT_ID = PATS.PAT_ID
LEFT OUTER JOIN CLARITY.DBO.IP_FLWSHT_MEAS MEAS ON REC.FSD_ID = MEAS.FSD_ID
I'm writing a query that will bring me back either row or rank '1'. When I run the query with the 'where rank or row clause equals 1', no data returns for those columns but if I take out the 'where rank or row clause' the data populates. Can someone tell me why the where clause is causing no data to return?
LEFT OUTER JOIN
(SELECT DISTINCT CLMED.SIMPLE_GENERIC_C
, MEDINFO.MEDICATION_ID as [CYTOTEC]
,FMED.PAT_ENC_CSN_ID
, FMED.HOSP_ADMSN_DATE
-- , MIN(FMED.TAKEN_DATETIME) over (Partition by FMED.PAT_ENC_CSN_ID,
MEDINFO.MEDICATION_ID ) as 'FIRST DOSE'
, MIN(FMED.TAKEN_DATETIME) over (Partition by FMED.PAT_ENC_CSN_ID, CLMED.SIMPLE_GENERIC_C
) AS 'FIRST DOSE'
, RANK() over (Partition by FMED.PAT_ENC_CSN_ID, CLMED.SIMPLE_GENERIC_C order by
FMED.TAKEN_DATETIME DESC) 'CYTOTEC_RANK'
, ROW_NUMBER() over (Partition by FMED.PAT_ENC_CSN_ID, MEDINFO.MEDICATION_ID order by
FMED.TAKEN_DATETIME) AS 'CYTOTEC_ROW'
, FMED.DISPLAY_NAME
, FMED.ORDER_MED_ID
, ORDMED.DESCRIPTION
, ZCGEN.NAME
FROM MED_ADMIN FMED
LEFT JOIN MEDINFO MEDINFO ON FMED.ORDER_MED_ID = MEDINFO.ORDER_MED_ID
LEFT JOIN MED ORDMED ON FMED.ORDER_MED_ID = ORDMED.ORDER_MED_ID
LEFT JOIN MEDICATION CLMED ON MEDINFO.MEDICATION_ID = CLMED.MEDICATION_ID
LEFT JOIN GENERIC ZCGEN ON CLMED.SIMPLE_GENERIC_C = ZCGEN.SIMPLE_GENERIC_C
WHERE ZCGEN.NAME = 'miSOPROStol'
AND 'CYTOTEC_RANK' = 1
) AS [CYT] ON CYT.PAT_ENC_CSN_ID = VITALS.PAT_ENC_CSN_ID
I am getting error Sub Query Returns more than 1 value
here is my Query
SELECT d.Description ,s.Version , d.UtiPrefix , d.UTI , d.PrimaryAC , s.ReportingObb , s.ReportingObb , d.LEI , d.LEI_Countp , d.LEI , s.ReportingDeleg ,d.Curr , c.Trade_Party_Domicile ,c.LEI_SGR
, Price =( select Price
From Price
inner join Derivatives
on Derivatives.UTI = Price.UTI)
FROM Derivatives as d
INNER JOIN Settings as s
ON d.LEI_SGR = s.LEI_SGR
INNER JOIN Clients c
ON d.LEI_SGR = c.LEI_SGR
Use a correlated subquery
Price =( select Price From Price p WHERE d.UTI = p.UTI)
I suspect this returns more than one row
( select Price
From Price
inner join Derivatives
on Derivatives.UTI = Price.UTI )
why are you mixing sub queries with joins?
SELECT d.Description ,s.Version , d.UtiPrefix , d.UTI , d.PrimaryAC , s.ReportingObb
, s.ReportingObb , d.LEI , d.LEI_Countp , d.LEI , s.ReportingDeleg ,d.Curr
, c.Trade_Party_Domicile ,c.LEI_SGR
, p.Price
FROM Derivatives as d
INNER JOIN Settings as s
ON d.LEI_SGR = s.LEI_SGR
INNER JOIN Clients c
ON d.LEI_SGR = c.LEI_SGR
INNER JOIN Price p
ON p.UTI = d.UTI
I want to know if it is possible to create another column in a table that has data that I wish to populate in this new column? The new column is Flag2. Here is the table:
what I want to do is, where item id is 30, I want the ITEM ID to only display 30 once and, populate the QC Unsupportted in Flag2? How do I do this?
I can only think of doing an inner join but this is not working.
This is what I have done in trying to do so:
SELECT
A.ITEMID, A.FLAG1, A.FLAG2
FROM
#FLAGS as A
INNER JOIN
#FLAGS as B ON A.ITEMID = B.ITEMID
GROUP BY
a.ITEMID, a.FLAG1, A.FLAG2
ORDER BY
ITEMID
Assuming I understand what you are after, if the current FLAG1 values are distinct for any ITEMID and you only have at most two instances of the same ID, I think this should do what you want:
SELECT
lft.ITEMID
, lft.FLAG1
, rght.FLAG1 FLAG2
FROM (
SELECT
t.ITEMID
, t.FLAG1
FROM (
SELECT
l.ITEMID
, l.FLAG1
, COUNT(l.ITEMID) i
FROM #FLAGS l
INNER JOIN #FLAGS r ON l.ITEMID = r.ITEMID
WHERE r.FLAG1 <= l.FLAG1
GROUP BY
l.ITEMID
, l.FLAG1) t
WHERE t.i=1) lft
LEFT OUTER JOIN (
SELECT
t.ITEMID
, t.FLAG1
FROM (
SELECT
l.ITEMID
, l.FLAG1
, COUNT(l.ITEMID) i
FROM #FLAGS l
INNER JOIN #FLAGS r ON l.ITEMID = r.ITEMID
WHERE r.FLAG1 <= l.FLAG1
GROUP BY
l.ITEMID
, l.FLAG1) t
WHERE t.i=2) rght ON lft.ITEMID = rght.ITEMID
-- Or better
SELECT
lft.ITEMID
, lft.FLAG1
, rght.FLAG1 FLAG2
FROM (
SELECT
t.ITEMID
, t.FLAG1
FROM (
SELECT
l.ITEMID
, l.FLAG1
, ROW_NUMBER() OVER(PARTITION BY ITEMID ORDER BY FLAG1) as i
FROM test l) t
WHERE t.i=1) lft
LEFT OUTER JOIN (
SELECT
t.ITEMID
, t.FLAG1
FROM (
SELECT
l.ITEMID
, l.FLAG1
, ROW_NUMBER() OVER(PARTITION BY ITEMID ORDER BY FLAG1) as i
FROM test l) t
WHERE t.i=2) rght ON lft.ITEMID = rght.ITEMID
If you have additional flag values for the same ID, a new outer join can be added to a new inline table (rght2, rght3, etc.) where i=3, 4, etc. and you are selecting rght2 AS FLAG3, rght3 AS FLAG4, etc.
Also note that the current values for FLAG1 will be distributed through FLAG1 and FLAG2 in alphabetical order. If you wanted to distribute them in reverse order you could replace <= with >=. If you had more than two flags that you wanted distributed in a specific order, you would have to create a separate table with a ranking value and join to that which would be doable but even uglier!