I am struggling with the below SQL script relating to the bolded "10 desc" at the end of the script
This field is not in the original tables. It was created separately.
I don't exactly know what it is for but if I remove it it will give me different result. Hope I could have some guidance on this.
SELECT ce.[Agreement ID], m.AGREEMENT_NAME, ce.Max_Deduct_Bond, d.[Product Type], d.Margin, d.Ccy, Left(Right(d.[Margin],12),2) AS Cty, d.Posted_Received_Flag, d.[Market Value], (d.[Market Value]*r.[Rate]) AS [Market Value HKD]
FROM [Step 0103 - CE: CE Source] AS ce,
[201_0002 - Get_Agreement_Treats_Mapping] AS m,
[201_0000 - Get_nCOMS_201_Details] AS d,
SysReportDate AS sd,
SysTOR AS r
WHERE (((ce.[Agreement ID])=[m].[AGREEMENT_ID] And (ce.[Agreement ID])="11") AND ((m.AGREEMENT_NAME)=[d].[Agreement]) AND ((ce.Max_Deduct_Bond)<>0) AND ((d.[Product Type])='Bond') AND ((d.[Reporting Date])=[sd].[Reporting Date]) AND ((sd.Date_Code)='01_CM') AND ((r.[Ccy Code])=[d].[Ccy]))
ORDER BY ce.[Agreement ID], 10 DESC;
10 in order by mean its ordering by 10th column listed in the select statement, which in your query is [Market Value HKD].
which is not recommended to do so , and your order by statement changes depend on the order of columns returned in the select .
always use column names:
... ORDER BY ce.[Agreement ID], [Market Value HKD] DESC;
Related
I have two tables. One table is the main registered Boat table that holds most of the information. it has many columns one of them is [MFR CODE].
The other table is a reference table that contains all the records associated with only Business Boats and it has only 3 columns : [MFR CODE], [MFR NAME] & [MODEL]
I'm trying to find how many times (count) Business Boats appear in the Registered boat tables.
SELECT
Count(*) As 'TotalNumberBoats'
, [BoatsReg].[dbo].[MASTER].[MFR MDL CODE]
,[BoatsReg].[dbo].[BusinessBoats].[MFR]
,[BoatsReg].[dbo].[BusinessBoats].[MODEL]
FROM [BoatsReg].[dbo].[MASTER],[BoatsReg].[dbo].[BusinessBoats]
Where [BoatsReg].[dbo].[MASTER].[MFR MDL CODE] = [BoatsReg].[dbo].[BusinessBoats].[CODE]
group by [BoatsReg].[dbo].[BusinessBoats].[CODE]
order by TotalNumberBoats asc
How do i get rid of all the square brackets, it's annoying.
why do i get an error ?
Well when you aggregate a result, you have to specify all other fixed columns in the GROUP BY clause
(Answer edited to add total row with a UNION ALL and add a sort field to put the total row as last one)
In your case:
SELECT 1 as sorted,
,Count(*) As TotalNumberBoats
,MASTER.[MFR MDL CODE]
,BusinessBoats.MFR
,BusinessBoats.MODEL
FROM BoatsReg, BusinessBoats
Where MASTER.[MFR MDL CODE] = BusinessBoats.CODE
Group by
,MASTER.[MFR MDL CODE]
,BusinessBoats.MFR
,BusinessBoats.MODEL
UNION ALL
SELECT 2 as sorted,
,Count(*) As TotalNumberBoats
,'','',''
FROM BoatsReg
Order by sorted, TotalNumberBoats
I currently have a very simple SQL script, which provides the sell price for a special customer of ours, who gets Priceline "J". I'd like to add a column of Retail pricing as well for them to reference, which is normally Priceline "R". My query currently looks like this:
SELECT RS_PCL.SKU,
RS_PCL.SKU_DESC,
RS_PCL.PACKAGE,
RS_PCL.PRICE AS [Sell Price],
RS_PCL.STD AS [Start Date],
RS_PCL.END AS [End Date]
FROM RS_PCL
WHERE RS_PCL.PRICELINE = "J"
Would anyone be able to help me figure out how I could add the "R" price line as another column instead of separate rows?
I can add WHERE RS_PCL.PRICELINE IN ("J","R") but it would create a separate row for each Retail price, instead of in the column next to it. I've seen examples of a separate SELECT CASE WHEN, but not sure exactly how the syntax works.
The prices are always going to have the same start date, so it would just need to join where the SKU matches and the Start Date matches.
NOTE : IM sorry I accidentally edited your answer, not my question...
Revised: Running into errors with this code still, saying "Your query does not include the specified expression 'GL Type' as part of an aggregate function" or would say each field that isn't included in the group by clause
SELECT RS_PCL.[GL Type],
RS_PCL.SKU,
RS_PCL.[SKU Desc],
RS_PCL.Supplier,
RS_PCL.[Case UPC],
RS_PCL.[Pack UPC],
RS_PCL.[Unit UPC],
RS_PCL.PDCN,
RS_PCL.Package,
RS_PCL.[Price Start Date],
RS_PCL.[Price End Date],
Iif( RS_PCL.PRICELINE = "J" , RS_PCL.Price , Null) AS [Sell Price],
Iif( RS_PCL.PRICELINE = "R" , RS_PCL.Price , Null) AS [Retail Price],
RS_PCL.Cost,
RS_PCL.Tax,
RS_PCL.Freight
FROM RS_PCL
WHERE (RS_PCL.PRICELINE IN ("J","R"))
Tested the Self Join query listed below, I keep getting a "Syntax Error in FROM Clause" with this query.
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN SELECT (RS_PCL.SKU, RS_PCL.[Price Start Date], RS_PCL.Price AS R_PRICE FROM RS_PCL WHERE RS_PCL.PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.[Price Start Date] = R.[Price Start Date]
WHERE RS_PCL.Priceline = "J"
SELECT CASE does not work in Access query. Use IIf() or Switch() or Choose().
Perhaps a CROSSTAB query or emulate CROSSTAB with expressions to create fields. Presuming, as indicated in question title, there is one "J" and one "R" record for each data combination:
SELECT SKU, SKU_DESC, PACKAGE, STD, END,
Max(IIf(PRICELINE = "J", PRICE, Null)) AS J,
Max(IIf(PRICELINE = "R", PRICE, Null)) AS R
FROM RS_PCL
GROUP BY SKU, SKU_DESC, PACKAGE, STD, END
Or a self-join, assuming only fields needed as unique identifier are SKU and STD:
SELECT J.*, R.R_PRICE FROM RS_PCL AS J
INNER JOIN (SELECT SKU, STD, PRICE AS R_PRICE FROM RS_PCL WHERE PRICELINE = "R") AS R
ON J.SKU = R.SKU AND J.STD = R.STD
WHERE PRICELINE = "J";
This would be useful on a report but useless for a data entry form.
Yet another approach would use DLookup() domain aggregate function. Domain aggregate function can cause slow performance in query or form but the dataset would be editable.
I am trying to aggregate the Total Sum of [Award] and Count of [Counter] for different [PO Types]. Is there a way to group all the existing PO Types into one value "SRM" within the same query?
The end result I want is one record: SRM | $37,823,372.89 | 1732
Here is the current SQL:
SELECT tblSRMECC.[PO Type], Sum(tblSRMECC.Award) AS SumOfAward, Count(tblSRMECC.[Counter (PO)]) AS
[CountOfCounter (PO)]
FROM tblSRMECC
GROUP BY [PO Type]
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"));
One way of doing this is to amend your existing query to add a UNION that does the subtotal. Something like this as your SQL:
SELECT 0 AS DataSortOrder, tblSRMECC.[PO Type], Sum(tblSRMECC.Award) AS SumOfAward, Count(tblSRMECC.[Counter (PO)]) AS
[CountOfCounter (PO)]
FROM tblSRMECC
GROUP BY [PO Type]
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
UNION SELECT 1,"SRM",Sum(tblSRMECC.Award), Count(tblSRMECC.[Counter (PO)])
FROM tblSRMECC
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
ORDER BY 1,2
The extra column that I have added, DataSortOrder, is just to make sure that the total appears after the individual totals.
If you only want to show the overall total, then you can just use the part of the query that I added:
SELECT "SRM",Sum(tblSRMECC.Award), Count(tblSRMECC.[Counter (PO)])
FROM tblSRMECC
HAVING (((tblSRMECC.[PO Type])<>"AutoPO" And (tblSRMECC.[PO Type])<>"Fixed Price Catalog"))
Regards,
I have two queries I want to join so I can get the percentages from each department of completed work orders but not sure how to go about it. I know I want a join and a crosstab query so I can display the results in a report.
The first query calculates the numerator,
SELECT
Count(MaximoReport.WorkOrder) AS CountOfWorkOrder,
MaximoReport.[Assigned Owner Group]
FROM MaximoReport
WHERE (
((MaximoReport.WorkType) In ("PMINS","PMOR","PMPDM","PMREG","PMRT"))
AND ((MaximoReport.Status) Like "*COMP")
AND ((MaximoReport.[Target Start])>=DateAdd("h",-1,[Enter the start date])
AND (MaximoReport.[Target Start])<DateAdd("h",23,[Enter the end date]))
AND ((MaximoReport.ActualLaborHours)<>"00:00")
AND ((MaximoReport.ActualStartDate)>=DateAdd("h",-11.8,[Enter the start date])
AND (MaximoReport.ActualStartDate)<DateAdd("h",23,[Enter the end date]))
)
GROUP BY MaximoReport.[Assigned Owner Group];
While the second query calculates the denominator:
SELECT
Count(MaximoReport.WorkOrder) AS CountOfWorkOrder,
MaximoReport.[Assigned Owner Group]
FROM MaximoReport
WHERE (
((MaximoReport.WorkType) In ("PMINS","PMOR","PMPDM","PMREG","PMRT"))
AND ((MaximoReport.Status)<>"CAN")
AND ((MaximoReport.[Target Start])>=DateAdd("h",-11.8,[Enter the start date])
AND (MaximoReport.[Target Start])<DateAdd("h",23,[Enter the end date])))
GROUP BY MaximoReport.[Assigned Owner Group];
Please advise how I can join the two queries to get the percentages of the departments and then do a crosstab query.
If there is a better way of doing this please also let me know.
I manage a state wide application and use Tableau to create visualizations of data.
I have been tasked with creating a visualization that show how much time is passing between contact entries and today (Case Note Dates). I know how to isolate the max case note date in the case note table:
Select
[Case_Master_ID],
[Case_Note_Date],
[Case_Note_Category_Desc],
[Case_Note_Summary_Narr]
From
buCase_Note
Where
Case_Note_Date = (Select MAX(Case_Note_Date)
From buCase_Note)
This query will show me that max case notes in the table from today. The issue is I need to show the max case note for all participants, not just the ones from today. The original query I have been using to view case notes is:
Select
vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
vc.[Case Status],
vc.[Reporting Structure Level 4],
vc.[Reporting Structure Level 5],
vc.[Application Date],
vc.[Eligibility Date],
vc.[Eligibility Determination Extension Date],
vc.[Eligibility Extended To Date],
vc.[Days in Application],
cn.[Case_Note_Date],
cn.[Case_Note_Category_Desc],
cn.[Case_Note_Summary_Narr]
From
biVR_Cases vc
Left outer Join
buCase_Note cn ON cn.Case_Master_ID = vc.[_Case Master ID]
I need to keep biVR_Cases on the left to show all the open clients. Then I need to join in the case note table and for every participant, I want to show their max case note date. When I add this to the end of the above query:
Where cn.[Case_Note_Date] = (
Select
MAX(cn.Case_Note_Date)
From buCase_Note)
I get the following error is SSMS 2012:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
I am looking to retain the bi table on the left while successfully joining in the case note table and bringing in only the most recent case note per participant.
Adding details:
Of course,
Here is a sample of data I get when running the following query:
Select
vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
cn.[Case_Note_Date],
From biVR_Cases vc
LEFT outer JOIN buCase_Note cn ON vc.[_Case Master ID] = cn.Case_Master_ID
_Caseload Assignment Current Test Participant Name Casenote Date
Test Counselor Participant A
September 29, 2010
September 23, 2010
August 30, 2010
June 30, 2010
June 1, 2010
The bi table contains participant information like name, application, case master ID, etc. The casenote table contains the case master ID as well hence the join. It also contains the dates each entry is created. So for the dataset above, I am trying to only bring in the most recent casenote for each participant. I only included 1 in the sample above, but we have over 15,000. Each participant will have many case notes. I am trying to grab the top casenote so I can calculate the date difference between the most recent case note and today for each participant. When I add :
Where cn.[Case_Note_Date] = (Select
top 1 [Case_Note_Date]
From buCase_Note
Order by 1 DESC))
OR
Where Case_Note_Date=(
Select
MAX(Case_Note_Date)
From buCase_Note)
It is only showing the top or max casenote for participants that had a casenote created today. Instead of showing the max casenote in the casenote table, I need the max casenote per participant. I hope that makes more sense.
You might try something like what I have included below. Without actual data, I do not know if it is efficient enough for you, but it should work. If you get a better answer, however, I would love to know it.
Select vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
vc.[Case Status],
vc.[Reporting Structure Level 4],
vc.[Reporting Structure Level 5],
vc.[Application Date],
vc.[Eligibility Date],
vc.[Eligibility Determination Extension Date],
vc.[Eligibility Extended To Date],
vc.[Days in Application],
cn.[Case_Note_Date],
cn.[Case_Note_Category_Desc],
cn.[Case_Note_Summary_Narr]
From biVR_Cases vc
LEFT outer JOIN
(SELECT Case_Master_ID, Case_Note_Date, Case_Note_Category_Desc, Case_Note_Summar_Narr,
ROW_NUMBER() OVER (PARTITION BY Case_Master_ID ORDER BY Case_Note_Date DESC) as RowNum FROM buCase_Note) cn
ON cn.Case_Master_ID = vc.[_Case Master ID] AND cn.RowNum=1
Remove the cn. from the MAX(cn.Case_Note_Date) row. You don't want to reference column from the main query. You just want to select a Case_Note_Date from buCase_Note.
So the whole query will be
Select
vc.[_Case Master ID],
vc.[_Caseload Assignment Current],
vc.[_Participant Name],
vc.[Case Status],
vc.[Reporting Structure Level 4],
vc.[Reporting Structure Level 5],
vc.[Application Date],
vc.[Eligibility Date],
vc.[Eligibility Determination Extension Date],
vc.[Eligibility Extended To Date],
vc.[Days in Application],
cn.[Case_Note_Date],
cn.[Case_Note_Category_Desc],
cn.[Case_Note_Summary_Narr]
From biVR_Cases vc
LEFT outer JOIN buCase_Note cn ON cn.Case_Master_ID = vc.[_Case Master ID]
Where cn.[Case_Note_Date] = (Select
MAX(Case_Note_Date)
From buCase_Note)