SQL - Avoiding repetitive columns - sql

This is my code where I want to display the A.SITES_CODE and A.SITES_NAME just once where they are exactly the same. Here is my code where I tried the HAVING function but it does not meat my criteria.
SELECT A.SITES_CODE, A.SITES_NAME, D.DESCRIPTION
FROM ZZZ_SITES_AVAILABLE A, ZZZ_DIFFICULTY_RATINGS D, ZZZ_SITES_VISITED V, ZZZ_HOLIDAY_DETAILS H
WHERE A.SITES_CODE = V.SITES_CODE
AND V.CODE_OF_HOLIDAY = H.CODE_OF_HOLIDAY
AND H.DIFFICULTY_RATING = D.HOLIDAY_DIFFICULTY
AND LENGTH(D.DESCRIPTION) > 6
GROUP BY A.SITES_CODE, A.SITES_NAME, D.DESCRIPTION
HAVING COUNT(*) >= 1
ORDER BY A.SITES_CODE;

You need to remove description from the group by. Along the way, you should also learn to use proper ANSI standard JOIN syntax:
SELECT A.SITES_CODE, A.SITES_NAME
FROM ZZZ_SITES_AVAILABLE A JOIN
ZZZ_SITES_VISITED V
ON A.SITES_CODE = V.SITES_CODE JOIN
ZZZ_HOLIDAY_DETAILS H
ON V.CODE_OF_HOLIDAY = H.CODE_OF_HOLIDAY JOIN
ZZZ_DIFFICULTY_RATINGS D
ON H.DIFFICULTY_RATING = D.HOLIDAY_DIFFICULTY
WHERE LENGTH(D.DESCRIPTION) > 6
GROUP BY A.SITES_CODE, A.SITES_NAME
HAVING COUNT(*) >= 1
ORDER BY A.SITES_CODE;

Related

How can I differentate sum of a count for different merchant

This is query which I tried:
select distinct
MerchantOfferMerchantID, c.PsmHaendlerName as Merchant_name,
MerchantOfferUpdateDateUTC,
t.MerchantOfferPropertyColumnName,
s.MerchantPdlMappingColumnMerchant as column_mapped,
sum(count(t.MerchantOfferPropertyValueBigint)) over() as SUM_GENDER
from
OFFERPART2.TBLMERCHANTOFFER k
join
OfferPart2.TBLMERCHANTOFFERPROPERTY t on k.MerchantOfferIdentHash = t.MerchantOfferPropertyMerchantOfferIdentHash
join
copy.tblPsmHaendler as c on k.MerchantOfferMerchantID = c.PsmHaendlerID
join
copy.tblMerchantPdlSpecification q on k.MerchantOfferMerchantID = q.MerchantPdlSpecificationMerchantID
join
copy.tblMerchantPdlMapping s on q.MerchantPdlSpecificationID = s.MerchantPdlMappingMerchantPdlSpecificationID
where
MerchantOfferPropertyColumnName like '%gender%'
and MerchantPdlMappingColumn like'%Gender%'
and MerchantOfferUpdateDateUTC > getdate()-0.5
group by
k.MerchantOfferUpdateDateUTC, t.MerchantOfferPropertyColumnName,
MerchantOfferMerchantID, c.PsmHaendlerName,
s.MerchantPdlMappingColumnMerchant
order by
MerchantOfferUpdateDateUTC desc
Result:
But I want sum_gender of different merchant_id
Try this :
SELECT k.MerchantOfferMerchantID,sum(count(t.MerchantOfferPropertyValueBigint)) over() AS SUM_GENDER
FROM OFFERPART2.TBLMERCHANTOFFER k
JOIN OfferPart2.TBLMERCHANTOFFERPROPERTY t ON k.MerchantOfferIdentHash = t.MerchantOfferPropertyMerchantOfferIdentHash
JOIN copy.tblPsmHaendler AS c ON k. MerchantOfferMerchantID = c.PsmHaendlerID
JOIN copy.tblMerchantPdlSpecification q ON k.MerchantOfferMerchantID = q.MerchantPdlSpecificationMerchantID
JOIN copy.tblMerchantPdlMapping s ON q.MerchantPdlSpecificationID = s.MerchantPdlMappingMerchantPdlSpecificationID
WHERE MerchantOfferPropertyColumnName LIKE '%gender%'
AND MerchantPdlMappingColumn LIKE'%Gender%'
AND MerchantOfferUpdateDateUTC > getdate()-0.5
GROUP BY
k.MerchantOfferMerchantID
ORDER BY k.MerchantOfferMerchantID DESC

Getting duplicate value in sql

