SQLITE unwanted behavior on group_concat in query - sqlite

I have two tables like this:
Fiddle: click here
My query:
select
f.id,
'{{' || group_concat(f.key||','||ifnull(f.value,'NULL'), '},{')||'}}' as
key_value_pair_1,
'{{' || group_concat(r.key||','||ifnull(r.value,'NULL'), '},{')||'}}' as
key_value_pair_2
FROM items_functions as f
LEFT JOIN items_functions_2 as r ON f.id = r.id
GROUP BY f.id
But this results into a strange behavior. All results where shown multiple times as you can see when you run the above linked fiddle.
But what I want is a result like this:
id key_value_pair_1 key_value_pair_2
214808 {{16,662},{17,808},{33,1},{60,2}} {{16,662},{17,808},{33,1},{60,2}}
214809 {{16,902},{17,1103},{33,1},{60,2}} {{16,902},{17,1103},{33,1},{60,2}}
218965 {{19,808},{21,662},{33,1},{60,8}} {{19,808},{21,662},{33,1},{60,8}}
218966 {{19,1103}{21,902},{33,1},{60,8}} {{19,1103},{21,902},{33,1},{60,8}}
244574 {{16,999},{18,999},{54,174}} {{16,999},{18,999},{54,174}}
I guess my query has to be adjusted. :)
Would be awesome, if anyone is happen to have a solution on this.
Thanks in advance!
Best regards,
Andreas

I believe the following will do what you want :-
SELECT
p1.id, p1.kvp1, p2.kvp2
FROM
(
SELECT
id,
'{{' || group_concat(key||','||ifnull(value,'NULL'), '},{')||'}}'
AS kvp1
FROM items_functions GROUP BY id
)
AS p1
JOIN
(
SELECT
id,
'{{' || group_concat(key||','||ifnull(value,'NULL'), '},{')||'}}'
AS kvp2
FROM items_functions_2 GROUP BY id
)
AS p2
ON p1.id = p2.id
Which produces :-

Related

Selecting incorrect records

