Hi I am trying to use group by rollup to get a grand total for a column but when I do it is duplicating the rows. What am I doing wrong? Thanks.
Code:
WITH TOTS AS
(
select
swkl.prn_cln_n,
swkl.swkl_bgn_dt,
swkl.swkl_end_dt,
swkl.sec_id,
actbl.*
from actbl
join actblc on actblc.actbl_seq_id = actbl.actbl_seq_id
join swkl on swkl.swkl_id = actblc.swkl_id
where swkl.prn_cln_n = '242931'
and swkl.stlm_rcd_sta_cd = 'A'
and trunc(swkl.stlm_rcd_mntd_ts) = to_date('03/10/2021','mm/dd/yyyy') --'14-JUN-2021'
and actblc.actblc_wkfl_pcs_cd = 'COM'
and actblc.stlm_rcd_sta_cd = 'A'
)
,F2 AS
(
SELECT PRN_CLN_N AS CLIENT_NUMBER
,LINE_TYPE_HM
,PR_TYP_C
,SUM(BIIL_A) AS BILL_AMOUNT
FROM TOTS
GROUP BY PRN_CLN_N
,LINE_TYPE_HM
,PR_TYP_C
)
SELECT coalesce(CLIENT_NUMBER,'TOTAL') AS CLIENT_NUMBER
,LINE_TYPE_HM
,PR_TYP_C
,SUM(BILL_AMOUNT) BILL_AMOUNT
FROM F2
group by rollup (CLIENT_NUMBER
,LINE_TYPE_HM
,PR_TYP_C)
There's nothing wrong, as far as I can tell.
When using rollup, you specify columns in it - client_number, line_type_hm, pr_typ_c which is 3 column. rollup will produce 3 + 1 = 4 levels of subtotals. Those subtotals are visually identified by having NULL values in rollup columns. In your screenshot,
1st level are all "fully populated" lines (those that have all columns with some values, i.e. the 1st line, 3rd, 5th, ...)
2nd level are lines whose pr_typ_c is NULL (2nd line, 4th, 6th, ...)
3rd level is the penultimate row (has both line_type_hm and pr_typ_c empty)
4th level is "grand total", the last line with TOTAL in client_number column
I don't have your tables nor data so it is you who might know what to do next because you can reduce number of subtotals by performing partial rollup. How? For example,
group by client_number, rollup(line_type_hm, pr_typ_c)
or any other combination of rollup columns.
Try it and see what happens.
Related
I need to get a state level count on number of services. For the purposes of this I only have two services. The first column is the states, the second column is the first services and the third column is the second service. What I am struggling with is to have the second and third column show up on the results in one query. Here is my code:
SELECT Distinct allstates.Name, count (data.StateName) as CareCase_Management_Services, count(data.StateName) Caregiver_Support_Services
From
(select distinct Name from USstate) allstates
Left Join
Client2017 data
on
allstates.Name = data.StateName and
data.FiscalYear = 2017 and
data.SrvstartCareCaseMgmtCode NOT IN('999','', '998') and
data.SrvstartCaregiverSuppCode NOT IN('999','', '998')
GROUP BY allstates.Name
ORDER BY allstates.Name ASC
I understand that you are looking to compute, for each state, the count of services that match certain criteria. There are two types of services, stored in two different columns.
If so, your query could be simplified using conditional aggregation :
SELECT
allstates.Name,
SUM(CASE WHEN c.SrvstartCareCaseMgmtCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) CareCase_Management_Services,
SUM(CASE WHEN c.SrvstartCaregiverSuppCode NOT IN ('999', '', '998') THEN 1 ELSE 0 END) Caregiver_Support_Services
FROM
(SELECT DISTINCT Name FROM USstate) s
LEFT JOIN Client2017 c ON s.Name = c.StateName AND c.FiscalYear = 2017
GROUP BY allstates.Name
With this technique, each service is counted according to its own logic ; when conditions are met, the record is counted in (1 is added to the SUM()), else it is ignored (+ 0).
NB : do you really have duplicated state names in USstate ? if no, you can replace subquery (SELECT DISTINCT Name FROM USstate) s with just USstate
I'm using access 2013 and trying to identify duplicate payments made to vendors. I use the SQL query below to identify different type of duplicates but it is not giving desired results as sometimes two criteria are different like invoice number and invoice date.
SELECT
Base.ID AS SerialNumber,
Base.CoCd AS CoCode,
Base.DocumentNo AS DocID,
Base.ClrngdocNo AS ClearingDoc,
Base.DocumentType AS DocType,
Base.Account AS VendorName,
Base.Reference AS InvoiceNumber,
Base.DocumentDate AS InvoiceDate,
Base.GrossInvoiceAmount AS InvAmount
FROM RawData2017TillDate AS Base
INNER JOIN RawData2017TillDate AS duplicate
ON (Base.ID <> duplicate.ID)
AND (Base.Account = duplicate.Account)
AND (Base.Reference <> duplicate.Reference)
AND (Base.DocumentDate = duplicate.DocumentDate)
AND (Base.GrossInvoiceAmount = duplicate.GrossInvoiceAmount)
ORDER BY Base.GrossInvoiceAmount DESC , Base.reference DESC;
I just want single query to identify duplicate with one or more characters added at the begining or at the end of invoice number like examples below
2713565
2713565R,
01456
1456,
I-0001118588
1118588
Also, if I could get a better query to identify duplicates based on other criteria will be appreciated. I am looking for a single query for all criteria.
Thanks in advance!
Please try like below:
I grouped invoice numbers based on their first 2 letters.
SELECT
Mid(Base.reference, 1, 2) , count(1)
FROM RawData2017TillDate AS Base
INNER JOIN RawData2017TillDate AS duplicate
ON (Base.ID <> duplicate.ID)
AND (Base.Account = duplicate.Account)
AND (Base.Reference <> duplicate.Reference)
AND (Base.DocumentDate = duplicate.DocumentDate)
AND (Base.GrossInvoiceAmount = duplicate.GrossInvoiceAmount)
group by Mid(Base.reference, 1, 2) ;
I have a dimension (page_type) where the names are not unique (so two keys can have the same name). Now I would like to see the clicks by page_type-name.
The following query unfortunately show the dimension names, but one line per key.
SELECT
{[Measures].[count_clicks]} ON COLUMNS,
[page_type].[page_type].members ON ROWS
FROM
[customer_journey]
The result:
category 150.000
product 100.000
category 80.000
...
How can I change this query, to get only one line per page_type?
category 230.000
product 100.000
...
This is slow but does this work:
with set SetOfPagesWithSameName as
filter
(
[page_type].[page_type].members as p,
p.current.name = [page_type].[page_type].currentmember.name
)
member Measures.TotalCountOFClicks as
sum(
existing SetOfPagesWithSameName,
[Measures].[count_clicks]
)
member Measures.CountSimilarPagesGrt1 as
IIF(SetOfPagesWithSameName.count > 0 , 1, null)
select
NonEmpty([page_type].[page_type].members, Measures.CountSimilarPagesGrt1) on 1,
Measures.TotalCountOFClicks on 0
from [customer_journey]
I am trying to be more specific on my query as you can see (link to image below) from the cupID column there is groups A to H 3 times. All I am trying to do is have 3 queries, first query to output all groups A-H only once, second query from the second and third from the third if that makes sense?
This is the query
SELECT cupID, date, matchno,
clan1,
clan2,
si
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
ORDER BY cupID ASC
which shows: (take a look at picture)
http://s13.postimg.org/6rufgywcn/image.png
so query 1/2/3 should output separately like (a,b,c,d etc) instead of 1 query showing multiples (aaa,bbb,ccc,ddd etc)
Many thanks for help
Based on the assumption that you are performing a union all on your 3 queries, please add a dummy column viz SortOrder and order by on it.
In the following sample query (SQL Server), I assumed all 3 queries as same, please do change them accordingly with the dummy sortorder:
-- 1st query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
1 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 2nd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
2 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
union all
-- 3rd query
SELECT cupID, date, matchno,
clan1,
clan2,
si,
3 as SortOrder -- dummy sort column
FROM ws_bi2_cup_matches
WHERE ladID='0'
AND matchno = '6'
AND TYPE = 'gs'
GROUP BY clan1
order by 7 -- dummy sort order column
Need help on a query using sql server 2005
I am having two tables
code
chargecode
chargeid
orgid
entry
chargeid
itemNo
rate
I need to list all the chargeids in entry table if it contains multiple entries having different chargeids
which got listed in code table having the same charge code.
data :
code
100,1,100
100,2,100
100,3,100
101,11,100
101,12,100
entry
1,x1,1
1,x2,2
2,x3,2
11,x4,1
11,x5,1
using the above data , it query should list chargeids 1 and 2 and not 11.
I got the way to know how many rows in entry satisfies the criteria, but m failing to get the chargeids
select count (distinct chargeId)
from entry where chargeid in (select chargeid from code where chargecode = (SELECT A.chargecode
from code as A join code as B
ON A.chargecode = B.chargeCode and A.chargetype = B.chargetype and A.orgId = B.orgId AND A.CHARGEID = b.CHARGEid
group by A.chargecode,A.orgid
having count(A.chargecode) > 1)
)
First off: I apologise for my completely inaccurate original answer.
The solution to your problem is a self-join. Self-joins are used when you want to select more than one row from the same table. In our case we want to select two charge IDs that have the same charge code:
SELECT DISTINCT c1.chargeid, c2.chargeid FROM code c1
JOIN code c2 ON c1.chargeid != c2.chargeid AND c1.chargecode = c2.chargecode
JOIN entry e1 ON e1.chargeid = c1.chargeid
JOIN entry e2 ON e2.chargeid = c2.chargeid
WHERE c1.chargeid < c2.chargeid
Explanation of this:
First we pick any two charge IDs from 'code'. The DISTINCT avoids duplicates. We make sure they're two different IDs and that they map to the same chargecode.
Then we join on 'entry' (twice) to make sure they both appear in the entry table.
This approach gives (for your example) the pairs (1,2) and (2,1). So we also insist on an ordering; this cuts to result set down to just (1,2), as you described.