'Not Like' Not Working - sql-server-2005

I'm trying to return results for service orders from a table that contains Document No. and Unit of Measure as the column header. The Unit of Measure column contains terms 'EACH', 'CHARGE' and 'HOUR'. I do not want to return any results for any Document No. whose Unit of Measure data contains the term 'Hour' I've tried Unit of Measure Not Like = 'Hour' in my Where clause but I still keep getting results with Hour. See two service orders from table I am trying to query. I dont want to return "Document No. svo-13352.
select [Document No.]
from [Service Line]
where [Unit of Measure] not like 'HOUR'

Something like this should work:
select distinct ([Document No.]) from [Service Line]
where [Document No.] not in
(
select distinct( [Document No.] ) from [Service Line] where [Unit of Measure] like 'HOUR'
)

Related

SQL create column for every week (Loop?)

I need to make a report for weekly changes.
This is the code for todays amount
SELECT
[Entry No_],
[Customer No_],
[Posting Date],
[Description],
[Currency Code],
Trans_type = case when [Deposit]=1 then 'Deposit'
when [Imprest]=1 then 'Imprest'
else 'Other' end,
A.Amount
FROM Table1
LEFT JOIN
(
SELECT Distinct [Cust_ Ledger Entry No_],
SUM ([Amount EUR]) as 'amount'
FROM Table2
group by [Cust_ Ledger Entry No_]
having
SUM ([Amount EUR]) <> '0'
)A
on [Entry No_] = A.[Cust_ Ledger Entry No_]
Where
A.Amount is not NULL
Code to generate data for previous week is here (adding only where clause):
SELECT
[Entry No_],
[Customer No_],
[Posting Date],
[Description],
[Currency Code],
Trans_type = case when [Deposit]=1 then 'Deposit'
when [Imprest]=1 then 'Imprest'
else 'Other' end,
A.Amount
FROM Table1
LEFT JOIN
(
SELECT Distinct [Cust_ Ledger Entry No_],
SUM ([Amount EUR]) as 'amount'
FROM Table2
where [posting Date] < '2020-11-23'
group by [Cust_ Ledger Entry No_]
having
SUM ([Amount EUR]) <> '0'
)A
on [Entry No_] = A.[Cust_ Ledger Entry No_]
Where
A.Amount is not NULL
It would be enough to union both queries and then export to Excel and make pivot, but problem is that I need results of last 50 weeks. Is there any smart way to avoid union 50 tables and run one simple code to generate weekly report?
Thanks
it might be easier with sample, but I don't know how to paste table here..
Maybe it is true, i dont need union here, and group by would be enough, but it stills sounds difficult for me :)
Ok. Lets say table has such headers: Project | Country | date | amount
The code below returns amount for todays date
Select
Project,
SUM(amount)
From Table
Group by Project
I actually need todays date and also the results of previous weeks (What was the result on November 22 (week 47), November 15 (week 46) and so on.. total 50 weeks from todays date).
Code for previous week amount is here:
Select
Project,
SUM(amount)
From Table
Where Date < '2020.11.23'
Group by Project
So my idea was to create create 50 codes and join the results together, but i am sure it is a better way to do this. Besides i dont want to edit this query every week and add a new date for it.
So any ideas, to make my life easier?
if I have understood your requirement correctly, all you need to do is extract the week from the date e.g.
Select
Project,
datepart(week, date),
SUM(amount)
From Table
Where Date < '2020.11.23'
Group by Project, datepart(week, date)

SQL sorting query

I have following table with following relevant columns-
Invoices-
Invoice Number | Invoice Date | Invoice Status
Invoice status can have values : PENDING, SENT, COURIER, LR, CANCELLED, DONE
I want to order the records in this table such that,
Invoices older than 2 days but not having status CANCELLED and DONE should appear first
then
Invoices newer then 2 days but not having status CANCELLED and DONE should appear first
then
All invoices having CANCELLED or DONE should be last on the list
How to achieve this in SQL.
I am using SQL server and writing stored procedure is something I am not keen to do it.
Ideally it should be single SQL statement.
What would be the solution for this problem?
You do this using expressions in the order by:
select i.
from invoices i
order by (case when invoicedate < dateadd(day, -2, getdate()) and
status not in ('Cancelled', 'Done')
then 1
when status not in ('Cancelled', 'Done')
then 2
then 3
end);
A stored procedure is not necessary for this (nor in my opinion is it desirable).
Please try the following...
SELECT [Invoice Number],
[Invoice Date],
[Invoice Status]
FROM Invoices
WHERE [Invoice Status] NOT IN ( 'CANCELLED', 'DONE' )
AND DATEADD( DAY, -2, GETDATE() ) > [Invoice Date]
UNION
SELECT [Invoice Number],
[Invoice Date],
[Invoice Status]
FROM Invoices
WHERE [Invoice Status] NOT IN ( 'CANCELLED', 'DONE' )
AND DATEADD( DAY, -2, GETDATE() ) <= [Invoice Date]
UNION
SELECT [Invoice Number],
[Invoice Date],
[Invoice Status]
FROM Invoices
WHERE [Invoice Status] IN ( 'CANCELLED', 'DONE' );
In each of by SELECT statements I have chosen to show all the fields and in the same order. Note that the corresponding fields in the SELECT statements making up a UNION should always be of the same type. In some situations a field of one type may be converted to match another, but this can sometimes lead to troubles and it is strongly recommended that you try to avoid such situations.
In my first statement I compare the date two days before the current date to the [Invoice Date] and if it is greater (i.e the Invoice is older than 2 days) then the row is included (subject to its [Invoice Status]).
For the second statement I choose invoices where the date is no more than two days before the current one.
For the third statement I show all Invoices where the status is either CANCELLED or DONE.
You can specify sorts, groupings, etc. of similar or different patterns within each list, but since you have not specified any such in your Question I have not attempted to implement them.
If you have any questions or comments, then please feel free to post a Comment accordingly.

