I'm getting the following error messages:
Msg 8120, Level 16, State 1, Line 1
Column 'customers.member_category' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 104, Level 16, State 1, Line 1
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
How can I order the results by the case?
Thanks
SELECT
Date,
order_id,
member_category,
COUNT(*) AS no_items,
SUM(item_amount) AS total_amount
FROM
order_items OI (NOLOCK) JOIN customers C (NOLOCK)
ON OI.CUSTOMER_NO = C.CUSTOMER_NO
WHERE
DATE = '01 FEB 2014'
GROUP BY
order_id,
member_category
UNION
SELECT
'',
'',
'Total',
COUNT(*) AS no_items,
SUM(item_amount) AS total_amount
FROM
order_items OI (NOLOCK) JOIN customers C (NOLOCK)
ON OI.CUSTOMER_NO = C.CUSTOMER_NO
WHERE
DATE = '01 FEB 2014'
GROUP BY Date WITH ROLLUP
ORDER BY Date ASC, CASE member_category WHEN 'VIP' THEN 1
WHEN 'STD' THEN 2
WHEN 'GLD' THEN 3
END
The second part of your UNION statement does not contain field member_category. This violates basic principle of UNION which is that number and order of columns in each statement should be same.
To resolve the issue, you need to add member_category both in the list of retrieved columns and in the GROUP BY clause.
Related
I'm trying to merge two query results in to one:
Query 1 and the reulsts:
SELECT MONTHNAME(tblFeesPaid.Pay_Date) AS 'Month',
SUM(tblFeesPaid.Fees_Paid) As 'Total Fees'
FROM tblFeesPaid
INNER JOIN tblFeesStructure ON tblFeesPaid.FID=tblFeesStructure.ID
WHERE Year(tblFeesPaid.Pay_Date)=2022
GROUP BY month(tblFeesPaid.Pay_Date);
Results
Month Total Fees
January 162000.00
February 69000.00
March 146926.00
Query 2 and results
SELECT MONTHNAME(tblTransFeesPaid.Pay_Date) AS 'Month',
SUM(tblTransFeesPaid.TransFee_Paid) As 'Transport Fees'
FROM tblTransFeesPaid
INNER JOIN tbltransfeesstructure ON tblTransFeesPaid.TransFID=tbltransfeesstructure.ID
WHERE Year(tblTransFeesPaid.Pay_Date)=2022
GROUP BY month(tblTransFeesPaid.Pay_Date);
Results
Month Transport Fees
March 7000.00
Could someone help me with the correct syntax that I'll achieve results as follows:
Expected results:
Month Total Fees Transport Fees
January 162000.00
February 69000.00
March 146926.00 7000.00
SELECT Z.Month, sum(Z.TotalFees) As 'Total Fees', sum(Z.TransportFees) As 'Transport Fees'
FROM
(
SELECT MONTHNAME(tblFeesPaid.Pay_Date) AS 'Month',
SUM(tblFeesPaid.Fees_Paid) As 'TotalFees',
0 As 'TransportFees'
FROM tblFeesPaid
INNER JOIN tblFeesStructure ON tblFeesPaid.FID=tblFeesStructure.ID
WHERE Year(tblFeesPaid.Pay_Date)=2022
GROUP BY month(tblFeesPaid.Pay_Date)
UNION
SELECT MONTHNAME(tblTransFeesPaid.Pay_Date) AS 'Month',
0 As 'TotalFees',
SUM(tblTransFeesPaid.TransFee_Paid) As 'TransportFees'
FROM tblTransFeesPaid
INNER JOIN tbltransfeesstructure ON tblTransFeesPaid.TransFID=tbltransfeesstructure.ID
WHERE Year(tblTransFeesPaid.Pay_Date)=2022
GROUP BY month(tblTransFeesPaid.Pay_Date)) Z
GROUP BY Z.Month;
First, you make sure both queries return same number of columns (add 0 as Transport Fees to first Query, and add 0 as Total Fees in second query);
Then, you UNION the queries, having one big resultset.
After that it's group by Month, and get MAX(Total Fees) and MAX(Transport Fees).
Would something like that work?
i.e.
SELECT Month, MAX(`Total Fees`), MAX(`Transport Fees`) FROM (
(
SELECT MONTHNAME(tblFeesPaid.Pay_Date) AS 'Month',
SUM(tblFeesPaid.Fees_Paid) As 'Total Fees',
0 AS 'Transport Fees'
FROM tblFeesPaid
INNER JOIN tblFeesStructure ON tblFeesPaid.FID=tblFeesStructure.ID
WHERE Year(tblFeesPaid.Pay_Date)=2022
GROUP BY month(tblFeesPaid.Pay_Date)
) t1
UNION
(
SELECT MONTHNAME(tblTransFeesPaid.Pay_Date) AS 'Month',
0 AS 'Total Fees',
SUM(tblTransFeesPaid.TransFee_Paid) As 'Transport Fees'
FROM tblTransFeesPaid
INNER JOIN tbltransfeesstructure ON tblTransFeesPaid.TransFID=tbltransfeesstructure.ID
WHERE Year(tblTransFeesPaid.Pay_Date)=2022
GROUP BY month(tblTransFeesPaid.Pay_Date)
) t2
) t3
GROUP BY Month;
Is it possible to order when the data comes from many select and union it together? Such as
In this statement, the vouchers data is not showing in the same sequence as I saved on the database, I also tried it with "ORDER BY v_payments.payment_id ASC" but won't be worked
( SELECT order_id as id, order_date as date, ... , time FROM orders WHERE client_code = '$searchId' AND order_status = 1 AND order_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
UNION
( SELECT vouchers.voucher_id as id, vouchers.payment_date as date, v_payments.account_name as name, ac_balance as oldBalance, v_payments.debit as debitAmount, v_payments.description as descriptions,
vouchers.v_no as v_no, vouchers.v_type as v_type, v_payments.credit as creditAmount, time, zero as tax, zero as freightAmount FROM vouchers INNER JOIN v_payments
ON vouchers.voucher_id = v_payments.voucher_id WHERE v_payments.client_code = '$searchId' AND voucher_status = 1 AND vouchers.payment_date BETWEEN '$start_date' AND '$end_date' ORDER BY v_payments.payment_id ASC , time )
UNION
( SELECT return_id as id, return_date as date, ... , time FROM w_return WHERE client_code = '$searchId' AND w_return_status = 1 AND return_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
Wrap the sub-select queries in the union within a SELECT
SELECT id, name
FROM
(
SELECT id, name FROM fruits
UNION
SELECT id, name FROM vegetables
)
foods
ORDER BY name
If you want the order to only apply to one of the sub-selects, use parentheses as you are doing.
Note that depending on your DB, the syntax may differ here. And if that's the case, you may get better help by specifying what DB server (MySQL, SQL Server, etc.) you are using and any error messages that result.
You need to put the ORDER BY at the end of the statement i.e. you are ordering the final resultset after union-ing the 3 intermediate resultsets
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. See link below:
ORDER BY and LIMIT in Unions
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
I've managed to separate the negatives & positives in the sql select below and I am trying to get the subtotals & totals as indicated in the table pic belom the third column is what the correct results should be.
Below is sql select I've used but its returning the results in column 2 of the above table and its not correct. How can I get the subtotal & total results in column 3
Thanks & Regards
SELECT * FROM
(SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) < 0
UNION ALL
SELECT
party AS "Account",
sum(debit-credit) AS "Balance"
FROM `tabGL Entry`
WHERE party_type ="Customer" AND MONTH(posting_date) = MONTH(Now()) -1
GROUP BY party WITH ROLLUP
HAVING sum(debit-credit) > 0
) rec
UNION ALL
SELECT NULL AS party, sum(dep) AS Balance
FROM (
SELECT SUM(credit-debit) AS dep
FROM `tabGL Entry`
UNION ALL
SELECT SUM(credit-debit) AS nodep
FROM `tabGL Entry`
) AS recb
GROUP BY party WITH ROLLUP
Use this syntax to prevent GROUP BY from being attached incorrectly:
( SELECT ... GROUP BY ... )
UNION ALL
( SELECT ... GROUP BY ... )
Versus
( SELECT ... )
UNION ALL
( SELECT ... )
GROUP BY ...
Where did the word 'Subtotal' come from? I don't see it in the query.
I suspect the inner WITH ROLLUP is tossed. What happens if you remove them?
Here's the report:
This is how I got the percentages for column the '%Change of most recent year".
=((Last(Fields!Quantity.Value,"Child") - First(Fields!Quantity.Value)) / First(Fields!Quantity.Value))`
= ((54675 - 55968)/55968 ) = -2.31%'
= ((54675 - 57849)/57849) = -5.49%'
It will always take the first year '2012' in this case and get the percentages against each other year. If I enter the years 2005,2004,2003,2002,2001 it will always take the first year and do a percentages against each additional year. 2005 to 2004, 2005 to 2003, 2005 to 2002 and so on. I can have as many as 2 column (year) to many columns.
I need to do it for the Total and Subtotal but it won't work because it's in a different scope.
data is = row Child group
Sub Total: = row Parent group
Total: = row Total group
Year = Column Period group
Query use to get result.
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
FROM vf_Sshot AS vfs
INNER JOIN vProd AS vP ON vfs.PRODUCT_CODE = vP.PRODUCT_CODE
INNER JOIN vMem_Type vMT on vMT.Member_Type = vfs.Member_Type
WHERE (PERIOD IN ( (SELECT Val from dbo.fn_String_To_Table(#Periods,',',1))))
AND (vMT.MEMBER_TYPE NOT IN ('a','b','c'))
AND (vfs.STATUS IN ( 'A', 'D', 'C'))
AND (MEMBERSHIP_CODE NOT IN ('ABC', 'DEF' ))
and vP.PROD_TYPE in ('DUE','MC','SC')
and vMT.Member_Record = '1'
GROUP BY MEMBERSHIP_CODE, PERIOD
Any ideas?
How would I produce this output?
TOTAL: 57,573 58,941 57,573 61,188 57,573 61,175 57,175
This is the easiest way of solving your problem. In your query, identify the sum for the latest period on a separate column (you can transform your query into a CTE, so that you don't have to change your base query a lot):
WITH query AS (
SELECT MEMBERSHIP_CODE
, PERIOD, COUNT(DISTINCT ID) AS Distinct_ID
, SUM(QUANTITY) AS Quantity
, '01-Personal' AS Child
, '01-Overall' AS Parent
, 'Total' as Total
...
UNION
SELECT
...
)
SELECT
A.MEMBERSHIP_CODE,
A.PERIOD,
A.Distinct_ID,
A.Child,
A.Parent,
A.Total,
A.Quantity,
B.Quantity AS LastPeriodQuantity
FROM
query A INNER JOIN
(SELECT *, ROW_NUMBER() OVER(PARTITION BY MEMBERSHIP_CODE, Distinct_ID, Child, Parent ORDER BY PERIOD DESC) as periodOrder FROM query) B ON
A.MEMBERSHIP_CODE = B.MEMBERSHIP_CODE AND
A.DISTINCT_ID = B.DISTINCT_ID AND
A.Parent = B.Parent AND
A.Child = B.Child AND
A.Total = B.Total AND
B.PeriodOrder = 1
And then on all your totals/subtotals/columns you will be accessing a column that is being grouped/filtered by the same rules than your denominator. Your expression can remain, for all cells, something like this:
=(Fields!LastPeriodQuantity.Value - Fields!Quantity.Value) / Fields!Quantity.Value
I have a column inside my table: tbl_customers that distinguishes a customer record as either a LEAD or a CUS.
The column is simply: recordtype, with is a char(1). I populate it with either C, or L.
Obviously C = customer, while L = lead.
I want to run a query that groups by the day the record was created, so I have a column called: datecreated.
Here's where I get confused with the grouping.
I want to display a result (in one query) the COUNT of customers and the COUNT of leads for a particular day, or date range. I'm successful with only pulling the number for either recordtype:C or recordtype:L , but that takes 2 queries.
Here's what I have so far:
SELECT COUNT(customerid) AS `count`, datecreated
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
AND `recordtype` = 'C'
GROUP BY `datecreated` ASC
As expected, this displays 2 columns (the count of customer records and the datecreated).
Is there a way to display both in one query, while still grouping by the datecreated column?
You can do a group by with over multiple columns.
SELECT COUNT(customerid) AS `count`, datecreated, `recordtype`
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
GROUP BY `datecreated` ASC, `recordtype`
SELECT COUNT(customerid) AS `count`,
datecreated,
SUM(`recordtype` = 'C') AS CountOfC,
SUM(`recordtype` = 'L') AS CountOfL
FROM `tbl_customers`
WHERE `datecreated` BETWEEN '$startdate."' AND '".$enddate."'
GROUP BY `datecreated` ASC
See Is it possible to count two columns in the same query
There are two solutions, depending on whether you want the two counts in separate rows or in separate columns.
In separate rows:
SELECT datecreated, recordtype, COUNT(*)
FROM tbl_customers
WHERE datecreated BETWEEN '...' AND '...'
GROUP BY datecreated, recordtype
In separate colums (this is called pivoting the table)
SELECT datecreated,
SUM(recordtype = 'C') AS count_customers,
SUM(recordtype = 'L') AS count_leads
FROM tbl_customers
WHERE datecreated BETWEEN '...' AND '...'
GROUP BY datecreated
Use:
$query = sprintf("SELECT COUNT(c.customerid) AS count,
c.datecreated,
SUM(CASE WHEN c.recordtype = 'C' THEN 1 ELSE 0 END) AS CountOfC,
SUM(CASE WHEN c.recordtype = 'L' THEN 1 ELSE 0 END) AS CountOfL
FROM tbl_customers c
WHERE c.datecreated BETWEEN STR_TO_DATE('%s', '%Y-%m-%d %H:%i')
AND STR_TO_DATE('%s', '%Y-%m-%d %H:%i')
GROUP BY c.datecreated",
$startdate, $enddate);
You need to fill out the date format - see STR_TO_DATE for details.