Home > database >  SQL statements, thank you!
SQL statements, thank you!

Time:12-07

CodePudding user response:

LZ you this is a few cents of mobile phone, camera so fuzzy

CodePudding user response:

Can see you look at the following form will know what do you mean

CodePudding user response:

 
AS WITH T (
SELECT 1 ID, '01' id_card, 1 person_type, '2001-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT 2 ID, '02' id_card, 1 person_type, '2002-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT 3 ID, '01' id_card, 1 person_type, '2003-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT 4 ID, '02' id_card, person_type 2, '2004-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT 5 ID, '03' id_card, person_type 2, '2005-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT 6 ID, '04' id_card, 1 person_type, '2006-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
7 ID, SELECT '01' id_card, 1 person_type, '2007-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT ID, 8 '03' id_card, 1 person_type, '2008-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
SELECT ID, 9 '01' id_card, 1 person_type, '2009-01-01' fee_date, 20 fee_amount FROM dual
UNION ALL
The SELECT 10 ID, '04' id_card, person_type 2, '2010-01-01' fee_date, 20 fee_amount FROM dual
)
The SELECT PERSON_TYPE, SUM (COUNT_), SUM (COUNT_2)
The FROM (SELECT ROW_NUMBER () OVER (PARTITION BY ID_CARD ORDER BY FEE_DATE DESC) RN,
COUNT (*) OVER (PARTITION BY ID_CARD) COUNT_,
COUNT (*) OVER (PARTITION BY ID_CARD) * FEE_AMOUNT COUNT_2,
T. *
The FROM T)
WHERE an RN=1
GROUP BY PERSON_TYPE;

CodePudding user response:

refer to the second floor ZZZQQQHHHHHH response:
can see you look at the following form will know what do you mean

Thank you very much!

CodePudding user response:

The SELECT PERSON_TYPE, SUM (COUNT_), SUM (COUNT_2)
The FROM (SELECT ROW_NUMBER () OVER (PARTITION BY ID_CARD ORDER BY FEE_DATE DESC) RN,
COUNT (*) OVER (PARTITION BY ID_CARD) COUNT_,
COUNT (*) OVER (PARTITION BY ID_CARD) * FEE_AMOUNT COUNT_2,
T. *
The FROM T)
WHERE an RN=1
GROUP BY PERSON_TYPE;
  • Related