I'm trying to write some SQL to allow me to get result of possible duplicated Invoices that will have the same [same Items, with Same Quantity], That is possible to be Duplicate issued
Invoice items Average around 300 Item
Total Invoice To be Revision around 2500 Invoice
The Following is a invoices sample with only 1 items or so, but in real population items average is 300
Inv_ID Item_Code Item_Q
A-800 101010 24
A-801 101010 24
A-802 202020 9
A-803 101010 18
A-804 202020 9
A-805 202020 9
A-806 101010 18
Hoping The Excepted Result will be
A-800, A-801
A-802, A-804, A-805
A-803, A-806
But the invoice has around 200 item, and the duplicated invoices has to be has the same items and exact same quantity for these.
It's SQL_Server
And The Result need to match the whole Invoices item
Like Invoice A has 300 Different Items line with each one Quantity 2
The Results need to be all invoice has the exact same 300 Item with the Exact Quantity.
The Supplier has issued multiple duplicated invoice to our accounting
Department by mistakes over 4 years, it was discovered by chance, so
we need to find out the duplicated invoice to remove it from payment
schedule.
The issued invoices Need to has the exact different items with exact quantity to be considered duplicated.,,,
You didn't specify your DBMS product, so this answer is for Postgres.
select string_agg(inv_id, ',' order by inv_id) as inv_ids
from the_table
group by item_code, item_q
order by inv_ids;
Online example
Other DBMS products have similar functions to do the aggregation. In Oracle or SQL Server, you would use listagg(), in MySQL you would use group_concat(). The individual syntax would be slightly different (you have to check the manual), but the idea is the same.
Related
I am new to SQL and Stack overflow and have a question about SQL Server syntax. i have searched online but I am not finding what I need and I would appreciate your assistance in this matter.
I have data in a source table for meal orders (each with a specific ID (e.g. 12345C) and items of each order (e.g. sandwich, drink, chips), each with an associated number starting with 1. For instance, the sandwich would have an item number of 1, the chips would be item # 2, and the drink would be item # 3 for the same orderID 12345C. The prior example would therefore have 3 rows of data in the source table for orderID 12345C.
My questions are these:
how do I use a SQL expression to determine the number of items per each order (e.g. 3 for the above example, which is also the maximum value for item number for each orderID)
and then add all of these items per order across hundreds of orders per day to determine the daily total number of items ordered.
So, if I had 3 orders in one day - one with 2 items, the second with 3 items, and the third with 4 items, I would like my final number to be 9.
This number is for use in a Sisense dashboard that allows SQL syntax in the field definition. Thank you for your help!
It is a bit difficult to explain but I am not able to use a query from a table because I am working with a dashboard in Sisense so I am adding fields in a pivot display and one of the fields I would like to include is the total count of order items per day (across several dozen orderIDs).
Here is an example of the data in the table: from the example I would like the final answer for orderID 1787588 to be 3 (there are 3 items within the order).
I am using SSRS 2014. My end goal is to get a table that looks like the following:
Project Status # of Projects Oldest Project Avg Project Age
Status 1 27 82 16
Status 2 9 29 6
Status 3 13 112 25
Status 4 33 68 16
Status 5 1 63 63
Status 6 1 27 27
Grand Total 84 112 17
A table like this, including the grand totals, can be done in Excel using pivot tables (this is how the data was originally retrieved...manually...)
If I have the same source data in a table where my columns include projectStatus and projectAge, I could generate a query to get all but the grand totals in SQL:
SELECT projectStatus, COUNT(projectStatus), MAX(projectAge), AVG(projectAge)
FROM projects
GROUP BY projectStatus
If I wanted to add grand totals to that, I could do a UNION with the following query:
SELECT 'Grand Total', COUNT(projectStatus), MAX(projectAge), AVG(projectAge)
FROM projects
Note that the grand totals are calculated off of the entire dataset and not just the grouped data - this mostly applies to the average, because it just happens to work out for the COUNT and MAX columns.
Now, if I am trying to generate this above table in SSRS, it is easy to generate the above table sans grand totals using the former query. However, now how do I add grand totals?
I could instead try a different approach where I use a more generic dataset query like:
SELECT projectStatus AS statusCategory, projectAge AS daysInCurrentStatus FROM projects
And then use aggregate functions in my table and have SSRS do the aggregation itself and provide grand totals on the raw data. I get a new problem now. What is the correct way to set my table up? I am calling grouping by [statusCategory] as "Group1", using scoped aggregate functions like so:
And it correctly generates the grand totals, but now I have a new problem: it is showing the average for each row instead of just one cell per group/scope. Is this something SSRS is meant to do, and if so, how can I do it?
...
You need to mess around a little to get right layout, but you basically need a group header or footer to show the Group aggregates.
if you right click on the lines next to [statusCategory] and select Insert Row > Outside Group - Above it will create a group header for you. Then just add your aggregate expressions in the correct columns like =Count(Fields!projectStatus.Value) then you can just set Row Visibilty on the details row to hide if you dont want to see it.
you should end up with a layout similar to this.
with the middle row (detail) visibility set to false
You're on the right track. You have two Row Groups right now. All you need to do is remove the inner one because it's not grouped by anything. In other words, it's repeating for every row of raw data.
So the Row Group named "Details" - either remove it or in the properties click "Add" and group it by 1. That will collapse those rows and do what you were expecting.
I am trying to create a database that will essentially act as a matchmaking service. I have two tables with the same categories, Clients and Opportunities.
The idea is to enter buy-side Clients as we acquire them, filling out the applicable descriptive fields (EBITDA, Gross Revenue, etc).
Then, when a possible Opportunity comes along, I enter that info in its respective table (with EBITDA, Gross Revenue, etc).
The idea is then to run a query that will essentially "match" records from Clients with records from Opportunities, in a prioritized order, i.e. the items with most matching fields rank at the top.
So I've created the tables (Clients and Opportunities), a third table Values that contains the possible choices for several of the fields, and forms for entering the information into Opportunities and Clients.
I've entered a test record in both Opportunities and Clients tables with several matching fields. But my query isn't returning any hits. Per some advice I received, I tried to add in some SQL code to address a possible NULL issue, but Access won't accept the code. Can this query be written? Are the results I'm looking for possible?
My query, designed using the Wizard, looks like this and returns no hits, despite the sample records in each table having matching entries:
SELECT Clients.*, Opportunities.*
FROM Clients INNER JOIN Opportunities
ON (Clients.[NAICS Sector Codes]=Opportunities.[NAICS Sector Codes])
AND (Clients.[Business Model]=Opportunities.[Business Model])
AND (Clients.[Gross Revenue]=Opportunities.[Gross Revenue])
AND (Clients.EBITDA=Opportunities.EBITDA)
AND (Clients.[EBITDA Margin (%)]=Opportunities.[EBITDA Margin (%)])
AND (Clients.[Growth-Timeframe in Years]=Opportunities.[Growth-Timeframe in Years])
AND (Clients.[Growth-Revenue]=Opportunities.[Growth-Revenue])
AND (Clients.[Growth-EBITDA (%)]=Opportunities.[Growth-EBITDA (%)])
AND (Clients.[Geographical Operational Location Preference]=Opportunities.[Geographical Operational Location Preference])
AND (Clients.[Debt Load]=Opportunities.[Debt Load])
AND (Clients.[Primary Area of Sales Concentration (Geographic)]=Opportunities.[Primary Area of Sales Concentration (Geographic)])
AND (Clients.[% of total revenue from ≤ top 3 customers]=Opportunities.[% of total revenue from ≤ top 3 customers])
AND (Clients.[% of total revenue from ≤ top 10 customers]=Opportunities.[% of total revenue from ≤ top 10 customers])
AND (Clients.[% of total revenue from ≤ top 20 customers]=Opportunities.[% of total revenue from ≤ top 20 customers])
AND (Clients.[Current Management Post-Transaction Plan]=Opportunities.[Current Management Post-Transaction Plan])
AND (Clients.[Preferred Deal Structure]=Opportunities.[Preferred Deal Structure]) AND (Clients.NAPCS=Opportunities.NAPCS)
AND (Clients.Notes=Opportunities.Notes)
AND (Clients.[ZLD Sector Notes]=Opportunities.[ZLD Sector Notes]);
Background
I have a database that hold records of all assets in an office. Each asset have a condition, a category name and an age.
A ConditionID can be;
In use
Spare
In Circulation
CategoryID are;
Phone
PC
Laptop
and Age is just a field called AquiredDate which holds records like;
2009-04-24 15:07:51.257
Example
I've created an example of the inputs of the query to explain better what I need if possible.
NB.
Inputs are in Orange in the above example.
I've split the example into two separate queries.
Count would be the output
Question
Is this type of query and result set possible using SQL alone? And if so where do I start? Would it be easier to use Ms Excel also?
Yes it is possible, for your orange fields you can just e.g.
where CategoryID ='Phone' and ConditionID in ('In use', 'In Circulation')
For the yellow one you could do a datediff of days of accuired date to now and divide it by 365 and floor that value, to get the last one (6+ years category) you need to take the minimum of 5 and the calculated value so you get 0 for all between 0-1 year old etc. until 5 which has everything above 6 years.
When you group by that calculated column and select the additional the count you get what you desire.
I'm writing a margin report on our General Ledger and I've got the basics working, but I need to merge the rows based on specific logic and I don't know how...
My data looks like this:
value1 value2 location date category debitamount creditamount
2029 390 ACT 2012-07-29 COSTS - Widgets and Gadgets 0.000 3.385
3029 390 ACT 2012-07-24 SALES - Widgets and Gadgets 1.170 0.000
And my report needs to display the two columns together like so:
plant date category debitamount creditamount
ACT 2012-07-29 Widgets and Gadgets 1.170 3.385
The logic to join them is contained in the value1 and value 2 column. Where the last 3 digits of value 1 and all three digits of value 2 are the same, the rows should be combined. Also, the 1st digit of value 1 will always been 2 for sales and 3 for costs (not sure if that matters)
IE 2029-390 is money coming in for Widgets and Gadgets sold to customers, while 3029-390 is money being spent to buy the Widgets and Gadgets from suppliers.
How can I so this programmatically in my stored procedure? (SQL Server 2008 R2)
Edit: Would I load the 3000's into one variable table the and the 2000's into another, then join the two on value2 and right(value1, 3)? Or something like that?
Try this:
SELECT RIGHT(LTRIM(RTRIM(value1)),3) , value2, MAX(location),
MAX(date), MAX(category), SUM(debitamount), SUM(creditamount) FROM
table1 GROUP BY RIGHT(LTRIM(RTRIM(value1)),3), value2
It will sum the credit amount and debit amount. It will choose the maximum string value in the other columns, assuming they are always the same when value2 and the last 3 digits of value1 are the same it shouldn't matter.