I have this kind of data in my table
lineid
price €
01
100.00
02
200.00
01
10.34
01
311.12
01
14.33
02
36.44
03
89.70
04
11.33
and i would like my output to be like this
docid
lineid
price €
1
01
100
1
02
200.00
2
01
10.34
3
01
311.12
4
01
14.33
4
02
36.44
4
03
89.70
4
04
11.33
Its data for invoices and for every line that has lineid='01' it means that the info is for different invoice so i have to mark it with new documentID that i want you to help me create it with a command.
Its probably something easy but i am searching like a maniac here and i cant find the solution.
EDIT: Yes , it Is "increment docid each time lineid equals 01" what i want
You could use running counts using something like below (assuming this is MS SQL you are talking about)
SELECT ROW_NUMBER() over(partition by [LineId] order by [LineId]) as DocId,
[LineId],
[Price]
FROM [StackOverflow].[dbo].[RunningCount] order by [LineId]
Related
I have a table with many informations about my customers, in summary :
id
dateInscription
specialite
dept
1
2018-04-09
Anesthesiology
75
2
2004-02-16
Neurology
62
3
1999-01-01
Pathology
34
4
2016-05-13
Family medicine
59
I want to calculate the total number of customer by year, Speciality and country code.
I already find a way to calculate the NEW customer with this query, but not the total :
SELECT
YEAR(dateInscription) as 'Annee_inscription',
a.specialite,
a.dept as 'Departement',
COUNT(a.id) as 'NOMBRE_PS'
FROM
customers a
WHERE
YEAR(dateInscription) IN(2013,
2015,
2017,
2019,
2021)
AND a.specialite IN ('ANATOMIE ET CYTOLOGIE PATHOLOGIQUES',
'ANESTHESIE-REANIMATION',
'BIOLOGIE MEDICALE',
'CARDIOLOGIE/PATHOLOGIE CARDIO-VASCULAIRE')
GROUP BY
Annee_inscription,
a.specialite ,
Departement
ORDER BY
Annee_inscription ASC,
a.specialite ASC,
Departement ASC
In the best world, i want an output like this :
Year_range
specialite
dept
number_customer
1999-2004
Anesthesiology
01
10
1999-2004
Anesthesiology
02
13
1999-2004
Anesthesiology
03
25
...
...
...
....
1999-2004
Family medicine
01
124
1999-2004
Family medicine
02
514
1999-2004
Family medicine
03
1284
...
...
...
....
1999-2006
Anesthesiology
01
15
1999-2006
Anesthesiology
02
17
1999-2006
Anesthesiology
03
29
...
...
...
....
i try to group by case but with no good result.
Pro tip : i dont have the write right on this database
In advance a big thank to you.
One option to solve this problem is to:
first transform your "dateInscription" input field to the corresponding "Year_range" output field (by extracting the minimum and maximum year partitioned by "specialite" values)
then apply your aggregation on the "Year_range" field, as well as "specialite" and "dept" fields
WITH cte AS (
SELECT *,
CONCAT_WS('-',
MIN(YEAR(dateInscription)) OVER(PARTITION BY specialite),
MAX(YEAR(dateInscription)) OVER(PARTITION BY specialite)) AS Year_range
FROM tab
)
SELECT Year_range,
specialite,
dept,
COUNT(*) AS number_customer
FROM cte
GROUP BY Year_range,
specialite,
dept
Check the demo here.
Note: the demo will allow you to play with the query, though for a better troubleshooting, more input data and corresponding expected output may be required.
Would you prompt me, please how to create the additional line on the line chart, which contains sum over all the lines in the chart.
E.g.:
-we have sales over months:
-x-axis is months
-y-axis is sum of sales
On the line chart, we have 3 lines:
-sales on office1
-sales on office2
-sales on office3
On the same chart, I need to add a line with summa of sales over 3 offices.
Thank you.
It depends on what your dataset looks like but the easiest way is usually to do this in your dataset query and supply the 'Total' office numbers in the same manner as the current numbers.
So, if your data looked like this
Office
Month
Amount
Office1
01
1000
Office2
01
1100
Office3
01
1200
Office1
02
1300
Office2
02
1400
Office3
02
1600
Office1
03
1700
Office2
03
1800
Then you could do something simple like
SELECT Office, Month , Amount FROM myTable
UNION ALL
SELECT 'Total', Month, SUM(Amount) from myTable GROUP BY Month
This way "Total" just gets displayed like any other office.
I hope someone can help. I'm trying to set up something along the below but am getting in a bit of a muddle. From what I understand, deriving a numeric ID variable (eg auto-incremented) for the primary key is more efficient that using a composite primary key of the 'natural' variables that define a record (especially if they're character variables (and more so if the collation is UTF-8))
As in the below example, each customer has a list of items (ITEMID) which are all members of a category (CATID), however, the problem is that I need customers to additionally be able to assign their items as a component of a collection set (SETID) which is a non-identifying reference table - any customer could have multiple versions of one SETID.
The items required for a set are specified by CATID. Therefore, in the example below which is for one customer, they could choose to assign Item 2, or 4 (or neither or both) to SET 001.
**ITEMS**
ITEMID CATID
1 04
2 02
3 01
4 02
5 05
**SETS**
SETID CATID
001 01
001 02
002 04
003 05
**CATEGORY**
CATID
01
02
03
04
05
**Wanted result:**
ITEMID CATID SETNUMBER SETID
1 04 (customer chose not to assign to SET 002)
2 02 1 001
3 01 1 001
4 02 2 001
5 05 003
Many thanks in advance!
From your description, it sounds like the "ITEM" table could stand to have a NULLable "SETID" foreign-key column.
looks easy but difficult for me. the src/dest path of idx 1, 2, 3 has the same values.
so I need only 1 row for them.
idx Src_path dest_path code
1 /abc/aaa.txt /abc/dec_aaa.txt 01
2 /abc/aaa.txt /abc/dec_aaa.txt 02
3 /abc/aaa.txt /abc/dec_aaa.txt 03
4 /abc/aaa.txt /abc2/dec_aaa.txt 04
5 /abc/bbb.txt /abc2/dec_bbb.txt 01
6 /abc/ccc.txt /abc2/dec_ccc.txt 01
the result rows should be like below..
idx Src_path dest_path code
3 /abc/aaa.txt /abc/dec_aaa.txt 03
4 /abc/aaa.txt /abc2/dec_aaa.txt 04
5 /abc/bbb.txt /abc2/dec_bbb.txt 01
6 /abc/ccc.txt /abc2/dec_ccc.txt 01
bit.. difficult for me..
naw... kinda lazy you are, but thank god its friday.
SELECT MAX(idx), src_path, dest_path, MAX(code)
FROM yourtable
GROUP BY src_path, dest_path
should work out.
use SELECT DISTINCT Src_path
otherwise you can use
GROUP_CONCAT(Src_path) but then you will have to GROUP_BY
I have a dataset like this:
Item Value Date Show
IT1 10 2012 01 01 1
IT1 9 2012 01 02 1
IT1 11 2012 01 03 1
IT2 8 2012 01 01 1
IT2 5 2012 01 02 1
IT2 3 2012 01 03 1
In a chart I have a filter which checks the Show value and leaves only rows where Show is 1. The problem is that instead of 2 items I see only 1, even though both of them have show = 1.
EDIT:
Filter expressions i tried:
=Fields!Show.Value = =1
=CInt(Fields!Show.Value) = =1
=CInt(Fields!Show.Value) = =CInt(1)
=CBool(Fields!Show.Value) = =CBool(1)
=CBool(Fields!Show.Value) = =CBool(True)
After using these expressions i get dataset like this:
Item Value Date Show
IT1 10 2012 01 01 1
I'm confused by your example - your dataset has 6 rows where show = 1 ?
Anyway, SSRS Filters are so obscure and generally best avoided, but this technique seems to be more reliable:
Filter Expression: =Fields!Show.Value = 1
Result Type: Boolean
Operator: =
Value: True