I have this SQL, but it dosen't work correctly.
SELECT [Customer No_], SUM(Amount) AS SumDebitor, [Posting Date]
FROM dbo.[3S Company A_S$Detailed Cust_ Ledg_ Entry]
WHERE ([Posting Date] <= CONVERT(DATETIME, '2015-04-10 00:00:00', 102))
GROUP BY [Customer No_], [Posting Date]
HAVING ([Customer No_] = '45')
What i want, is get the total SUM() of all posts from before my date.
Right now i get more that 5000 results, sum of everyday.
Can someone help me on the right way?
you should not have posting date with grouping (if you do posting date grouping .. you will get all posting date sum independently)
and moreover having clause is not required .. your query should be like following
SELECT [Customer No_], SUM(Amount) AS SumDebitor
FROM dbo.[3S Company A_S$Detailed Cust_ Ledg_ Entry]
WHERE ([Posting Date] <= CONVERT(DATETIME, '2015-04-10 00:00:00', 102))
And ([Customer No_] = '45')
GROUP BY [Customer No_]
or as below (if you need count for all customer)
SELECT [Customer No_], SUM(Amount) AS SumDebitor
FROM dbo.[3S Company A_S$Detailed Cust_ Ledg_ Entry]
WHERE ([Posting Date] <= CONVERT(DATETIME, '2015-04-10 00:00:00', 102))
GROUP BY [Customer No_]
Related
I have this query in SQL:
select cast(faturas.datatotal as date) as Dia, faturas.sumF, credito.sumC, isnull(sumF,0)-isnull(sumC,0) as Total
from
(SELECT SUM([Line Amount]) as sumF, [Posting Date] as datatotal
FROM [CMW$Sales Invoice Line]
where [CMW$Sales Invoice Line].[Posting Date] >= '2017-01-01'
group by [CMW$Sales Invoice Line].[Posting Date]) faturas
full outer join
(SELECT SUM(Amount) as sumC, [Posting Date]
FROM [CMW$Sales Cr_Memo Line]
where [CMW$Sales Cr_Memo Line].[Posting Date] >= '2017-01-01'
group by [CMW$Sales Cr_Memo Line].[Posting Date]) credito
on faturas.datatotal=credito.[Posting Date]
I need to calculate the cumulative "Total" in this same query. How can I get this?
I solved my problem with this:
sum(isnull(sumF,0)-isnull(sumC,0)) over(order by faturas.datatotal rows unbounded preceding) as Acumulado
Thanks
I have used DATEDIFF to distinguish between when the first unit rate was created and the posting date is when the first transaction of that item was posted.
I have the result that I need , however the DateDiff function gives me NULL values starting date for some rows.
SELECT DISTINCT b.[Entry No_] ,
a.[Starting Date],
b.[Posting Date],
b.[Item No_],
b.[Invoiced Quantity],
a.[Litre Conversion Factor],
a.[Unit Rate] ,
b.[Location Code],
a.[Excise Location],
a.[Excise Type Code],
a.[Unit Of Measure Code]
FROM [Spier Live$Value Entry] b
LEFT JOIN [Transfer Excise Tbl] a
ON a.[No_] = b.[Item No_]
AND b.[Location Code] = a.[Location Code]
AND DateDiff(y,b.[Posting Date],a.[Starting Date]) > -365 --DateDiff Year -365 for starting date
AND DateDiff(y,b.[Posting Date],a.[Starting Date]) < 0 --DateDiff Yer < 0 for posting date
WHERE b.[Posting Date] > '2013-02-26' --This is when the unit rate was entered
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')
AND b.[Invoiced Quantity] <>0 --Removing all zero values
AND b.[Item No_] = 'F00335'
ORDER BY b.[Posting Date]
My Result
Transfer Excise Tbl This is the table I am joining on
As alex points out in the comments, you're looking for things with datediffs based on day of year, not year. Did you mean <= 0, btw? These criteria don't make sense and so probably aren't what you really want. If so, they'd cause joins to fail where you don't really want them to, leading to nulls showing for table a columns.
Good day
I have two tables I need to Join , Transfer Excise Tbl and Value Entry.
Transfer Excise Tbl: No must match the Item no in the Value Entry table. I did do a comparison for Items not in Transfer Excise that is in Value entry and found a few.
Transfer Excise Tbl:
Starting Date No_ Excise Location Location Code Unit Rate Excise Type Code Unit Of Measure Code Litre Conversion Factor
----------------------- -------------------- --------------- ------------- --------------------------------------- ---------------- -------------------- ---------------------------------------
2013-02-28 00:00:00.000 600011263 NONBOND ~DUTY PAID 2.70000000000000000000 UWNEPACK LITRES 1.33333000000000000000
2014-02-27 00:00:00.000 600011263 NONBOND ~DUTY PAID 2.87000000000000000000 UWNEPACK LITRES 1.33333000000000000000
2015-02-26 00:00:00.000 600011263 NONBOND ~DUTY PAID 3.07000000000000000000 UWNEPACK LITRES 1.33333000000000000000
2016-02-25 00:00:00.000 600011263 NONBOND ~DUTY PAID 3.31000000000000000000 UWNEPACK LITRES 1.33333000000000000000
Value Entry Table:
Item No_ Location Code Gen_ Bus_ Posting Group Invoiced Quantity
-------------------- ------------- ----------------------- ---------------------------------------
F00330 VINI EXSA -10.00000000000000000000
F00331 VINI EXSA -30.00000000000000000000
F00332 VINI EXSA -40.00000000000000000000
I want to write the query to exclude duplicates as the script below still creates duplicates. The PK is Item No and the FK is Location Code. you will see on the Transfer excise table that for each year I new unit rate was supplied for a specific Item and Location
SELECT DISTINCT a.[Starting Date],
b.[Posting Date],
b.[Item No_],
b.[Invoiced Quantity],
a.[Litre Conversion Factor],
a.[Unit Rate] ,
a.[Location Code],
a.[Excise Location],
a.[Excise Type Code],
a.[Unit Of Measure Code]
FROM [Transfer Excise Tbl] a JOIN [Spier Live$Value Entry] b
ON a.[No_] = b.[Item No_]
WHERE b.[Posting Date] > '2013-02-26 '
AND b.[Location Code] = a.[Location Code]
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')
AND b.[Posting Date] >= a.[Starting Date]
AND b.[Invoiced Quantity] <>0
First of all, there is something wrong with your [Value Entry] table.
1) In your query you refer to [Posting Date] column, but there is no such column in your example data.
Now, if I have well understood the scenario, I think your problem is related to how you join lines from the two tables.
I get more lines than you expect because you JOIN each line in [Value Entry] with ALL lines in [Transfer Excise Tbl] with a [Starting Date] older, not only the LAST (valid) line.
To solve the problem you should pre-calc the period of validity of your [Transfer Excise Tbl] line finding the [End Date] of each line, and then you will
JOIN b.[Posting Date] BETWEEN a.[Starting Date] AND a.[End Date]
final query will be something like:
;WITH
EndDates as (-- add [End Date] to [Transfer Excise Tbl]
select t1.*, ISNULL([End Date], CONVERT(date, '9999-12-31', 121)) [End Date]
from [Transfer Excise Tbl] t1
outer apply (
select MIN([Starting Date]) [End Date]
from [Transfer Excise Tbl]
where [Starting Date] > t1.[Starting Date]
) T2
)
SELECT DISTINCT a.[Starting Date],
b.[Posting Date],
b.[Item No_],
b.[Invoiced Quantity],
a.[Litre Conversion Factor],
a.[Unit Rate] ,
a.[Location Code],
a.[Excise Location],
a.[Excise Type Code],
a.[Unit Of Measure Code]
FROM [EndDates] a JOIN [Spier Live$Value Entry] b ON a.[No_] = b.[Item No_] AND b.[Posting Date] BETWEEN a.[Starting Date] AND a.[End Date]
WHERE b.[Posting Date] > '2013-02-26 '
AND b.[Location Code] = a.[Location Code]
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')
AND b.[Invoiced Quantity] <> 0
It should return only the number of rows you expect
I hope this helps
I have a SQL query which gives me the result of invoice lines.
I got all true information, but I want to group it. The problem is that I already grouped it.
There is my code:
WITH Agg1 AS(
SELECT [Ship-to Name] As 'Pirkejas',
[Order No_]
FROM [Artilux].[dbo].[Trivilita UAB$Sales Invoice Header]
)
SELECT sil.[Document No_] as 'Pardavimo nr.',
sil.[Shipment Date] as 'Siuntos data',
sil.[Order No_] as 'Musu užsak. nr.',
sil.[Customer Order No_] AS 'Klientio užsak. nr.',
a.[Pirkejas],
CAST(sil.[Unit Volume]*sil.[Quantity] AS DECIMAL(16,4)) AS 'Tūris',
CAST(sil.[Unit Price]*sil.[Quantity] AS DECIMAL(16,2)) AS 'Suma €'
FROM [Artilux].[dbo].[Trivilita UAB$Sales Invoice Line] sil
LEFT JOIN Agg1 a
ON a.[Order No_] = sil.[Order No_]
WHERE sil.[Sell-to Customer No_] = 'PRK0820'
AND sil.[Shipment Date] > '2015-09-01 00:00:00.000'
AND sil.[Shipment Date] <= dateadd(day,datediff(day,0,GETDATE()),0) AND sil.[Document No_] = 'TRV0093219'
GROUP BY
[Unit Price],[Quantity],
sil.[Document No_],
sil.[Unit Volume],
sil.[Shipment Date],
sil.[Order No_],
sil.[Customer Order No_],
a.[Pirkejas]
ORDER BY sil.[Shipment Date] DESC
The result I get:
TRV0093219 2015-12-22 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 0.2400 17.35
TRV0093219 2015-12-22 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 0.8140 121.00
TRV0093219 2015-12-21 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 0.0000 0.00
TRV0093219 2015-12-21 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 0.0000 0.00
TRV0093219 2015-12-21 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 0.0000 0.00
The result I need to get:
TRV0093219 2015-12-21 00:00:00.000 SO0184846 IO1710379-C Living AS, Lade 1.054 138.35
Remove those columns from GROUP BY and add an aggregate function:
WITH Agg1 AS(
SELECT [Ship-to Name] As 'Pirkejas',
[Order No_]
FROM [Artilux].[dbo].[Trivilita UAB$Sales Invoice Header]
)
SELECT sil.[Document No_] as 'Pardavimo nr.',
sil.[Shipment Date] as 'Siuntos data',
sil.[Order No_] as 'Musu užsak. nr.',
sil.[Customer Order No_] AS 'Klientio užsak. nr.',
a.[Pirkejas],
SUM(CAST(sil.[Unit Volume]*sil.[Quantity] AS DECIMAL(16,4))) AS 'Tūris',
SUM(CAST(sil.[Unit Price]*sil.[Quantity] AS DECIMAL(16,2))) AS 'Suma €'
FROM [Artilux].[dbo].[Trivilita UAB$Sales Invoice Line] sil
LEFT JOIN Agg1 a
ON a.[Order No_] = sil.[Order No_]
WHERE sil.[Sell-to Customer No_] = 'PRK0820'
AND sil.[Shipment Date] > '2015-09-01 00:00:00.000'
AND sil.[Shipment Date] <= dateadd(day,datediff(day,0,GETDATE()),0) AND sil.[Document No_] = 'TRV0093219'
GROUP BY
sil.[Document No_],
sil.[Shipment Date],
sil.[Order No_],
sil.[Customer Order No_],
a.[Pirkejas]
ORDER BY sil.[Shipment Date] DESC
I have this table called myTable
Posting Date|Item No_|Entry Type|
2015-01-13|1234|1
2015-01-13|1234|1
2015-01-12|1234|1
2015-01-12|5678|1
2015-02-12|4567|1
What I want, is only return result where a [Item No_] is ind the table 1 time.
So in this example of my table, i only want to return [Item No_] 5678 and 4567, because there only are one record in it. And then ignore [Item No_] 1234
This is my SQL i have tried, but something is wrong. Can anyone help me?
SELECT [Item No_], [Posting Date], COUNT([Item No_]) AS Antal
FROM myTable
GROUP BY [Entry Type], [Posting Date], [Item No_]
HAVING ([Entry Type] = 1) AND (COUNT([Item No_]) = 1)
ORDER BY [Posting Date] DESC
select [Item No_]
from myTable
group by [Item No_]
having count(*)=1
Remove Posting Date from group by
SELECT [Item No_],Entry Type, COUNT([Item No_]) AS Antal
FROM myTable
GROUP BY [Entry Type], [Item No_]
HAVING COUNT([Item No_]) = 1
or if you want other details use a subquery
SELECT [Item No_],
Entry Type,
Posting Date
FROM myTable a
WHERE EXISTS (SELECT 1
FROM myTable b
where a.[Item No_]=b.[Item No_]
GROUP BY [Entry Type],
[Item No_]
HAVING Count(1) = 1)
ORDER BY [Posting Date] DESC
or window function
;WITH cte
AS (SELECT [Item No_],
[Posting Date],
[Entry Type],
Row_number()OVER (Partition BY [Entry Type], [Item No_] ORDER BY [Item No_]) RN
FROM myTable)
SELECT *
FROM cte a
WHERE NOT EXISTS (SELECT 1
FROM cte b
WHERE a.[Item No_] = b.[Item No_]
AND rn > 1)
ORDER BY [Posting Date] DESC
You can use ROW_Number() in Sql Server
select * from (
SELECT [Item No_], [Posting Date],
Row_Number() over (Parition by [Item No_]
order by [Item No]) RN
FROM myTable
)D
where D.RN=1