I preface this by saying I am new to SQL and have been learning on the job thanks to Stack Overflow.
I am running a query from multiple tables (3 in total), and I am trying to get the results for each unique identifier on one row. 1 of the table items has multiple returns, and I am able to write it to where they show as a max command in their own column; however, it still returns multiple rows for the same identifier.
Here's what I have so far:
SELECT tbl.1.field as ID, tbl.2.field as Name, tbl.2.fieldb as Product,
COUNT(*) AS ConfirmedSales
MAX(CASE WHEN tbl.3.field = 'Product1' then 1 else 0 end) as CustomCol1
MAX(CASE WHEN tbl.3.field = 'Product2' then 1 else 0 end) as CustomCol2
FROM tbl.2
LEFT JOIN tbl.2 on tbl.2.x = tbl.1.x
INNER JOIN tbl.3 on tbl.2.x = tbl.3.x
WHERE ((tbl.1.date between '01/01/2014 00:00:00' and 06/30/2014 23:59:59'))
GROUP BY tbl.1.field, tbl.2.field, tbl.2.fieldb
Results return as follows:
Row |ID |Name |CustomCol1 |CustomCol2
1 |8048 |Jon Smith |1 |0
2 |8048 |Jon Smith |0 |1
3 |4044 |Max Williams |0 |0
I would like for the results for CustomCol1 and CustomCol2 to share the same line if the ID is the same. Is this possible?
Consider moving your cases into a subquery like so:
SELECT t.ID, t.name, MAX(s.custom1) AS custom1, MAX(custom2) AS custom2
FROM #tbl1 t
INNER JOIN (SELECT personID
,CASE WHEN s.product = 'product1' THEN 1 ELSE 0 END custom1
,CASE WHEN s.product = 'product2' THEN 1 ELSE 0 END custom2
FROM #sales s) s ON t.ID = s.personID
GROUP BY t.ID, t.Name
This will prevent the duplication you're seeing. If you're ultimate goal is something else, though, give us some more info, and I'm sure someone will have a good answer.
Related
SELECT
SELECT pp.id, TO_CHAR(pp.created_dt::date, 'dd.mm.yyyy') AS "Date", CAST(pp.created_dt AS time(0)) AS "Time",
au.username AS "User", ss.name AS "Service", pp.amount, REPLACE(pp.status, 'SUCCESS', ' ') AS "Status",
pp.account AS "Props", pp.external_id AS "External", COALESCE(pp.external_status, null, 'indefined') AS "External status"
FROM payment AS pp
INNER JOIN auth_user AS au ON au.id = pp.creator_id
INNER JOIN services_service AS ss ON ss.id = pp.service_id
WHERE pp.created_dt::date = (CURRENT_DATE - INTERVAL '1' day)::date
AND ss.name = 'Some Name' AND pp.status = 'SUCCESS'
id | Date | Time | Service |amount | Status |
------+-----------+-----------+------------+-------+--------+---
9 | 2021.11.1 | 12:20:01 | some serv | 100 | stat |
10 | 2021.12.1 | 12:20:01 | some serv | 89 | stat |
------+-----------+-----------+------------+-------+--------+-----
Total | | | | 189 | |
I have a SELECT like this. I need to get something like the one shown above. That is, I need to get the total of one column. I've tried a lot of things already, but nothing works out for me.
If I understand correctly you want a result where extra row with aggregated value is appended after result of original query. You can achieve it multiple ways:
1. (recommended) the simplest way is probably to union your original query with helper query:
with t(id,other_column1,other_column2,amount) as (values
(9,'some serv','stat',100),
(10,'some serv','stat',89)
)
select t.id::text, t.other_column1, t.other_column2, t.amount from t
union all
select 'Total', null, null, sum(amount) from t
2. you can also use group by rollup clause whose purpose is exactly this. Your case makes it harder since your query contains many columns uninvolved in aggregation. Hence it is better to compute aggregation aside and join unimportant data later:
with t(id,other_column1,other_column2,amount) as (values
(9,'some serv','stat',100),
(10,'some serv','stat',89)
)
select case when t.id is null then 'Total' else t.id::text end as id
, t.other_column1
, t.other_column2
, case when t.id is null then ext.sum else t.amount end as amount
from (
select t.id, sum(amount) as sum
from t
group by rollup(t.id)
) ext
left join t on ext.id = t.id
order by ext.id
3. For completeness I just show you what should be done to avoid join. In that case group by clause would have to use all columns except amount (to preserve original rows) plus the aggregation (to get the sum row) hence the grouping sets clause with 2 sets is handy. (The rollup clause is special case of grouping sets after all.) The obvious drawback is repeating case grouping... expression for each column uninvolved in aggregation.
with t(id,other_column1,other_column2,amount) as (values
(9,'some serv','stat',100),
(10,'some serv2','stat',89)
)
select case grouping(t.id) when 0 then t.id::text else 'Total' end as id
, case grouping(t.id) when 0 then t.other_column1 end as other_column1
, case grouping(t.id) when 0 then t.other_column2 end as other_column2
, sum(t.amount) as amount
from t
group by grouping sets((t.id, t.other_column1, t.other_column2), ())
order by t.id
See example (db fiddle):
(To be frank, I can hardly imagine any purpose other than plain reporting where a column mixes id of number type with label Total of text type.)
TABLE A: Pre-joined table - Holds a list of providers who belong to a group and the group the provider belongs to. Columns are something like this:
ProviderID (PK, FK) | ProviderName | GroupID | GroupName
1234 | LocalDoctor | 987 | LocalDoctorsUnited
5678 | Physican82 | 987 | LocalDoctorsUnited
9012 | Dentist13 | 153 | DentistryToday
0506 | EyeSpecial | 759 | OphtaSpecialist
TABLE B: Another pre-joined table, holds a list of providers and their demographic information. Columns as such:
ProviderID (PK,FK) | ProviderName | G_or_I | OtherColumnsThatArentInUse
1234 | LocalDoctor | G | Etc.
5678 | Physican82 | G | Etc.
9012 | Dentist13 | I | Etc.
0506 | EyeSpecial | I | Etc.
The expected result is something like this:
ProviderID | ProviderName | ProviderStatus | GroupCount
1234 | LocalDoctor | Group | 2
5678 | Physican82 | Group | 2
9012 | Dentist13 | Individual | N/A
0506 | EyeSpecial | Individual | N/A
The goal is to determine whether or not a provider belongs to a group or operates individually, by the G_or_I column. If the provider belongs to a group, I need to include an additional column that provides the count of total providers in that group.
The Group/Individual portion is relatively easy - I've done something like this:
SELECT DISTINCT
A.ProviderID,
A.ProviderName,
CASE
WHEN B.G_or_I = 'G'
THEN 'Group'
WHEN B.G_or_I = 'I'
THEN 'Individual' END AS ProviderStatus
FROM
TableA A
LEFT OUTER JOIN TableB B
ON A.ProviderID = B.ProviderID;
So far so good, this returns the expected results based on the G_or_I flag.
However, I can't seem to wrap my head around how to complete the COUNT portion. I feel like I may be overthinking it, and stuck in a loop of errors. Some things I've tried:
Add a second CASE STATEMENT:
CASE
WHEN B.G_or_I = 'G'
THEN (
SELECT CountedGroups
FROM (
SELECT ProviderID, count(GroupID) AS CountedGroups
FROM TableA
WHERE A.ProviderID = B.ProviderID
GROUP BY ProviderID --originally had this as ORDER BY, but that was a mis-type on my part
)
)
ELSE 'N/A' END
This returns an error stating that a single row sub-query is returning more than one row. If I limit the number of rows returned to 1, the CountedGroups column returns 1 for every row. This makes me think that its not performing the count function as I expect it to.
I've also tried including a direct count of TableA as a factored sub-query:
WITH CountedGroups AS
( SELECT Provider ID, count(GroupID) As GroupSum
FROM TableA
GROUP BY ProviderID --originally had this as ORDER BY, but that was a mis-type on my part
) --This as a standalone query works just fine
SELECT DISTINCT
A.ProviderID,
A.ProviderName,
CASE
WHEN B.G_or_I = 'G'
THEN 'Group'
WHEN B.G_or_I = 'I'
THEN 'Individual' END AS ProviderStatus,
CASE
WHEN B.G_or_I = 'G'
THEN GroupSum
ELSE 'N/A' END
FROM
CountedGroups CG
JOIN TableA A
ON CG.ProviderID = A.ProviderID
LEFT OUTER JOIN TableB
ON A.ProviderID = B.ProviderID
This returns either null or completely incorrect column values
Other attempts have been a number of variations of this, with a mix of bad results or Oracle errors. As I mentioned above, I'm probably way overthinking it and the solution could be rather simple. Apologies if the information is confusing or I've not provided enough detail. The real tables have a lot of private medical information, and I tried to translate the essence of the issue as best I could.
Thank you.
You can use the CASE..WHEN and analytical function COUNT as follows:
SELECT
A.PROVIDERID,
A.PROVIDERNAME,
CASE
WHEN B.G_OR_I = 'G' THEN 'Group'
ELSE 'Individual'
END AS PROVIDERSTATUS,
CASE
WHEN B.G_OR_I = 'G' THEN TO_CHAR(COUNT(1) OVER(
PARTITION BY A.GROUPID
))
ELSE 'N/A'
END AS GROUPCOUNT
FROM
TABLE_A A
JOIN TABLE_B B ON A.PROVIDERID = B.PROVIDERID;
TO_CHAR is needed on COUNT as output expression must be of the same data type in CASE..WHEN
Your problem seems to be that you are missing a column. You need to add group name, otherwise you won't be able to differentiate rows for the same practitioner who works under multiple business entities (groups). This is probably why you have a DISTINCT on your query. Things looked like duplicates which weren't. Once you've done that, just use an analytic function to figure out the rest:
SELECT ta.providerid,
ta.providername,
DECODE(tb.g_or_i, 'G', 'Group', 'I', 'Individual') AS ProviderStatus,
ta.group_name,
CASE
WHEN tb.g_or_i = 'G' THEN COUNT(DISTINCT ta.provider_id) OVER (PARTITION BY ta.group_id)
ELSE 'N/A'
END AS GROUP_COUNT
FROM table_a ta
INNER JOIN table_b tb ON ta.providerid = tb.providerid
Is it possible that your LEFT JOIN was going the wrong direction? It makes more sense that your base demographic table would have all practitioners in it and then the Group table might be missing some records. For instance if the solo prac was operating under their own SSN and Type I NPI without applying for a separate Type II NPI or TIN.
I am working with the AdventureWorks2014 database and am using the following query.
select
SUM(Purchasing.PurchaseOrderDetail.OrderQty) as 'Total Quantity',
SUM(Purchasing.PurchaseOrderDetail.LineTotal) as 'Total Amount',
Purchasing.PurchaseOrderHeader.VendorID
from Purchasing.PurchaseOrderDetail
inner join Purchasing.PurchaseOrderHeader
on Purchasing.PurchaseOrderDetail.PurchaseOrderID = Purchasing.PurchaseOrderHeader.PurchaseOrderID
group by Purchasing.PurchaseOrderHeader.VendorID, DATEPART(year,Purchasing.PurchaseOrderHeader.OrderDate)
order by Purchasing.PurchaseOrderHeader.VendorID
This gives me the following output.
|------------------------------------|
|Total Quantity|Total Amount|VendorID|
|15 |694.1655 | 1492|
|288 |12370.239 | 1492|
|45 |1931.7375 | 1492|
|180 |7682.6295 | 1492|
|9350 |150404.1 | 1494|
|1650 |26541.9 | 1494|
|550 |8847.3 | 1494|
|16500 |265419 | 1494|
|------------------------------------|
From what i understand, this is each year's data, i.e,the values 2011,2012,2013 and 2014, for each vendor. Which is why each vendor is repeated 4 times.
I need to have each of these years as a separate column in the output as follows.
|--------------------------------------------------------------------------------|
|Total Quantity|Total Amount|VendorID|2011Amount|2012Amount|2013Amount|2014Amount|
|--------------------------------------------------------------------------------|
Any thoughts?
Pivot Method, make sure you first prepare the query how you want prior to pivoting.
;WITH cte AS (
SELECT
DATEPART(year,poh.OrderDate) as [Year]
,SUM(pod.OrderQty) OVER (PARTITION BY DATEPART(year,poh.OrderDate)) as TotalQuantity
,SUM(pod.LineTotal) OVER (PARTITION BY DATEPART(year,poh.OrderDate)) as TotalAmount
,pod.LineTotal as Amount
FROM
Purchasing.PurchaseOrderDetail pod
INNER JOIN Purchasing.PurchaseOrderHeader poh
ON pod.PurchaseOrderId = poh.PurchaseOrderId
)
SELECT *
FROm
cte
PIVOT (
SUM(Amount)
FOR [Year] IN ([2011],[2012],[2013],[2014])
) p
Conditional Aggregation Method
SELECT
SUM(pod.OrderQty) as TotalQuantity
,SUM(pod.LineTotal) as TotalAmount
,SUM(CASE WHEN DATEPART(year,poh.OrderDate) = 2011 THEN pod.LineTotal ELSE 0 END) as [2011Amount]
,SUM(CASE WHEN DATEPART(year,poh.OrderDate) = 2012 THEN pod.LineTotal ELSE 0 END) as [2012Amount]
,SUM(CASE WHEN DATEPART(year,poh.OrderDate) = 2013 THEN pod.LineTotal ELSE 0 END) as [2013Amount]
,SUM(CASE WHEN DATEPART(year,poh.OrderDate) = 2014 THEN pod.LineTotal ELSE 0 END) as [2014Amount]
FROM
Purchasing.PurchaseOrderDetail pod
INNER JOIN Purchasing.PurchaseOrderHeader poh
ON pod.PurchaseOrderId = poh.PurchaseOrderId
In this case I think I would go with the conditional aggregation method..... Please note I used Table Aliases to refer to the table rather than continuing to type the long names it is a good habit to get into.
This exact code is of course untested because you did not include test data and desired result but the techniques are the most standard way of doing this. Note when more than 1 column is involved in aggregation it is typically easiest to do conditional aggregation.
Having a Table Person
and a Table PersonRecord
I need to select only one record for each person, the record with the max status.
The status are ordered by C > B > A, a person can have multiple records with different or the same status, I need always select the greater status or the first (if the person have records with the same status).
I make the following query to get the rows ordered
select ep.personid, ep.persondesc, records.veryimportantcode, records.status
from extperson ep
left join
(
select rownum as rn, v.* from
(
select pr.personid, pr.veryimportantcode, pr.status
from personrecord pr
group by pr.personid, pr.veryimportantcode, pr.status
order by pr.personid,
decode(pr.status,
'C', 1,'B', 2,'A', 3,
4)
) v
) records
on ep.personid = records.personid
it give me:
I need
|PERSONID |PERSONDESC|VERYIMPORTANTCODE |STATUS |
|00325465 |Bjork |(null) |(null) |
|00527513 |Paul |ZP-2143540 |A |
|00542369 |Hazard |ZH-7531594 |C |
|0324567 |Jhon |ZJ-2346570 |B |
I try to achieve this using an aditional materialized subquery where I count the number of repetitions and make a left join with a where (subquerymat.nrorepeat > 1 and rownum = 1) or (subquerymat.nrorepeat = 1 or subquerymat.nrorepeat is null) but does not work.
There is one very important rule for this query, I would append this query in the right side of an union inside a view then I can't use stored procedures.
Try:
select personid, persondesc, veryimportantcode, status
from (select pe.personid,
pe.persondesc,
pr.veryimportantcode,
pr.status,
row_number() over(partition by pe.personid order by pr.status desc,
pr.autoid) as rn
from person pe
left join personrecord pr
on pe.personid = pr.personid)
where rn = 1
Fiddle test: http://sqlfiddle.com/#!4/25074/2/0
I'm looking for a sql instruction (Select) to create a table format from a existing table.
I have a table in this format.
Id|Record|Title |Value|
1. |1 |name |John |
2. |1 |ammount|200 |
3. |2 |name |Lisa |
4. |2 |ammount|400 |
However I need to show information in this format
Record|Name|Ammount|
1. |John| 200|
2. |Lisa| 400|
I don't want to create a new table, I'm looking for a select or join operation.
Do you know any instruction to perform this operation?
Thanks in advance.
SELECT record,
MAX(CASE WHEN Title = 'name' THEN Value END) AS Name,
MAX(CASE WHEN Title = 'ammount' THEN Value END) AS Ammount
FROM YourTable
GROUP BY record
SQLFIDDLE
You can try this (assuming it's MySQL):
SELECT
A.Record,
A.Value AS Name,
B.Value AS Ammount
FROM
tbl A
JOIN
tbl B
ON
A.Record = B.Record
WHERE
A.Title = 'name' AND
B.Title = 'ammount'
Basically you join the same table twice through Record value, filter the results by Title column and select required columns.
SQLfiddle example
SELECT tt.record,
(SELECT value FROM t WHERE tt.record = record AND title = 'name') AS name,
(SELECT value FROM t WHERE tt.record = record AND title = 'ammount') AS ammount
FROM t as tt
GROUP BY tt.record
;