How do I divide the sum of one column by the sum of another column when they are from different tables in Access?

I want to grab the sum of two identically named columns, except they are from two different tables (I have been told to combine them multiple times, unfortunately I do not have a choice in the matter...). I want to then divide these two summed values to find the percentage variation.
Below is the code I wrote, all it does is load infinitely.
SELECT SUM(C.[Market Value]) AS CSUM,
SUM(P.[Market Value]) AS PSUM,
CSUM/PSUM AS Percentage_Variation
FROM JanReport AS P,
FebReport AS C;
I have been unsuccessful in using UNION and JOIN. It seems like this function should be easily doable, as I have been able to sum and find % variation by asset class, I can even get the sum of both columns to show with the Totals function. However I just cannot seem to get them to divide.
Code that worked:
SELECT SUM(FebReport.[Market Value]) AS Curr_Total_MV,
(
SELECT SUM(JanReport.[Market Value]) FROM JanReport
) AS Prior_Total_MV,
SUM(FebReport.[Market Value]) /
(
SELECT SUM([Market Value]) FROM JanReport
) AS Percentage_Variation_In_Total_MV,
IIf(
0.9<Percentage_Variation_In_Total_MV AND
Percentage_Variation_In_Total_MV<1.1,'Pass','Fail') AS Result
FROM FebReport;
Inline SELECT should work here:
SELECT SUM([Market Value]) / (SELECT SUM([Market Value]) FROM JanReport) AS Percentage_Variation,
FROM FebReport;
Update: Two sub-queries each returning just one row can be cross-joined to provide the underlying values:
SELECT
[Current Market Value],
[Previous Market Value],
[Current Market Value] / [Previous Market Value] As Percentage_Variation
FROM
(SELECT SUM([Market Value]) As [Current Market Value] FROM FebReport) AS C,
(SELECT SUM([Market Value]) As [Previous Market Value] FROM JanReport) AS P;

Error in SQL sum()

I have this SQL line
SELECT No_, sum(Quantity) AS Sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104' GROUP BY No_
But i got this error for my script.
Column 'dbo.3S Company A_S$Sales Invoice Line.Shipment Date' is invalid in the select
list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone help me with why?
If you use GROUP BY in your query, only the columns used in your grouping clause and any aggregate functions like SUM are allowed in the select list. In your case, you specify GROUP BY No_, so that is the only column you can select without using an aggregate function.
If you want to get the remaining columns, you could select No_ and the other aggregate columns in a subquery and then select other columns by matching the No_ column with the corresponding column in subquery.
The error means you have a column which may have multiple values when grouped and SQL doesn't know which value to select in the column
You cn use e.g. min() to select min value. Like this
SELECT No_,
sum(Quantity) AS Sold,
min([Shipment Date]) AS SoldDate,
min([Item Category Code]),
min(Description),
min([Description 2])
FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104'
GROUP BY No_
Or read about aggregate functions to choose proper one
BWT it's not MySQL but rather MS SQL (MySQL does not complain the column usages)
TRY THIS
SELECT No_, sum(Quantity) AS Sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line]
WHERE [Item Category Code] = '5104' GROUP BY No_,[Shipment Date],
[ItemCategoryCode], Description,[Description 2]
IN SQL IF YOU USE ANY COLUMN NAME IN SELECT CLAUSE EXCEPT AGGREGRATE FUNCTION THEN YOU NEED TO ADD ALL THE COLUMNS IN GROUP BY ALSO OTHER WISE IT WILL SHOW EXCEPTION
IF YOU WANT SUM ONLY BY NO_ Column then you have to write a subquery with the aggregarte function and join it to you other columns as folows
SELECT No_ ,quant.sold, [Shipment Date] AS SoldDate, [Item Category Code],
Description, [Description 2] FROM dbo.[3S Company A_S$Sales Invoice Line] INV,
(SELECT No_, sum(Quantity) AS Sold from dbo.[3S Company A_S$Sales Invoice Line] where
WHERE [Item Category Code] = '5104' group by No_) quant
WHERE [Item Category Code] = '5104' and
inv.no_=quant.no_
The columns (Other than aggregate functions) that exists in select clause should also present in the group by clause. This is what the error message states.
Select Productid, Sum(Saled) from product
Group by ProductId
In the above example, ProductId is in the group by clause. So that query is valid. If you introduce one more column that should also be in the group by clause to avoid error.
You have to put columns not using the aggregate functions in your GROUP BY :
GROUP BY
No_
, [Shipment Date]
, [Item Category Code]
, Description
, [Description 2]

GROUPING A TABLE BASED ON MULTIPLE FIELDS

I have a table with following format
Now i am using Access and want to create a table which will provide me sum of units where Segment is commercial, at the same time I just want to sum Subregions where they are same..means MCA+MCA+MCA... Also the period should be same where we have added them..means 2013Q1 should be added too 2013Q1 only and grouped in that format.
Something like below
Thanks and Regards
The following query totals the unit while grouping on the other fields. To change the range that it totals over, you will either be adding a WHERE clause or you will change the fields that it is GROUP BY
SELECT [Sub Region], [Corporate Family], [HP Segment], [Product Category], [Period], Sum([Units])
FROM TableName
GROUP BY [Sub Region], [Corporate Family], [HP Segment], [Product Category], [Period]