Query is below:
select
C.ngo_seq_no, B.member_name, B.member_pan_no, B.member_aadhar_no,
C.dept_name, C.source, C.fin_year, C.amt_sanctioned, C.comment,
R.member_designation_desc
from
member_designation R, ngo_member_detail B
inner join
ngo_source_fund C on B.ngo_seq_no = C.ngo_seq_no
where
mem_designation = member_designation_id::text
and C.ngo_seq_no = '15' ;
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Sanjay";"43537459874";"346555378895";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Ajay ";"587592894832";"646738972456";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"
15;"Rahul";"4353745983";"346732856575";"Nic";"2";"29922";"Nothing"

Get the sum of a count column in SQL

I have the following query
SELECT
dtc.coupon_type_company_name,
COUNT(*) * dtc.coupon_type_company_coupon_amount AS 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM
[dbo].[coupon_type_Company_User] dtcu
JOIN
coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN
person p ON dtcu.userID = p.userID
WHERE
coupon_type_company_coupon_is_combinable = 1
OR coupon_type_company_has_coupon = 1
AND dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
This returns the following:
What I want to have however is just one column and one row that should take the SUM of my middle column (count(*)*dtc.coupon_type_company_coupon_amount).
How could I achieve this and prevent doing this in my code backend (C#)
You can wrap your query like this:
SELECT SUM(Total_Coupon_To_Be_Used) AS the_sum
FROM (
your query
) s
Use a "Table Expression", as in:
select sum(Total_Coupon_To_Be_Used) from (
SELECT dtc.coupon_type_company_name,
count(*) * dtc.coupon_type_company_coupon_amount as 'Total_Coupon_To_Be_Used',
dtc.coupon_type_company_coupon_months_combinable
FROM [dbo].[coupon_type_Company_User] dtcu
JOIN coupon_type_Company dtc ON dtcu.coupon_type_Company_ID = dtc.id
JOIN person p ON dtcu.userID = p.userID
WHERE coupon_type_company_coupon_is_combinable = 1
or coupon_type_company_has_coupon = 1
and dtc.companyID = 1081
AND p.is_active = 1
GROUP BY
dtc.coupon_type_company_name,dtc.coupon_type_company_coupon_amount,
dtc.coupon_type_company_coupon_months_combinable
) x

Oracle SQL - using the coalesce function

I really can't get my head around the coalesce function ... or if this is even the best way to get the result I'm trying to achieve.
I have three dates in the following script (iv.dated, iv1.dated, dh.actshpdate). When I run the following script the dates are in separate columns (as expected);
select unique li.catnr, li.av_part_no, li.artist||' / '||li.title description, li.cust_catnr pallet_ref,
trunc(iv.dated), trunc(iv1.dated), trunc(dh.actshpdate)
from leos_item li
left join invtran_view_oes iv
on li.av_part_no = iv.part_no
and (iv.transaction = 'NREC' and iv.location_no = ' RETURNS W')
left join invtran_view_oes iv1
on li.av_part_no = iv1.part_no
and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
left join oes_delsegview od
on od.catnr = li.catnr
and od.prodtyp = li.prodtyp
and od.packtyp = li.packtyp
left join oes_dpos dp
on od.ordnr = dp.ordnr
and od.posnr = dp.posnr
and od.segnr = dp.segnr
left join oes_dhead dh
on dp.dheadnr = dh.dheadnr
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'
What I would like to achieve is one column with all dates in date order.
I tried replacing my dates with ...
trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) transaction_date
... but, I lose some of the dates;
How can I achieve the following result?
You could use UNION in the following way -
WITH DATA AS(
<your query goes here>
)
SELECT A, b, c, d, e FROM DATA
UNION
SELECT A,b,c,d,f FROM DATA
UNION
SELECT A,b,c,d,g FROM DATA
where a, b, c, d, e, f, g are the column alias of the select list in your original query. You can give your own column alias in the UNION query.

Adding column contents in a SQL UNION query

So far I have this query
SELECT
COUNT(f.code_id) as item_count,
f.code_desc
FROM
foo f
INNER JOIN foohistory fh ON f.history_id = fh.history_id
WHERE
MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY
f.code_desc
UNION ALL
SELECT
COUNT(b.code_id) as item_count,
b.code_desc
FROM
bar b
INNER JOIN barhistory bh ON b.history_id = bh.history_id
WHERE
MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY
b.code_desc
My goal is to UNION these two queries add SUM the 'item_count' columns foreach code_desc. Is this possible?
Without more information about the codes, like if it's possible that codes are mutually exclusive between the two tables, use:
SELECT x.code_desc,
SUM(x.item_count)
FROM (SELECT f.code_desc,
COUNT(f.code_id) as item_count
FROM foo f
JOIN foohistory fh ON f.history_id = fh.history_id
WHERE MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY f.code_desc
UNION ALL
SELECT b.code_desc,
COUNT(b.code_id) as item_count
FROM bar b
JOIN barhistory bh ON b.history_id = bh.history_id
WHERE MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY b.code_desc) x
GROUP BY x.code_desc
Yeah, doing something like this
SELECT Sum(unionedTable.item_count)
FROM
(
//your query
) as unionedTable