In following query, what is happening is that, the 3rd join is not being done. we are getting pharmacy match and then the display is showing patients in other facilities who share the same pharmacy, can you see why this would be happening?
Insert Into #tblNDC
SELECT ROW_NUMBER() OVER(ORDER BY ID_KEY DESC) AS RN,*
From
(
Select distinct A.PHARMACYNPI,
f.FACILITY_NAME,
ID_KEY,
[BATCH] AS column1,
[IMPORTDATE],
[DATEBILLED],
[RX],
[DATEDISPENSED],
[DAYSUPPLY],
[PAYTYPE],
A.[NPI],
[PHYSICIAN],
[COST],
[QUANTITY],
[MEDICATION],
A.[NDC],
f.FACILITY_ID
FROM [PBM].[T_CHARGES] A
LEFT OUTER JOIN [OGEN].[NDC_M_FORMULARY] B ON A.[NDC] = B.[NDC]
--Left Outer Join PBM.FACILITY f on A.FACILITYNPI = f.FACILITY_NPI
Left Outer Join PBM.PHARMACY_NPI pn on A.PHARMACYNPI = pn.NPI
Inner join PBM.PHARMACY_FACILITY pp on pn.PHARMACY_ID = pp.PHARMACY_ID
Inner Join PBM.FACILITY f on pp.FACILITY_ID = f.FACILITY_ID
Where [STAT] not in (3, 4, 5)
AND [TIER] <> 'T1'
AND f.FACILITY_ID IN
(
select FacilityID from #tblFacility
)
AND f.FACILITY_ID IN
(
SELECT * FROM [PBM].[Split1] (#selectedFacility)
)
--- it seems 3rd condition not being done ----------------------------------
Its hard to see for sure without knowing the data, but my first thoughts are that the Left Outer joins will be giving you joinage you might not want.
Go through each join and remove it until you start to get dodgy records back, if it is the 3rd one when it suddenly goes strange, then i suspect you have multiple IDs for a facility.

Merge 2 Tables Data in SQL

I have 3 Data Table Claim, Part and Labor.
In this Claim is parent table and Part and Labor is mapping tables of Claim and they have Part and Labor has the ClaimId as a Foreign Key.
Claim has data like:
Part has data Like
Labor table has data Like
Target Output would be:
Can anyone help me to achieve this in SQL server.
I have tried to solve with the Union/CTE but it did not gives the result as I want.
I got the same output (for your updated output screen) for this specific case. I don't know if any other data will work for you.
SELECT TMP.ClaimId
, CASE WHEN TMP.RowNum = 1 THEN TMP.Name ELSE NULL END AS ClaimName
, CASE WHEN TMP.RowNum = 1 THEN TMP.Note ELSE NULL END AS Note
, TMP.PartId
, TMP.PartNumber
, TMP.PartCost
, JOIN_L.LaborId
, JOIN_L.LaborCost
FROM (
SELECT C.ClaimId, C.Name, C.Note, P.PartId, P.PartNumber, P.PartCost
, ROW_NUMBER() OVER(PARTITION BY C.ClaimId ORDER BY P.PartId) AS RowNum
FROM Claim AS C
LEFT JOIN Part AS P ON C.ClaimId = P.ClaimId
)AS TMP
LEFT JOIN (
SELECT *
, ROW_NUMBER() OVER(PARTITION BY L.ClaimId ORDER BY L.ClaimId) AS RowNum
FROM Labor AS L
) AS JOIN_L ON (TMP.ClaimId = JOIN_L.ClaimId AND TMP.RowNum = JOIN_L.RowNum)
ORDER BY TMP.ClaimId
Not sure why you tried CTE here
Select C.ClaimId,C.name,C.Note,P.PartId,P.PartNumber,P.PartCost,L.LabourId,L.LabourCost
From Claim C
Left Outer Join Part P On P.ClaimId = C.ClaimId
Left Outer Join Labor L On L.ClaimId=C.ClaimId

PL-SQL Lastest record within a group: looking for alternate (PARTITION BY?) approaches

Using PL-SQL, I need to find the record with the lastest INVC_LN_ITEM_STAT_START_DT value within a group of records that share the same value for SHPMNT_LN_ITEM_KEY and RPT_PER_KEY.
How else might this be done? Are there analytical functions for this type of query?
SELECT
m1.*
FROM
HD_INVC_LN_ITEM_STAT m1
LEFT OUTER JOIN HD_INVC_LN_ITEM_STAT m2
ON (
m1.SHPMNT_LN_ITEM_KEY = m2.SHPMNT_LN_ITEM_KEY
AND m1.RPT_PER_KEY = m2.RPT_PER_KEY
AND m1.INVC_LN_ITEM_STAT_START_DT < m2.INVC_LN_ITEM_STAT_START_DT)
WHERE
m2.SHPMNT_LN_ITEM_KEY IS NULL
ORDER BY
m1.SHPMNT_LN_ITEM_KEY
,m1.RPT_PER_KEY
,m1.INVC_LN_ITEM_STAT_CD
,m1.INVC_LN_ITEM_STAT_START_DT
How about this?
SELECT
HD_INVC_LN_ITEM_STAT1.*
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT1
INNER JOIN
(
SELECT
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
,MAX(HD_INVC_LN_ITEM_STAT2.INVC_LN_ITEM_STAT_START_DT) AS MAX_INVC_LN_ITEM_STAT_START_DT
FROM
HD_INVC_LN_ITEM_STAT HD_INVC_LN_ITEM_STAT2
GROUP BY
HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
) HD_INVC_LN_ITEM_STAT2
ON
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY = HD_INVC_LN_ITEM_STAT2.SHPMNT_LN_ITEM_KEY
AND HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
= HD_INVC_LN_ITEM_STAT2.RPT_PER_KEY
AND HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT = HD_INVC_LN_ITEM_STAT2.MAX_INVC_LN_ITEM_STAT_START_DT
ORDER BY
HD_INVC_LN_ITEM_STAT1.SHPMNT_LN_ITEM_KEY
,HD_INVC_LN_ITEM_STAT1.RPT_PER_KEY
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_CD
,HD_INVC_LN_ITEM_STAT1.INVC_LN_ITEM_STAT_START_DT;
It's longer but arguably more intuitive. I would like other people's opinions on which is more efficient.

record types that weren't found for a specific value in oracle query

I have this query
Select distinct p_id, p_date,p_city
from p_master
where p_a_id in(1,2,5,8,2,1,10,02)
and my IN clause contains 200 values. How do I get to know which ones weren't returned by the query. Each value in the IN clause may have a record in some cases they don't. I want to know all the records that weren't found for any selected p_a_id type.
Please help
This will do the trick but I'm sure there's an easier way to find this out :-)
with test1 as
(select '1,2,5,8,2,1,10,02' str from dual)
select * from (
select trim(x.column_value.extract('e/text()')) cols
from test1 t, table (xmlsequence(xmltype('<e><e>' || replace(t.str,',','</e><e>')|| '</e></e>').extract('e/e'))) x) cols
left outer join
(Select count(*), p_a_id from p_master where p_a_id in (1,2,5,8,2,1,10,02) group by p_a_id) p
on p.p_a_id = cols.cols
where p_a_id is null
;

dbo.StoredProcedure SQL Syntax error near 'SELECT' and near 'AS'

Hey guys I hope you can help me I've tried every thing I can think of and it keeps telling me that my syntax near SELECT and that my syntax near AS is incorrect
CREATE PROCEDURE dbo.StoredProcedure2
SELECT
, Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
ORDER BY Message.CreateDate DESC;
It should be:
CREATE PROCEDURE dbo.StoredProcedure2
AS -- you were missing this
SELECT -- you had an extra comma here
Announcements.ID
, Announcement.CreateDate
, Announcements.Announcement
,aspnet_Users.UserName
,(SELECT Announcement_Read_State.Read_Date
FROM Announcement_Read_State
WHERE Announcement_Read_State.Announcement_ID = Announcements.ID
AND Announcement_Read_State.User_ID = 2) AS ReadState
FROM Announcements INNER JOIN aspnet_User ON Announcements .Sender_User_ID = aspnet_User.UserName
WHERE (Announcements.ID IN
( SELECT Max(Announcements.ID)
FROM Thread_Participant INNER JOIN Announcements ON
Thread_Participant.ThreadId = Announcements.Announcement_ThreadId
WHERE MessageThreadParticipant.UserID = 2
GROUP BY ThreadParticipant.AnnouncementThreadId
)
)-- you were missing this one
ORDER BY Message.CreateDate DESC;

Resources